Hello All,
i have looked and looked and looked for a correct divergence code but have always come back with 2 different sets which are available at many places. Unconvinced, i set out to try to make that code my self and looked up for the things required to assemble such code. Am putting down those requisites here and request people with coding knowledge to lend me a helping had to formulate that code.
Elements :-
1. Low array of price
2. High array of Price
3. Rsi(x)
4. ROC(element(1 or 2 or 3), n bars)
5. n = current bar(0) upto 50 bars (lookback period for ROC)
6. ext = scan the most recent 200 bars
Logic :-
Bullish Divergence : When rate of change of Low of nth bars {ROC(L,n)} is -ve and ROC(RSI(x),n) is +ve; plot line from L,nth bar to L,nth bar.
Bearish Divergence : When rate of change of High of n bars {ROC(H,n)} is +ve and ROC(RSI(x),n) is -ve; plot line from H,nth bar to H,nth bar.
The above code will loop for every bar from current 0 bar upto user defined bars(200), so as to mark any divergance which may have happened in the past as well. Thus if divergence is being looked for the current bar, the same will be looked for the past as well.
My attempt to code this logic is very raw and am not getting the desired output. Attaching the code below.
Any support is highly appreciated!
TIA,
i have looked and looked and looked for a correct divergence code but have always come back with 2 different sets which are available at many places. Unconvinced, i set out to try to make that code my self and looked up for the things required to assemble such code. Am putting down those requisites here and request people with coding knowledge to lend me a helping had to formulate that code.
Elements :-
1. Low array of price
2. High array of Price
3. Rsi(x)
4. ROC(element(1 or 2 or 3), n bars)
5. n = current bar(0) upto 50 bars (lookback period for ROC)
6. ext = scan the most recent 200 bars
Logic :-
Bullish Divergence : When rate of change of Low of nth bars {ROC(L,n)} is -ve and ROC(RSI(x),n) is +ve; plot line from L,nth bar to L,nth bar.
Bearish Divergence : When rate of change of High of n bars {ROC(H,n)} is +ve and ROC(RSI(x),n) is -ve; plot line from H,nth bar to H,nth bar.
The above code will loop for every bar from current 0 bar upto user defined bars(200), so as to mark any divergance which may have happened in the past as well. Thus if divergence is being looked for the current bar, the same will be looked for the past as well.
My attempt to code this logic is very raw and am not getting the desired output. Attaching the code below.
Any support is highly appreciated!
TIA,
Code:
_SECTION_BEGIN("Plot Div");
LB = Param("Look Back Period",50,2,500,1);
ext = Param("Look Back Period",200,2,500,1);
PlotOHLC( Open, High, Low, Close, "", colorBlack, styleCandle | styleThick );
RS=RSI(14);
for (i=1;i<(LB+1);i++)
{
for(j=0;j<ext;j++)
{
if(j>0)
{
rocl = ROC(Ref(L,-j),i);
roch = ROC(Ref(H,-j),i);
rocr = ROC(Ref(RS,-j),i);
IIf(rocr>0 AND rocl<0, PlotShapes(shapesmallCircle,colorTeal,0,Ref(L,-j),-10),0);
IIf(rocr<0 AND roch>0, PlotShapes(shapesmallCircle,colorPink,0,Ref(H,-j),10),0);
}
if(j==0)
{
rocl = ROC(L,i);
roch = ROC(H,i);
rocr = ROC(RS,i);
IIf(rocr>0 AND rocl<0, PlotShapes(shapesmallCircle,colorTeal,0,L,-10),0);
IIf(rocr<0 AND roch>0, PlotShapes(shapesmallCircle,colorPink,0,H,10),0);
}
}
}
_SECTION_END();