Skip to main content

Super Trend (convert from metastock) for Amibroker (AFL)

vole_00 over 14 years ago Amibroker (AFL)

  • Rating:
    2 / 5 (Votes 3)
  • Tags:
    oscillator, amibroker

Link: http://www.inditraders.com/amibroker/5324-supertrend-oliver-seban.html

Code meta:
Factor:=Input(“Factor”,1.00,10.00,3.00);
Pd:=Input(“ATR Periods”,1,100,10);
Up:=MP);
Dn:=MP);
Td:=If(Cross(C,LLV),1,If(Cross(HHV,C ),-1,PREV));
Dnx:=If(Dn=HighestSince(1,Cross(Td,0),Dn),Dn,PREV) ;
Upx:=If(Up=LowestSince(1,Cross(0,Td),Up),Up,PREV);
ST:=If(Td=1,Dnx,If(Td=-1,Upx,PREV));
ST

I converted and uploaded to inditraders too.

Indicator / Formula

Copy & Paste Friendly
Factor=Param("Factor",3,1,10,1);
Pd=Param("ATR Periods",10,1,100,1);
Up=(H+L+C)/3+(Factor*ATR(Pd));
Dn=(H+L+C)/3-(Factor*ATR(Pd));
PREV1=1;
PREV1=Ref(IIf(Cross(C,LLV(Up,13)),1,IIf(Cross(HHV(Dn,13),C ),-1,PREV1)),-1);
Td=IIf(Cross(C,LLV(Up,13)),1,IIf(Cross(HHV(Dn,13),C ),-1,PREV1));

PREV2=Dn;
PREV2=Ref(IIf(Dn==HighestSince(Cross(Td,0),Dn,1),Dn,PREV2),-1);
Dnx=IIf(Dn==HighestSince(Cross(Td,0),Dn,1),Dn,PREV2) ;

PREV3=Up;
PREV3=Ref(IIf(Up==LowestSince(Cross(0,Td),Up,1),Up,PREV3),-1);
Upx=IIf(Up==LowestSince(Cross(0,Td),Up,1),Up,PREV3);

PREV4=Dnx;
PREV4=IIf(Td==1,Dnx,IIf(Td==-1,Upx,PREV4));
ST=IIf(Td==1,Dnx,IIf(Td==-1,Upx,PREV4));
Plot(ST,"SuperStrength",colorRed,styleLine);

10 comments

1. ole
over 14 years ago

The Metastock code above is not correct. Here is the IndiTrader code:

Factor:=Input("Factor",1.00,10.00,3.00);
Pd:=Input("ATR Periods",1,100,10);
Up:=MP()+(Factor*ATR(Pd));
Dn:=MP()-(Factor*ATR(Pd));
Td:=If(Cross(C,LLV(Up,13)),1,If(Cross(HHV(Dn,13),C ),-1,PREV));
Dnx:=If(Dn=HighestSince(1,Cross(Td,0),Dn),Dn,PREV) ;
Upx:=If(Up=LowestSince(1,Cross(0,Td),Up),Up,PREV);
ST:=If(Td=1,Dnx,If(Td=-1,Upx,PREV));
ST

If this code is plotted in Metastock it is different than that obtained in AmiBroker. So the AFL conversion is not correct. I’d post the charts but can’t figure out how.

over 14 years ago

Thank you very much.

How do you understand the PREV in metastock code?

Could you convert the following code too?

AMA Binary Wave

