Backtesting for Amibroker (AFL)
kilroy0514 almost 15 years ago Amibroker (AFL)
Better way to backtest by output an excel file
When we do the backtest, we always can only choice
“open” “high” “low” “close” “average” as our entry price.
In this way, we could precisely output our exact entry price
into an excel file. So that we could do more on the result
of backtesting.
Indicator / Formula
Bars=1 + BarsSince( Day()!= Ref(Day(), -1));
NewDay = Day() != Ref(Day(), -1);
Plot(C,"",colorLime,128+styleNoLabel );
ExitTime = TimeNum()==134000;
RangeHigh = ValueWhen(NewDay, H);
RangeLow = ValueWhen(NewDay, L);
Buy = H > RangeHigh AND TimeNum()<134000;
Short = L < RangeLow AND TimeNum()<134000;
Sell=Short OR exittime;
Cover=Buy OR exittime;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
PlotShapes(IIf(Buy,shapeHollowUpTriangle,shapeNone),colorRed,0,L,IIf(Buy,-30,-25));
PlotShapes(IIf(Short,shapeDownTriangle,shapeNone),colorBlue,0,H,IIf(Short,-30,-25));
PlotShapes(IIf(exittime,shapeSmallSquare,shapeNone),colorBrightGreen,0,H,IIf(Sell,-30,-25));
BuyentryPrice=IIf(bars==1,O, RangeHigh );
ShortentryPrice=IIf(bars==1,O, RangeLow );
BT=fopen("c:backtest.csv","w");
y = Year();
m = Month();
d = Day();
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] )
{
BTE=StrFormat("%02.0f/%02.0f/%02.0f,B,%.0f\n",Y[i],M[i],D[i],BuyentryPrice[i]);
fputs(BTE,BT);
}
if( Short[i] )
{
BTE=StrFormat("%02.0f/%02.0f/%02.0f,S,%.0f\n",Y[i],M[i],D[i],ShortentryPrice[i]);
fputs(BTE,BT);
}
if( exittime[i] )
{
BTE=StrFormat("%02.0f/%02.0f/%02.0f,S,%.0f\n",Y[i],M[i],D[i],C[i]);
fputs(BTE,BT);
}
}
fclose(BT);
2 comments
Leave Comment
Please login here to leave a comment.
tanksssssssssssss
I don’t think this backtest represents real trading.
If I am not mistaken, Your AFL will trigger buy only if closing price above res, so You should use Closing price as BuyPrice. Forcing to use “BuyPrice=ValueWhen(Buy,e,1);” is cheating for two things :
1. You buy only if closing price confirm the break. This already remove a lot of cut loss transaction for false breakout.
2. The difference price range between your buy value and close oftenly huge and make a of difference in ending portofolio value.
Correct Me if I am wrongly read your AFL logic :)
regards,
toto