Simple Coding Help - No Promise.

cloudTrader

Well-Known Member
Can someone please share a good Trailing Stop loss AFL ? I got hold of one through WST but somehow I feel that there may be better versions in terms of flexibility to change parameters.
 
Dear friend houmansat,
for rsi divergence number 1 will be shown on chart
for stoch divergence number 2 will be shown on chart
and for mfi divergence number 3 will be shown on chart
Note --- for calculation of MFI, volume is needed :happy: so if u dont have volume data , MFI wont be shown :happy:
here is the code :happy:
Code:
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 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Buy=Sell=Cover=Short=0;

mfip=Param("MFI period",14,5,100,1);
rsip=Param("RSI period",14,5,100,1);
stochp=Param("Stoch period",14,5,100,1);

n=Param("Divergence Distance RSI",20,0,100,1);
Buy1=Sell1=0;
Var = Zig(RSI(rsip), n);
t= Trough(RSI(rsip), n, 1);
p= Peak(RSI(rsip), n, 1);

for ( i=0; i<BarCount; i++)
{
if (i ==0)
{
x[0] =Var[0];
price[0] = C[0];
j=0;
}

if(Var[i] == p[i])
{
j++;
x[j] =Var[i];
price[j] =C[i];
if(x[j] <x[j-1] && price[j-1]< price[j])
Sell1[i] =1;
}
}

for ( i=0; i<BarCount; i++)
{
if (i ==0)
{
x[0] =Var[0];
price[0] = C[0];
j=0;
}
if(Var[i] == t[i])
{
j++;
x[j] =Var[i];
price[j] =C[i];
if(x[j] >x[j-1] && price[j]<price[j-1])
Buy1[i] =1;
}
}

PlotShapes ( IIf(Sell1, shapeDigit1, shapeNone), colorRed,0,H,12);
PlotShapes( IIf(Buy1, shapeDigit1, shapeNone), colorBrightGreen,0,L,-12);

ST33=StochD(stochp);
TR1=LLVBars(ST33,4);
TR2=IIf(ST33<30 AND TR1>0 AND Ref(TR1,-1)==0,Ref(ST33,-1),0);
TRC=IIf(TR2>0,C,0);
vs=ValueWhen(tr2, Ref(st33,-1), 1);
dvs=vs-Ref(vs,-1);
vc=ValueWhen(trc, LLV(C,3), 1);
dvc=vc-Ref(vc,-1);
diver=IIf(dvs>0 AND dvc<0,30,0);
DAS=BarsSince(Ref(TR2,-1)>0);
DD=IIf(DAS<20 AND C>=Ref(C,-1),DIVER,0);
Buy2=DD>0 ;

ST33=StochD(stochp);
TR11=HHVBars(ST33,4);
TR21=IIf(ST33>70 AND TR11>0 AND Ref(TR11,-1)==0,Ref(ST33,-1),0);
TRC1=IIf(TR21>0,C,0);
vs1=ValueWhen(tr21, Ref(st33,-1), 1);
dvs1=vs1-Ref(vs1,-1);
vc1=ValueWhen(trc1, HHV(H,3), 1);
dvc1=vc1-Ref(vc1,-1);
diver1=IIf(dvs1<0 AND dvc1>0,90,0);
DAS1=BarsSince(Ref(TR21,-1)>0);
ddd1=IIf(DAS1<20 AND C<Ref(C,-1),DIVER1,0);
Sell2=ddd1==90;

PlotShapes( IIf(Buy2, shapeDigit2, shapeNone), colorBrightGreen,0,L,-12);
PlotShapes ( IIf(Sell2, shapeDigit2, shapeNone), colorRed,0,H,12);

f=Param("Divergence Distance MFI",20,0,100,1);
Buy3=Sell3=0;
Varf = Zig(MFI(mfip), f);
tf= Trough(MFI(mfip), f, 1);
pf= Peak(MFI(mfip), f, 1);

for ( i=0; i<BarCount; i++)
{
if (i ==0)
{
x[0] =Varf[0];
price[0] = C[0];
j=0;
}

if(Varf[i] == pf[i])
{
j++;
x[j] =Varf[i];
price[j] =C[i];
if(x[j] <x[j-1] && price[j-1]< price[j])
Sell3[i] =1;
}
}

for ( i=0; i<BarCount; i++)
{
if (i ==0)
{
x[0] =Varf[0];
price[0] = C[0];
j=0;
}
if(Varf[i] == tf[i])
{
j++;
x[j] =Varf[i];
price[j] =C[i];
if(x[j] >x[j-1] && price[j]<price[j-1])
Buy3[i] =1;
}
}

