Simple Coding Help - No Promise.

McNish

Active Member
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, :)

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();
 

McNish

Active Member
Some people in the previous post were looking for MTF function. Here is one that i coded. Soft of super trend.
HTF (Higher time frame) id D1
LTF (Lower time frame) is m15
open TF is 1minute.

Code:
_SECTION_BEGIN("Swing Ribbon MTF");
no=Param( "Swing", 12, 1, 55 );
SST = ParamToggle("Show 1Min ST","show|hide",1);

tsl_col=ParamColor( "Color", colorLightGrey );
tslD_col=ParamColor( "Color", colorLightGrey );
tslm_col=ParamColor( "Color", colorGrey50 );

if(Interval(0) <= 900)
{
TimeFrameSet(in15Minute);
resl=HHV(H,no);
supl=LLV(L,no);
avdl=IIf(C>Ref(resl,-1),1,IIf(C<Ref(supl,-1),-1,0));
avnl=ValueWhen(avdl!=0,avdl,1);
tsll=IIf(avnl==1,supl,resl);

Ribbon1=IIf(  (C) >(tsll)  ,colorTeal, IIf(( tsll )>( C ), colorOrange,colorYellow));
TimeFrameRestore();

Plot(-0.5, "Ribbon1", TimeFrameExpand(Ribbon1, in15Minute, expandFirst), styleOwnScale| styleArea| styleNoLabel,-100,2,0,0,1);
//Plot(tsll, _DEFAULT_NAME(), tsl_col, styleStaircase|styleNoRescale);
}

if(Interval(0) <= 3600)
{
TimeFrameSet(inHourly);
resD=HHV(H,no);
supD=LLV(L,no);
avdD=IIf(C>Ref(resD,-1),1,IIf(C<Ref(supD,-1),-1,0));
avnD=ValueWhen(avdD!=0,avdD,1);
tslD=IIf(avnD==1,supD,resD);

Ribbon2=IIf(  (C) >(tslD)  ,colorTeal, IIf(( tslD )>( C ), colorOrange,colorYellow));
TimeFrameRestore();

Plot(-1, "Ribbon2", TimeFrameExpand(Ribbon2, inHourly,expandFirst), styleOwnScale| styleArea| styleNoLabel,-100,-1,0,0,1);
//Plot(TimeFrameExpand(tslD, inDaily),_DEFAULT_NAME(), tslD_col, styleStaircase|styleNoRescale);
}

if(Interval(0) == 60 AND SST)
{
TimeFrameSet(in1Minute);
resm=HHV(H,no);
supm=LLV(L,no);
avdm=IIf(C>Ref(resm,-1),1,IIf(C<Ref(supm,-1),-1,0));
avnm=ValueWhen(avdm!=0,avdm,1);
tslm=IIf(avnm==1,supm,resm);

Ribbonm=IIf((C) >(tslm)  ,colorTeal, IIf(( tslm )>( C ), colorOrange,colorYellow));
TimeFrameRestore();

//Plot(-1, "Ribbonm", TimeFrameExpand(Ribbon2, inHourly,expandFirst), styleOwnScale| styleArea| styleNoLabel,-100,-1,0,0,1);
Plot(TimeFrameExpand(tslm, in1Minute,expandFirst),_DEFAULT_NAME(), tsld_col, styleStaircase|styleNoRescale);
}

if(Interval(0) >= 86400)
{
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);
Ribbon3=IIf(  (C) >(tsl)  ,colorTeal, IIf(( tsl )>( C ), colorOrange,colorYellow));

Plot(-1, "Ribbon3", Ribbon3, styleOwnScale| styleArea| styleNoLabel,-100,-1,0,0,1);

//Plot( TimeFrameExpand(tslD, inDaily),_DEFAULT_NAME(), tslD_col, styleStaircase|styleNoRescale);
}

_SECTION_END();
 

josh1

Well-Known Member
Sir Will this do!
Code:
_SECTION_BEGIN("Candle Color @ 14:01 ");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

Bar1401 = TimeNum() == 140159;

cc=    IIF(C>O,IIf(Bar1401,colorYellow,colorLime),
    IIF(C<O,IIf(Bar1401,colorYellow,colorRed),Null));

Plot(C,"Close",Cc,styleCandle | ParamStyle("Style") | GetPriceStyle() ,0,0,0,0);

_SECTION_END();
Thanks. Changed the code a bit and it works well.

Code:
_SECTION_BEGIN("Candle Color @ 14:01 ");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

Bar1401 = TimeNum() >= 140100 AND TimeNum() <= 140500;

setbarfillcolor(IIf(Bar1401,colorYellow,colorLime));

// cc=    IIF(C>O,IIf(Bar1401,colorYellow,colorLime),    IIF(C<O,IIf(Bar1401,colorYellow,colorRed),Null));

Plot(C,"Close",Cc,styleCandle | ParamStyle("Style") | GetPriceStyle() ,0,0,0,0);

_SECTION_END();
 

travi

Well-Known Member
Thanks. Changed the code a bit and it works well.

Code:
_SECTION_BEGIN("Candle Color @ 14:01 ");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

Bar1401 = TimeNum() >= 140100 AND TimeNum() <= 140500;

setbarfillcolor(IIf(Bar1401,colorYellow,colorLime));

// cc=    IIF(C>O,IIf(Bar1401,colorYellow,colorLime),    IIF(C<O,IIf(Bar1401,colorYellow,colorRed),Null));

Plot(C,"Close",Cc,styleCandle | ParamStyle("Style") | GetPriceStyle() ,0,0,0,0);

_SECTION_END();


