Inputs:
v1(NumericSeries), //price,close or high low
v2(NumericSeries), //indicate OSC of MACD,J of KDJ
Len1( numericsimple ), //max calculate bars
Len2( numericsimple ); //min deviate range
Vars :
ni(Len2),
deviate_L(False),
deviate_S(False);
ni=Len2;
deviate_L=False;
deviate_S=False;
Switch(v2[0]>0)
begin
case true:
while deviate_L=false and ni<=Len1 and v1[0] >= Highest(v1,ni)
begin
deviate_L = v1[0]>v1[ni] and v2[0]<v2[ni] and v2[ni]>0;
ni=ni+1;
end;
case false:
while deviate_S=false and ni<=Len1 and v1[0] <= lowest(v1,ni)
begin
deviate_S = v1[0]<v1[ni] and v2[0]>v2[ni] and v2[ni]<0;
ni=ni+1;
end;
end; //end of switch
if ni < Len1 and deviate_L=True then _deviate=ni
else if ni < Len1 and deviate_S=True then _deviate=ni
else _deviate=-1;