How to Backtest a Trading System in Amibroker

One of the most useful things that you can do in the Amibroker is to back-test your trading strategy on historical data. This can give you valuable insight into strengths and weak points of your system before investing real money. This single AmiBroker feature is can save lots of money for you.
Let’s see how to back–test a trading system:
Fist you need to have a trading system and it should have buy and short condition, for this here I used simple 20 and 50 dma trading system, look at the below formula.


_SECTION_BEGIN("MA CROSSOVER");

MA1 = MA ( C, 20);

MA2 = MA ( C, 50);

Buy = Cross (MA1,MA2);

Sell = Cross (MA2,MA1);

Buy=ExRem(Buy,Sell);

Sell=ExRem(Sell,Buy);

Short=Sell;

Cover=Buy;

BuyPrice=ValueWhen(Buy,C);

SellPrice=ValueWhen(Sell,C);

ShortPrice=ValueWhen(Short,C);

CoverPrice=ValueWhen(Cover,C);

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );

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

To use above formula, copy the formula and save it with .afl extension.

Now Open your Amibroker and go to Analysis and select New Analysis.



A New Analysis window will open, and then pick the required formula as mentioned below image ( if you need above formula, just pick the formula from saved folder).



Then in the same window you have to select symbols, you can select the all symbols of the data base or you can filter the some symbols or you can select current symbol. Here I opened NIFTY chart, in Apply to section I selected current so the formula applied only for NIFTY.



Then you have to mention the data range which you want back test. Here you have four options, All quotes, 1 recent bars, 1 recent days and From-To date. Here I selected From-To dates so  I have to specify  from and to date in next section.



Then press Settings in Analysis window.



A Setting window will open, in that specify the Initial Equity, in next section select position, here you have three choice, if you select Long, the formula will only apply for long trade, if you select Short, the formula will only apply for short trade, If you select Long and Short, the formula will apply for both long and short trade. Here I selected long and short.
In next section you have to select periodicity, here you can select different periodicity. Here I selected 5 minutes.
In next section Min. shares make it 1 and Min. pos. Value make it 0. Other settings are not necessary, default settings is enough.



Then press Backtest in Analysis window, now Amibroker will back test your system.



Now you can get the detailed report of your trading system, to get that press report button in Analysis window.



In report you can get detailed statistics of your trading system. This system gives 16.39% returns in nifty from August to January.





That’s all; this is the simple way to back test your trading system in Amibroker.
Previous
Next Post »