PlotShapes ( IIf(Sell3, shapeDigit3, shapeNone), colorRed,0,H,12);
PlotShapes( IIf(Buy3, shapeDigit3, shapeNone), colorBrightGreen,0,L,-12);
:happy:
Hi dear my friend ROMEO
Will you please add scan/explorer to your code.thanks
 

Romeo1998

Well-Known Member
Dear friend houmansat,
modified the earlier code little bit to look good on chart :happy:
and added code to scan,explore n backtest too :happy:
here is the code :happy:
Code:
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 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Buy=Sell=Cover=Short=0;

mfip=Param("MFI period",14,5,100,1);
rsip=Param("RSI period",14,5,100,1);
stochp=Param("Stoch period",14,5,100,1);

n=Param("Divergence Distance RSI",20,0,100,1);
Buy1=Sell1=0;
Var = Zig(RSI(rsip), n);
t= Trough(RSI(rsip), n, 1);
p= Peak(RSI(rsip), n, 1);

for ( i=0; i<BarCount; i++)
{
if (i ==0)
{
x[0] =Var[0];
price[0] = C[0];
j=0;
}

if(Var[i] == p[i])
{
j++;
x[j] =Var[i];
price[j] =C[i];
if(x[j] <x[j-1] && price[j-1]< price[j])
Sell1[i] =1;
}
}

for ( i=0; i<BarCount; i++)
{
if (i ==0)
{
x[0] =Var[0];
price[0] = C[0];
j=0;
}
if(Var[i] == t[i])
{
j++;
x[j] =Var[i];
price[j] =C[i];
if(x[j] >x[j-1] && price[j]<price[j-1])
Buy1[i] =1;
}
}

PlotShapes ( IIf(Sell1, shapeDigit1, shapeNone), colorRed,0,H,10);
PlotShapes( IIf(Buy1, shapeDigit1, shapeNone), colorBrightGreen,0,L,-10);

ST33=StochD(stochp);
TR1=LLVBars(ST33,4);
TR2=IIf(ST33<30 AND TR1>0 AND Ref(TR1,-1)==0,Ref(ST33,-1),0);
TRC=IIf(TR2>0,C,0);
vs=ValueWhen(tr2, Ref(st33,-1), 1);
dvs=vs-Ref(vs,-1);
vc=ValueWhen(trc, LLV(C,3), 1);
dvc=vc-Ref(vc,-1);
diver=IIf(dvs>0 AND dvc<0,30,0);
DAS=BarsSince(Ref(TR2,-1)>0);
DD=IIf(DAS<20 AND C>=Ref(C,-1),DIVER,0);
Buy2=DD>0 ;

ST33=StochD(stochp);
TR11=HHVBars(ST33,4);
TR21=IIf(ST33>70 AND TR11>0 AND Ref(TR11,-1)==0,Ref(ST33,-1),0);
TRC1=IIf(TR21>0,C,0);
vs1=ValueWhen(tr21, Ref(st33,-1), 1);
dvs1=vs1-Ref(vs1,-1);
vc1=ValueWhen(trc1, HHV(H,3), 1);
dvc1=vc1-Ref(vc1,-1);
diver1=IIf(dvs1<0 AND dvc1>0,90,0);
DAS1=BarsSince(Ref(TR21,-1)>0);
ddd1=IIf(DAS1<20 AND C<Ref(C,-1),DIVER1,0);
Sell2=ddd1==90;

PlotShapes( IIf(Buy2, shapeDigit2, shapeNone), colorBrightGreen,0,L,-20);
PlotShapes ( IIf(Sell2, shapeDigit2, shapeNone), colorRed,0,H,20);

f=Param("Divergence Distance MFI",20,0,100,1);
Buy3=Sell3=0;
Varf = Zig(MFI(mfip), f);
tf= Trough(MFI(mfip), f, 1);
pf= Peak(MFI(mfip), f, 1);

for ( i=0; i<BarCount; i++)
{
if (i ==0)
{
x[0] =Varf[0];
price[0] = C[0];
j=0;
}

if(Varf[i] == pf[i])
{
j++;
x[j] =Varf[i];
price[j] =C[i];
if(x[j] <x[j-1] && price[j-1]< price[j])
Sell3[i] =1;
}
}

for ( i=0; i<BarCount; i++)
{
if (i ==0)
{
x[0] =Varf[0];
price[0] = C[0];
j=0;
}
if(Varf[i] == tf[i])
{
j++;
x[j] =Varf[i];
price[j] =C[i];
if(x[j] >x[j-1] && price[j]<price[j-1])
Buy3[i] =1;
}
}

PlotShapes ( IIf(Sell3, shapeDigit3, shapeNone), colorRed,0,H,30);
PlotShapes( IIf(Buy3, shapeDigit3, shapeNone), colorBrightGreen,0,L,-30);