Periods:=Input("Time Periods",1,1000,10);
Direction:=CLOSE-Ref(CLOSE,-periods);
Volatility:=Sum(Abs(ROC(CLOSE,1,$)),periods);
ER:=Abs(Direction/Volatility);
FastSC:=2/(2+1);
SlowSC:=2/(30+1);
SSC:=ER*(FastSC-SlowSC)+SlowSC;
Constant:=Pwr(SSC,2);
AMA:=If(Cum(1)=periods+1,Ref(CLOSE,-1) + 
constant*(CLOSE-Ref(CLOSE,-1)),PREV + 
constant*(CLOSE-PREV));
FilterPercent:=Input("Filter Percentage",0,100,15)/100;
Filter:=FilterPercent*Std(AMA-Ref(AMA,-1),Periods);
AMALow:=If(AMA<Ref(AMA,-1),AMA,PREV);
AMAHigh:=If(AMA>Ref(AMA,-1),AMA,PREV);
If(AMA-AMALow>Filter,1{Buy Signal},
If(AMAHigh-AMA>Filter,-1{Sell Signal},0{No_Signal})) 
over 14 years ago

the supertrend code is translated as:

/* 
original code:
Factor:=Input("Factor",1.00,10.00,3.00);
Pd:=Input("ATR Periods",1,100,10);
Up:=MP()+(Factor*ATR(Pd));
Dn:=MP()-(Factor*ATR(Pd));
Td:=If(Cross(C,LLV(Up,13)),1,If(Cross(HHV(Dn,13),C ),-1,PREV));
Dnx:=If(Dn=HighestSince(1,Cross(Td,0),Dn),Dn,PREV) ;
Upx:=If(Up=LowestSince(1,Cross(0,Td),Up),Up,PREV);
ST:=If(Td=1,Dnx,If(Td=-1,Upx,PREV));
ST

translation in Amibroker AFL, Edward Pottasch, 9/29/2011
*/

Factor=Param("Factor",3,1,10,1);
Pd=Param("ATR Periods",10,1,100,1);
//Up=(H+L+C)/3+(Factor*ATR(Pd));
//Dn=(H+L+C)/3-(Factor*ATR(Pd));
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));

//Td:=If(Cross(C,LLV(Up,13)),1,If(Cross(HHV(Dn,13),C),-1,PREV));
aa=Cross(C,LLV(Up,13));
bb=Cross(HHV(Dn,13),C);
Td[0]=1;
for(i=1;i<BarCount;i++ )
{
	prev=Td[i-1];
	if(aa[i])
	{
		Td[i]=1;
	}
	else if(bb[i])
	{
		Td[i]=-1;
	}
	else
	{
		Td[i]=prev;
	}
}
//Dnx:=If(Dn=HighestSince(1,Cross(Td,0),Dn),Dn,PREV);
aa=Dn==HighestSince(Cross(Td,0),Dn);
Dnx=Dn[0];
for(i=1;i<BarCount;i++ )
{
	prev=Dnx[i-1];
	if(aa[i])
	{
		Dnx[i]=Dn[i];
	}
	else
	{
		Dnx[i]=prev;
	}
}
//Upx:=If(Up=LowestSince(1,Cross(0,Td),Up),Up,PREV);
aa=Up==LowestSince(Cross(0,Td),Up);
Upx=Up[0];
for(i=1;i<BarCount;i++ )
{
	prev=Upx[i-1];
	if(aa[i])
	{
		Upx[i]=Up[i];
	}
	else
	{
		Upx[i]=prev;
	}
}
//ST:=If(Td=1,Dnx,If(Td=-1,Upx,PREV));
aa=Td==1;
bb=Td==-1;
ST[0]=1;
for(i=1;i<BarCount;i++ )
{
	prev=ST[i-1];
	if(aa[i])
	{
		ST[i]=Dnx[i];
	}
	else if(bb[i])
	{
		ST[i]=Upx[i];
	}
	else
	{
		ST[i]=prev;
	}
}
SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0,0)));
SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
Plot(ST,"\nUP",colorWhite,styleDashed);
over 14 years ago

the other code is translated as:

Metastock code:

