Is it possible to code Supertrend without the for loop? I tried but as I am not proficient with AFL I was able to get only so far. If you have Supertrend without the for loop then please share. I have posted both versions (with and without loop) below. If some person with proficiency in AFL can help me further then it will be highly appreciated. My objective is to make Supertrend run faster.
Supertrend with LOOP
Supertrend without LOOP
Supertrend with LOOP
Code:
_SECTION_BEGIN("SuperTrend");
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(ParamColor("bkcolor",colorWhite));
GfxSetBkMode(0);
GfxSetOverlayMode(1);
Factor=3;//Param("Factor",3,1,10,0.1);
Pd=7;//Param("ATR Periods",7,1,100,1);
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;
for (i = 1; i <BarCount-1; i++) {
TrendUp[i] = Null;
TrendDown[i] = Null;
trend[i]=1;
if (H[i]>Up[i-1]) {
trend[i]=1;
if (trend[i-1] == -1) changeOfTrend = 1;
}
else if (L[i]<Dn[i-1]) {
trend[i]=-1;
if (trend[i-1] == 1) changeOfTrend = 1;
}
else if (trend[i-1]==1) {
trend[i]=1;
changeOfTrend = 0;
}
else if (trend[i-1]==-1) {
trend[i]=-1;
changeOfTrend = 0;
}
if (trend[i]<0 && trend[i-1]>0) {
flag=1;
}
else {
flag=0;
}
if (trend[i]>0 && trend[i-1]<0) {
flagh=1;
}
else {
flagh=0;
}
if (trend[i]>0 && Dn[i]<Dn[i-1]){
Dn[i]=Dn[i-1];
}
if (trend[i]<0 && Up[i]>Up[i-1])
{ Up[i]=Up[i-1];
}
if (flag==1)
{ Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
}
if (flagh==1)
{ Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
}
if (trend[i]==1) {
TrendUp[i]=Dn[i];
if (changeOfTrend == 1) {
TrendUp[i-1] = TrendDown[i-1];
changeOfTrend = 0;
}
}
else if (trend[i]==-1) {
TrendDown[i]=Up[i];
if (changeOfTrend == 1) {
TrendDown[i-1] = TrendUp[i-1];
changeOfTrend = 0;
}
}
}
BuyTrend = trend==1;
SellTrend=trend==-1;
BuyTrend=ExRem(BuyTrend,SellTrend);
SellTrend=ExRem(SellTrend,BuyTrend);
ShortTrend=SellTrend;
CoverTrend=BuyTrend;
Plot(TrendUp,"Trend",colorGreen,styleNoTitle+styleNoLabel);
Plot(TrendDown,"Down",colorRed,styleNoTitle+styleNoLabel);
PlotShapes(IIf(BuyTrend , shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(BuyTrend , shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(BuyTrend , shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(ShortTrend, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(ShortTrend, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(ShortTrend, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat(" - Open %g, Hi %g, Lo %g, Close %g (%.1f%%) ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
Code:
_SECTION_BEGIN("SuperTrend");
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(ParamColor("bkcolor",colorWhite));
GfxSetBkMode(0);
GfxSetOverlayMode(1);
Factor=3;//Param("Factor",3,1,10,0.1);
Pd=7;//Param("ATR Periods",7,1,100,1);
iATR=ATR(Pd);
Up=(H+L)/2+(Factor*iATR);
Dn=(H+L)/2-(Factor*iATR);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend[0]=0;
flag=flagh=0;
_TRACE("barindex()="+BarIndex());
trend = IIf( H>Ref(Up,-1) ,1, IIf( L<Ref(Dn,-1), -1, trend ) );
changeOfTrend = IIf(trend!=Ref(trend, -1), 1, changeOfTrend );
flag=IIf(trend<0 AND Ref(trend,-1)>0, 1, 0);
flagh=IIf(trend>0 AND Ref(trend,-1)<0, 1, 0);
Up=IIf(trend<0 AND Up>Ref(Up,-1), Ref(Up,-1), Up);
Dn=IIf(trend>0 AND Dn<Ref(Dn,-1), Ref(Dn,-1), Dn);
Up=IIf(flag==1,(H+L)/2+ Factor*iATR, Up);
Dn=IIf(flagh ==1,(H+L)/2- Factor*iATR, Dn);
TrendUp=IIf(trend==1, Dn, TrendUp);
TrendDown=IIf(trend==-1, Up, TrendDown);
TrendUpPrevious=Ref(TrendUp,-1);
TrendUpPrevious=IIf(trend==1 AND ChangeOfTrend==1, Ref(TrendDown,-1), TrendUpPrevious);
TrendDownPrevious=Ref(TrendDown,-1);
TrendDownPrevious=IIf(trend==-1 AND ChangeOfTrend==1, Ref(TrendUp,-1), TrendDownPrevious);
BuyTrend = trend==1;
SellTrend=trend==-1;
BuyTrend=ExRem(BuyTrend,SellTrend);
SellTrend=ExRem(SellTrend,BuyTrend);
ShortTrend=SellTrend;
CoverTrend=BuyTrend;
Plot(TrendUpPrevious,"Trend",colorGreen,styleNoTitle+styleNoLabel);
Plot(TrendDownPrevious,"Down",colorRed,styleNoTitle+styleNoLabel);
PlotShapes(IIf(BuyTrend , shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(BuyTrend , shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(BuyTrend , shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(ShortTrend, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(ShortTrend, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(ShortTrend, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat(" - Open %g, Hi %g, Lo %g, Close %g (%.1f%%) ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
Supertrend without LOOP
Aucun commentaire:
Enregistrer un commentaire