[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Divergence Indicator



PureBytes Links

Trading Reference Links

> Good evening,
>
> I think this question was 10x on the list but maybe one kind soul will
> answer.
> Is there any kind of indicator which will show divergence between
price and
> maybe RSI or MACD histo...?
> Any hint would be appreciate even if its total useless
>
> Thanks Laura
>

Laura,

a pretty good one i came across some time ago.

regards

michael

{*******************************************************************}


Input:OSC(slowkclassic(5,
3)),Mingap(3),MaxOfset(3),OscDif(.01),PrcDif(1),MinPeak(30),
         MinPeak2(30),MaxVal(70),MaxVal2(70),MaxNum(10);

Array:OscH[51](0),OscHBar[51](0),OscL[51](0),OscLBar[51](0);

Var:OscVal(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;

{******************************************************************}