Let’s Learn Amibroker – How To Backtest a Single Stock Or Index


Beginners Course  |  Intermediate Course  |  Amibroker Course

Using Amibroker to backtest your trading ideas automatically can be one of the greatest time saving tools you will ever use.  Instead of manually testing your idea, you get the flawless machine to do it and tell you whether you can be successful or not using a particular method.

Luckily with Amibroker and the Amibroker Formula Language (AFL) you can back-test your strategy automatically, over as much history as is available.  Instead of testing taking days or even weeks, you can test your strategy in a matter of minutes with exact results.

Doesn’t it makes sense to test out the plane before you fly it?  Or test drive the car before you buy it?

Now we are going to learn how to run an automatic backtest using Amibroker, and input some new Amibroker Formula Language (AFL) to boot.

How To Run A Back-Test On A Stock Using Amibroker AFL

First let’s enter our Amibroker AFL for the back-test.  This is a simple buy on a “20 Day High” strategy, sell on 10 day low, a REALLY rough version of Richard Dennis’ Turtles strategy.

You could also change the values to be anything you like (for example, a 52 week high / low scenario).  Simply change the 20 for the highest high value you want, and the 10 for the lowest low value you want.

In the top toolbar, go to Analysis > Formula Editor and enter the following code:

HI = HHV (C, 20);
LW = LLV (C, 10);
Buy = C >= HI;
Sell = C <= LW;

  • Sometimes the code won’t work unless you type it in manually, as copying it from a webpage can bring additional whitespace or hidden characters.
  • You can save this code if you like, then; Go to Tools > Send To Auto Analysis

The Automatic Analysis Window will appear.  Here we will move from left to right and ultimately click our Amibroker Backtest button.

  • Under “Apply To”, select “Current Stock” and make sure you are looking at  chart of the stock you wish to backtest.
  • Under “Range”, select “From” and input the dates you wish to backtest the stock for (usually five to 10 years is a good timeframe, and some people believe the more the better).
  • Under “Settings” in the General Tab, select “Periodicity” > Daily.

Click the Backtest button, and the stock will back test using the method we gave it!

How To See Your Results Quickly And Easily Using “Report”

Once the back test is complete, click the “Report” button.  The statistics of this method will come up in your Backtest report.  You can see things like the Annual Return, The overall Net Profit, and then your Win Percentage, Loss Percentage, and your average profit and loss per trade.

If you click on the “Charts” button up the top of this back-test report, you can see things like your equity curve, your drawdown or Underwater Equity, and your monthly or yearly profit and loss statement.  Just like a real Managed Fund or Hedge Fund.  Pretty cool huh?

How To See The Buy And Sell Arrows On Your Amibroker Backtest

So we’ve played with all our data and gasped in awe at the great things we can measure with our stock market system, wouldn’t it be great to see on our chart exactly where we bought and sold?

Of course it would.

So if we go to our chart that is open in Amibroker and right click, we get options like “Parameters” and “Edit Formula”.

  • Click on “Parameters”.
  • Go to “Axes and Grid” tab
  • Scroll down and select “Show Trading Arrows” > Yes Click OK
  • Now if we go back to our Automatic Analysis screen (Analysis > Automatic Analysis), run the backtest then;
  • Right Click on the stock name (with the details and results) Select “Show Arrows For Actual Trades”

And you will be able to see a Green buy arrow and a Red sell arrow on your chart where the system bought and sold!

Amazing stuff!

Go And Play

This tool is so amazing and powerful, and I have often spent countless hours tweaking a system or method and checking the statistics it brings up.  I hope you enjoy it too, go and play with it and see what you come up with!

Happy trending,

Dave McLachlan

Other Amibroker Articles and Videos

Lessons On Trading Systems


. If you enjoyed this, subscribe and get the latest updates for free:

Enter Your Email Address:


(Check Your Junk Mail, Just In Case)

Related Articles You Will Like:

June 3, 2012  Tags: , , , , , , ,   Posted in: Amibroker, Amibroker Course, Articles On Building Wealth

One Response

  1. Babu Kothandaraman - December 21, 2013

    Hi Dave,
    What a simple but effective collection you’ve compiled! It’s just amazing to see the amount of efforts you’ve put in; and your thought process to make it simple and at the same time to be more effective is commendable! These are really golden nuggets for those beginners who’re clueless on how to use AB.

    Keep up the good work, Dave!

    Thanks a million!
    Babu Kothandaraman

Leave a Reply