/* 
Periods := Input("Time Periods",1,1000, 10); 
Direction := CLOSE - Ref(Close,-periods);
Volatility := Sum(Abs(ROC(CLOSE,1,$)),periods);
ER := Abs(Direction/Volatility);
FastSC := 2/(2 + 1);
SlowSC := 2/(30 + 1);
SSC := ER * (FastSC - SlowSC) + SlowSC;
Constant := Pwr(SSC,2);
AMA := If(Cum(1) = periods +1, ref(Close,-1) + constant * (CLOSE - ref(Close,-1)),Prev + constant * (CLOSE - PREV));
FilterPercent := Input("Filter Percentage", 0,100,15)/100;
Filter := FilterPercent * Std(AMA - Ref(AMA,-1),Periods);
AMALow := If(AMA < Ref(AMA,-1),AMA,PREV);
AMAHigh := If(AMA > Ref(AMA,-1),AMA,PREV);
If(AMA - AMALow > Filter, 1 {Buy Signal}, If(AMAHigh - AMA > Filter, -1 {Sell Signal}, 0 {No_Signal}))

Amibroker AFL translation, Edward Pottasch, 9/30/2011
*/

Periods=Param("Time Periods",10,1,1000,1); 
FilterPercent=Param("Filter Percentage",15,0,100,1)/100;
fast=2/(2+1);
slow=2/(30+1);
dir=abs(Close-Ref(Close,-Periods));
vol=Sum(abs(Close-Ref(Close,-1)),Periods);
ER=dir/vol;
sc=(ER*(fast-slow)+slow)^2; 
AMAf=AMA(Close,sc); 

//Metastock code =>  AMALow := If(AMA < Ref(AMA,-1),AMA,PREV);
aa=AMAf<Ref(AMAf,-1);
AMALow=AMAf[0];
for(i=1;i<BarCount;i++ )
{
	prev=AMALow[i-1];
	if(aa[i])
	{
		AMALow[i]=AMAf[i];
	}
	else
	{
		AMALow[i]=prev;
	}
}
//Metastock code =>  AMAHigh := If(AMA > Ref(AMA,-1),AMA,PREV);
aa=AMAf>Ref(AMAf,-1);
AMAHigh=AMAf[0];
for(i=1;i<BarCount;i++ )
{
	prev=AMAHigh[i-1];
	if(aa[i])
	{
		AMAHigh[i]=AMAf[i];
	}
	else
	{
		AMAHigh[i]=prev;
	}
}

Filterf=FilterPercent*StDev(AMAf-Ref(AMAf,-1),Periods);
Buy=AMAf-AMALow>Filterf;
Sell=AMAHigh-AMAf>Filterf;
Buy=ExRem(Buy,Sell);
BuyPrice=C;
Sell=ExRem(Sell,Buy);
SellPrice=C;

SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0,0)));
SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"Price",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
Plot(AMAf,"\nAMA",colorWhite,styleDashed);
Plot(AMALow,"\nAMALow",colorGreen,styleDashed);
Plot(AMAHigh,"\nAMAHigh",colorRed,styleDashed);