Buy = Buy1 OR Buy2 OR Buy3;
Sell = Sell1 OR Sell2 OR Sell3;
 
Filter= Buy OR Sell;
AddColumn(C,"Close");
AddColumn( IIf(Buy1,1,null) ,"RSI Buy Divergence",1,colorBlack,IIf(Buy1,colorGreen,colorBlack));
AddColumn( IIf(Sell1,1,null) ,"RSI Sell Divergence",1,colorBlack,IIf(Sell1,colorRed,colorBlack));
AddColumn( IIf(Buy2,1,null) ,"Stoch Buy Divergence",1,colorBlack,IIf(Buy2,colorGreen,colorBlack));
AddColumn( IIf(Sell2,1,null) ,"Stoch Sell Divergence",1,colorBlack,IIf(Sell2,colorRed,colorBlack));
AddColumn( IIf(Buy3,1,null) ,"MFI Buy Divergence",1,colorBlack,IIf(Buy3,colorGreen,colorBlack));
AddColumn( IIf(Sell3,1,null) ,"MFI Sell Divergence",1,colorBlack,IIf(Sell3,colorRed,colorBlack));
:happy:
 
Last edited:
YOU ARE THE KING OF AFL ROMEO.I AM VERY LUCKY MAN THAT YOU ARE MY FRIEND. your helpful guides are so much useful and help me a lot and increase the quality of my trades.i appreciate you so much.
Iاhave Duplicate request. because i still need to add scan /explore to exact afl you know that afl . so i ask you again please add scan just this afl it could be very usefull for me if you add filter .i need this.thanks

Code:
_SECTION_BEGIN("RSI ");
SetChartOptions(0,0,chartGrid30|chartGrid70);
periods = Param( "Periods", 14, 1, 200, 1 );
a = RSI(periods);
Plot( a, _DEFAULT_NAME(), ParamColor( "Color", colorYellow ), ParamStyle("Style",styleThick)  );

/*RSI Trendlines and Wedges*/
G=0;// set G=1, 2, 3, ... to see previous Resistance trendlines
GG=0;// set G=1, 2, 3, ... to see previous Support trendlines
x = Cum(1);
per = Param("Sensitivity",1,1,100,1); // sensitivity calibration
s1=a;
s11=a;
pS = TroughBars( s1, per, 1 ) == 0;
endt= LastValue(ValueWhen( pS, x, 1+GG ));
startt=LastValue(ValueWhen( pS, x, 2+GG ));
dtS =endt-startt;
endS = LastValue(ValueWhen( pS, s1, 1+GG ) );
startS = LastValue( ValueWhen( pS, s1, 2+GG ));
aS = (endS-startS)/dtS;
bS = endS;
trendlineS = aS * ( x -endt ) + bS;// the Support trendline equation
Plot(IIf(x>startt-6 AND TRENDLINES>0 AND TRENDLINES<100,trendlineS,-1e10),"Support",IIf(as>0,5,4),8);
pR = PeakBars( s11, per, 1 ) == 0;
endt1= LastValue(ValueWhen( pR, x, 1+G ));
startt1=LastValue(ValueWhen( pR, x, 2+G ));
dtR =endt1-startt1;
endR = LastValue(ValueWhen( pR, s11, 1+G ) );
startR = LastValue( ValueWhen( pR, s11, 2 +G ));
aR = (endR-startR)/dtR;
bR = endR;
trendlineR = aR * ( x -endt1 ) + bR;// the Resistance trendline equation
Plot(IIf(x>startT1-6 AND TRENDLINER>0 AND TRENDLINER<100,trendlineR,-1e10),"Resistance",IIf(Ar>0,5,4),8);
Ascwedge=Ar>0.5 AND As>sqrt(2)*Ar AND trendlineR>trendlineS;
Descwedge= As<-0.5 AND As>Ar/sqrt(2) AND trendlineR>trendlineS;
Filter=1;
AddColumn(Ascwedge,"AscWedge",1.0);
AddColumn(DescWedge,"DesWedge",1.0);
AddColumn(Ascwedge AND trendlineS>50 AND a>50,"Probable Bearish Breakout",1.0);
AddColumn(Descwedge AND trendlineR<50 AND a<50,"Probable Bullish Breakout",1.0);
/* END of code */
i have this filter but i dont know how should add it.buy and sell need to define
Filter=Buy OR Sell;

AddColumn(IIf(Buy,BuyPrice,Null)," Buy Signal ", 6.2,1.2,colorGreen);
AddColumn(IIf(Sell,SellPrice,Null)," Sell Signal ",6.2,1.2,colorOrange);
 
Last edited:

Similar threads