Indikator: DivergenceFinder; ruft Funktionen: RB_CheckPeak, RB_CheckValley.
Indikator {***************************************} InputSC(slowkclassic(5, 3)),Mingap(3),MaxOfset(3),OscDif(.01),PrcDif(1),MinPeak(30), MinPeak2(30),MaxVal(70),MaxVal2(70),MaxNum(10);
ArrayscH[51](0),OscHBar[51](0),OscL[51](0),OscLBar[51](0);
VarscVal(0),LastH(0),LastL(0),Loop(0),FoundH(0),FoundL(0), StrtBar(0),EndBar(0),StrtVal(0),IncVal(0),NewPeak(false),NewVal(false), LastPeak(0),LastVal(0),EndVal(0); {MaxOfset - Number of bars away (in price chart) from corresponding peak or valley in Stochastic that is allowable and still considered valid Zero - tight, will produce fewest trades, the greater the number, the more trades that will be generated. MinGap - Minimun number of bars needed between peaks or valleys. OscDif - Minimum difference needed between points of Stochastic in order to be greater or less than. PrcDif - Minimun number of points between highs or lows of bars in order to be greater or less than. MinPeak - The minimum value needed for the first peak (of the stochastic) MaxVal - The Maximum value allowed for the first valley (of the stochastic)}
{Record current values of oscilator in OscH and OschL arrays. If we come to a peak or valley, start a new entry. Set up as a stack, most recent entry is element 1, older entries have higher numbers. }
OscVal = OSC; NewPeak=false; NewVal=false;
{look for peak in Stochastic}
If barnumber > 2 and LastH > 0 then begin if OscH[1]-OscVal>=OscDif and OscH[1] - OscVal[2]>=OscDif then begin {found new peak} LastH = Minlist(LastH+1,MaxNum); NewPeak = true; for loop = LastH downto 2 begin OscH[Loop] = OscH[Loop-1]; OscHBar[Loop] = OscHBar[Loop-1]; end; end; end ELSE LastH = 1; {first entry}
{Capture and *save values}
OscH[1] = OscVal; OscHBar[1] = BarNumber;
{Look for valley in Stochastic}
if barnumber>2 and LastL > 0 then begin if NewPeak = false then begin if OscVal-OscL[1]>=OscDif and OscVal[2]-OscL[1]>=OscDif then begin {Found new valley} LastL = minlist(LastL+1,MaxNum); NewVal = true; For Loop = LastL downto 2 begin OscL[Loop] = OscL[Loop-1]; OscLBar[Loop] = OscLBar[Loop-1]; end; end; end; end ELSE LastL = 1; {first entry}
{Capture and save values} OscL[1] = OscVal; OscLBar[1] = BarNumber;
{Look for the first part of a sell divergence, the second stochastic peak is Lower than the previous stochastic peak by one full % point. Also, the first peak has to be >= the MinPeak value. Take the first occurance}
if NewPeak or barnumber-OscHBar[2] <= MaxOfset then begin FoundH = 0; for loop = 3 to lastH begin if FoundH=0 and (OscH[loop]-OscH[2]>=1) and OscHBar[2]-OscHBar[loop]>=MinGap and OscH[loop]>=MinPeak and OscH[2]>=MinPeak2 then FoundH = Loop; { negative slope} end;
if foundH > 0 then begin {FOUND FIRST PART OF SELL DIVERGENCE, CHECK PRICE BARS FOR MATCH} StrtBar = RB_CheckPeak(OscHBar[FoundH],MaxOfset,PrcDif); EndBar = RB_CheckPeak(OscHBar[2],MaxOfset,PrcDif); StrtVal = high[StrtBar]; EndVal = high[EndBar]; If StrtBar > 0 and EndBar > 0 and EndVal >= StrtVal and StrtBar - EndBar >= MinGap then begin {Found match, have true Divergence}
{ if OscHBar[2]<>LastPeak then } if EndVal = StrtVal then IncVal = 0 else IncVal = (EndVal-StrtVal)/(StrtBar-EndBar); value2 = 0; for value1 = StrtBar downto EndBar begin Plot1[value1](StrtVal+value2*IncVal,"SDIVERG"); value2 = value2 + 1; end;
{ LastPeak = OscHBar[2]; }
end; end; end;
{Look for the first part of a buy divergence, the second stochastic valley is Higher than the previous stochastic Valley by one full % point. Also, the first valley has to be<=the MaxVal value.Take the first occurance}
if NewVal or barnumber-OscLBar[2] <= MaxOfset then begin FoundL = 0; for loop = 3 to lastL begin if FoundL=0 and (OscL[2]-OscL[Loop]>=1) and OscLbar[2]-OscLBar[loop]>=MinGap and OscL[loop] <= MaxVal and OscL[2]<=MaxVal2 then FoundL = Loop; { positive slope} end; if foundL > 0 then begin {FOUND FIRST PART OF BUY DIVERGENCE, CHECK PRICE BARS FOR MATCH}
EndBar = RB_CheckValley(OscLBar[2],MaxOfset,PrcDif); StrtBar = RB_CheckValley(OscLBar[FoundL],MaxOfset,PrcDif); StrtVal = low[StrtBar]; EndVal = low[EndBar]; if StrtBar>0 and EndBar>0 and EndVal <= StrtVal and StrtBar - EndBar >= MinGap then begin {Found match, have true Divergence} { if OscLBar[2]<>LastVal then }
if EndVal = StrtVal then IncVal = 0 else IncVal = (StrtVal-EndVal)/(StrtBar-EndBar); value2 = 0; for value1 = StrtBar downto EndBar begin Plot2[value1](StrtVal-value2*IncVal,"BDIVERG"); value2 = value2 +1; end; { LastVal = OscLBar[2]; }
end; end; end;
{**************************************}
Funktion: RB_Checkpeak
{****************************************}
InputskNmbr(NumericSimple),MaxOfset(NumericSimple), PrcDif(numericsimple); Vars:Last(0),First(0),TstVal(0),PrevVal(0),NextVal(0);
{PkNmbr - Barnumber of peak in stochastic that you are checking against MaxOfset - Number of bars away from this PkNmbr that is allowable and still considered a valid match PrcDif - Minimun number of points between highs or lows of bars in order to be greater or less than.
Will return the offset bar number (bars back from current bar) of the peak bar or will return a Zero if there is no matching peak}
First = Barnumber - Minlist(PkNmbr+MaxOfset,Barnumber-1); Last = Barnumber - Maxlist(PkNmbr-MaxOfset,2);
RB_CheckPeak = 0; if First<=maxbarsback and Last<=maxbarsback then begin For Value1 = First to Last begin TstVal = High[value1]; PrevVal = High[Value1+1]; NextVal = High[Value1-1];
{ {***Check For Equal Bars***} value2 = 2; while TstVal = PrevVal begin PrevVal = High[value1+value2]; value2 = value2+1; end; value2 = 2; while TstVal = NextVal and value1-value2>= 0 begin NextVal = High[value1-value2]; value2=value2+1; end; }
if TstVal-NextVal>=PrcDif points and TstVal-PrevVal>=PrcDif points then RB_CheckPeak =value1; end; end;
{**************************************}
Funktion: RB_Checkvalley {***************************************}
InputskNmbr(NumericSimple),MaxOfset(NumericSimple), PrcDif(numericsimple); Vars:Last(0),First(0),TstVal(0),NextVal(0),PrevVal(0);
{PkNmbr - Barnumber of peak in stochastic that you are checking against MaxOfset - Number of bars away from this PkNmbr that is allowable and still considered a valid match PrcDif - Minimun number of points between highs or lows of bars in order to be greater or less than.
Will return the offset bar number (bars back from current bar) of the valley bar or will return a Zero if there is no matching valley}
First = Barnumber - Minlist(PkNmbr+MaxOfset,Barnumber-1); Last = Barnumber - Maxlist(PkNmbr-MaxOfset,2); RB_CheckValley = 0;
if First<=maxbarsback and Last<=maxbarsback then begin For Value1 = First to Last begin TstVal = Low[value1]; PrevVal = Low[Value1+1]; NextVal = Low[Value1-1];
{ {***Check For Equal Bars***} value2 = 2; while TstVal = PrevVal begin PrevVal = Low[value1+value2]; value2 = value2+1; end; value2 = 2; while TstVal = NextVal and value1-value2>= 0 begin NextVal = Low[value1-value2]; value2=value2+1; end; }
if NextVal-TstVal>=PrcDif points and PrevVal-TstVal>=PrcDif points then RB_CheckValley = value1; end; end;
{***************************************}
grüsse
micha
|