PlotShapes(IIf(Buy,shapeUpTriangle,shapeNone),colorGreen,0,L,-10);
PlotShapes(IIf(Buy,shapeHollowSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);
PlotShapes(IIf(Sell,shapeDownTriangle,shapeNone),colorRed,0,H,-10);
PlotShapes(IIf(Sell,shapeHollowSmallCircle,shapeNone),colorWhite,0,SellPrice,0);
5. ole
over 14 years ago

empottasch’s Metastock PREV conversion to AFL provides the correct result. For those who want to convert PREV in the future you might find the messages posted on the AmiBroker Yahoo forum through the years about doing the conversion with both arrays and loops useful.

over 14 years ago

I added a little extra code to the supertrend to give the trend a color:

// translation in Amibroker AFL, Edward Pottasch, 9/29/2011
Factor=Param("Factor",3,1,10,1);
Pd=Param("ATR Periods",10,1,100,1);
Up=(H+L+C)/3+(Factor*ATR(Pd));
Dn=(H+L+C)/3-(Factor*ATR(Pd));

aa=Cross(C,LLV(Up,13));
bb=Cross(HHV(Dn,13),C);
Td[0]=1;
for(i=1;i<BarCount;i++ )
{
	prev=Td[i-1];
	if(aa[i])
	{
		Td[i]=1;
	}
	else if(bb[i])
	{
		Td[i]=-1;
	}
	else
	{
		Td[i]=prev;
	}
}
aa=Dn==HighestSince(Cross(Td,0),Dn);
Dnx=Dn[0];
for(i=1;i<BarCount;i++ )
{
	prev=Dnx[i-1];
	if(aa[i])
	{
		Dnx[i]=Dn[i];
	}
	else
	{
		Dnx[i]=prev;
	}
}
aa=Up==LowestSince(Cross(0,Td),Up);
Upx=Up[0];
for(i=1;i<BarCount;i++ )
{
	prev=Upx[i-1];
	if(aa[i])
	{
		Upx[i]=Up[i];
	}
	else
	{
		Upx[i]=prev;
	}
}
aa=Td==1;
bb=Td==-1;
ST[0]=1;
trend=Null;
for(i=1;i<BarCount;i++ )
{
	prev=ST[i-1];
	if(aa[i])
	{
		ST[i]=Dnx[i];
		trend[i]=1;
	}
	else if(bb[i])
	{
		ST[i]=Upx[i];
		trend[i]=-1;
	}
	else
	{
		ST[i]=prev;
		trend[i]=trend[i-1];
	}
}
SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0,0)));
SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
Plot(IIf(trend==1,ST,Null),"\nUP",colorGreen,styleDashed);
Plot(IIf(trend==-1,ST,Null),"\nDN",colorRed,styleDashed);
over 11 years ago

DearSir,

I have One Metastock Formula, I want to Use this formula into Amibroker
Will U Please Help me Anyone for COnvert METASTOCK FORMULA into Amibroker

buy

period:=(10);
atrfact:=(6);
HiLo:=If(H-L<1.5*Mov(H-L,period,S),H-L, 1.5*Mov(H-L,period,S));
Href:=If(L<=Ref(H,-1),H-Ref(C,-1),(H-Ref(C,-1))-(L-Ref(H,-1))/2);
Lref:=If(H>=Ref(L,-1),Ref(C,-1)-L,(Ref(C,-1)-L)-(Ref(L,-1)-H)/2);
diff1:=Max(HiLo,Href);
diff2:=Max(diff1,Lref);
atrmod:=Wilders(diff2,period);
loss:=atrfact*atrmod;
trail:=
If(C>PREV AND Ref(C,-1)>PREV,
Max(PREV,C-loss),
If(C<PREV AND Ref(C,-1)<PREV,
Min(PREV,C+loss),
If(C>PREV,C-loss,C+loss)));
Trail;
C>Trail

sell

period:=(10);
atrfact:=(6);
HiLo:=If(H-L<1.5*Mov(H-L,period,S),H-L, 1.5*Mov(H-L,period,S));
Href:=If(L<=Ref(H,-1),H-Ref(C,-1),(H-Ref(C,-1))-(L-Ref(H,-1))/2);
Lref:=If(H>=Ref(L,-1),Ref(C,-1)-L,(Ref(C,-1)-L)-(Ref(L,-1)-H)/2);
diff1:=Max(HiLo,Href);
diff2:=Max(diff1,Lref);
atrmod:=Wilders(diff2,period);
loss:=atrfact*atrmod;
trail:=
If(C>PREV AND Ref(C,-1)>PREV,
Max(PREV,C-loss),
If(C<PREV AND Ref(C,-1)<PREV,
Min(PREV,C+loss),
If(C>PREV,C-loss,C+loss)));
Trail;
C<Trail

Leave Comment

Please login here to leave a comment.