Code:
setbarfillcolor(IIf(Bar1401,colorYellow,colorLime));
colorLime should be colorDefault.
Haven't tested, but that will account more appropriately for down bars if its of a different colour
unless the next line Plot(C..... styleCandle flag forces an override of color as set I preference.

Nevertheless, best coding practices :)
 
hi,
below is the afl for macd histogram. when i try to plot macd and its signal line in it, the histogram compressed.
i want histogram to be of same height ( as it is right now) and also macd and its signal line in it ).
m1 is macd is the afl and
s1 is signal line.
can somebody make the necessary additions to make it complete.
thanks
Code:
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

ml = MACD(r1, r2);
sl = Signal(r1,r2,r3);
Hist = ml-sl;

MACUP = Hist > Ref(Hist,-1);
MACDN = Hist < Ref(Hist,-1);

BarColor  = IIf(MACUP,colorGreen,IIf(MACDN,colorRed,colorBlue));

//Plot( Hist, "MACD Histogram", ParamColor("Histogram color", colorBlack ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );
Plot( Hist, "MACD Histogram", BarColor  , styleHistogram |styleThick|styleNoLabel);
Plot(0, "0", colorBlack,styleNoLabel|styleNoTitle );
 
hi,
below is the afl for macd histogram. when i try to plot macd and its signal line in it, the histogram compressed.
i want histogram to be of same height ( as it is right now) and also macd and its signal line in it ).
m1 is macd is the afl and
s1 is signal line.
can somebody make the necessary additions to make it complete.
thanks
Code:
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

ml = MACD(r1, r2);
sl = Signal(r1,r2,r3);
Hist = ml-sl;

MACUP = Hist > Ref(Hist,-1);
MACDN = Hist < Ref(Hist,-1);

BarColor  = IIf(MACUP,colorGreen,IIf(MACDN,colorRed,colorBlue));

//Plot( Hist, "MACD Histogram", ParamColor("Histogram color", colorBlack ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );
Plot( Hist, "MACD Histogram", BarColor  , styleHistogram |styleThick|styleNoLabel);
Plot(0, "0", colorBlack,styleNoLabel|styleNoTitle );
Sir, if you add these line at the end of your code it should work.

Plot(sl,"Signal", colorRed,styleLine|stylenorescale);
Plot(ml,"MACD", colorgreen,styleLine|stylenorescale);
 

Taiki

Well-Known Member
hi,
below is the afl for macd histogram. when i try to plot macd and its signal line in it, the histogram compressed.
i want histogram to be of same height ( as it is right now) and also macd and its signal line in it ).
m1 is macd is the afl and
s1 is signal line.
can somebody make the necessary additions to make it complete.
thanks
Code:
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

ml = MACD(r1, r2);
sl = Signal(r1,r2,r3);
Hist = ml-sl;

MACUP = Hist > Ref(Hist,-1);
MACDN = Hist < Ref(Hist,-1);

BarColor  = IIf(MACUP,colorGreen,IIf(MACDN,colorRed,colorBlue));

//Plot( Hist, "MACD Histogram", ParamColor("Histogram color", colorBlack ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );
Plot( Hist, "MACD Histogram", BarColor  , styleHistogram |styleThick|styleNoLabel);
Plot(0, "0", colorBlack,styleNoLabel|styleNoTitle );
In your histogram style use styleOwnScale.

Plot( Hist, "MACD Histogram", BarColor , styleHistogram |styleThick|styleNoLabel|styleOwnScale);
Plot(sl,"Signal", colorRed,styleLine);
Plot(ml,"MACD", colorGreen,styleLine);

Best Regards
Taiki
 
Code:
nd = (Day() != Ref(Day(), -1));
tn = TimeNum();
CDR = IIf(ND == 0, Sum(V, 1 + BarsSince(ND)), V);
PDR1 = 0;
for (i = 1; i < BarCount; i++) {
PDR1[i] = PDR1[i-1];
if (i > 375) {
for(k = i - 1; k > 0; k--) {
if (tn[i] == tn[k]) {
PDR1[i] = CDR[k];
break;
k = 0;
}}}}

PDR2 = ValueWhen(tn == TimeNum(), CDR, 2);

AddColumn(CDR, "CDR");
AddColumn(PDR1, "PDR1");
AddColumn(PDR2, "PDR2");
Filter = 1;
This is one of the very old AFL written by me. It is for Intraday and it creates two columns of running volume (one of today) and (one for previous day) for Intraday purposes. When I first wrote this, I used two nested loops and it was working correctly, albeit slowly.

now when I tried to create loop-less version, it does not work correctly and I am stumped.
 

Romeo1998

Well-Known Member
Hello mastermind007 Sir
I am not getting the correct results but i think this might be helpful
Code:
nd = (Day() != Ref(Day(), -1));
tn = TimeNum();
CDR = IIf(ND == 0, Sum(V, 1 + BarsSince(ND)), V);
PDR1 = 0;
bi = BarIndex();
IIf(ValueWhen(bi>375,tn)==ValueWhen(bi<375,tn), pdr1 = ValueWhen(bi<375,cdr),0);
PDR2 = ValueWhen(tn == TimeNum(), CDR, 2);
AddColumn(CDR, "CDR");
AddColumn(PDR1, "PDR1");
AddColumn(PDR2, "PDR2");
Filter = 1;
:)
 
Hello,

Can we plot a 0 & 100 line in an indicator pan based on the Buy / Sell condition ?

for eg. If weekly close crossed monthly high then green line in indicator pan will be at 100
and whenever the weekly close crosses below previous month's low then the same line will turn in to red with the level 0 till the next buy signal

Thanks in advance
AR
 

Similar threads