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

Beginners Course  |  Intermediate Course  |  Amibroker Course Home

Videos in the FREE Amibroker Course:

  1. Amibroker: How To Import A Watchlist With A CSV File
  2. Amibroker: How To Backtest A Single Stock Or Index
  3. Amibroker: How To Backtest a Portfolio or Group of Stocks
  4. Amibroker: How To Optimize A Trading System
  5. Amibroker: How To Scan The Market For Buy And Sell Signals
  6. Amibroker: How To Use Explore To Find And Filter Stocks
  7. Amibroker: How To Add Position Sizing Using AFL
  8. Amibroker: How to Add An Index Filter To Your Trading System Using AFL
  9. Amibroker: How To Plot Your Trading System On A Chart (incl. Buy/Sell Arrows)
  10. Amibroker: How to Use the If, Then Else Function (IIF)
  11. Amibroker: How to Code the ATR Trailing Stop (Chandelier) using ApplyStop
  12. Amibroker: How to Plot ApplyStop on a Chart
  13. Amibroker: How to Code Van Tharp Position Sizing (Fixed Fractional)
  14. Amibroker: How to “Scale In” to Positions (1) – Buying Once a Month
  15. Amibroker: How to “Scale In” to Positions (2) – Pyramiding Three Times
  16. Amibroker: How to Use “Cross”, When One Thing Crosses Another
  17. Amibroker Advanced: How to Compare Equity Curves

FREE Trading System Video Lessons:

  1. 7 Free Trading Systems and Their Returns From The Last 13 Years
  2. Seven Full Trading Systems Revisited, With Completely New Data
  3. Trading System: How to Create A Buy and Hold Trading System
  4. Trading System: How to Code “Sell In May And Go Away”
  5. Trading System: How to Code a Moving Average Channel That Made 23% p.a
  6. Trading System: How to Code Highest Highs As Used In 52 Week Highs Or Turtle Trading System
  7. Trading System: How to Code a Bollinger Band Breakout Trading System
  8. Trading System: How to Code a Moving Average Crossover That Returned 28% per annum
  9. Trading System: How to Refer to Previous Signals For MAup Trading System
  10. Trading System: How to Code the Leap Of Faith (Gaps) Trading System That Returned 24% p.a.
  11. Trading System: How to Code a Short Selling System (Plus Results)
  12. Trading System: Meb Faber Trading System Using Different Timeframes
  13. Trading System: How to Code the Three Billion Dollar Trading System

FREE Amibroker Q & A Videos:

  1. Amibroker Q & A: How To Do A Monte Carlo Test? (on ALL versions)
  2. Amibroker Q & A: Visualising Monte Carlo Data with Histograms and Scatter Plots
  3. Amibroker Q & A: How to Create the 52 Week High/Low Index with AddToComposite
  4. Amibroker Q & A: How to Code Trend Pilot: Five Consecutive Days Above or Below a Moving Average
  5. Amibroker Q & A: Three “Hacks” To Improve Your Coding Skills
  6. Amibroker Q & A: How to Remove Excess Signals

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

4 Responses

  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

  2. mohammed hasham ahmed - September 19, 2016

    how to build auto trading system in excel?–PLEASE HELP.
    T H A N K S.

  3. Witbooi - March 30, 2017

    Good day,
    on Amibroker 5.50 rc3
    Your back test code generates
    Error 702. Missing short/cover variable assignments.

  4. Craig - June 4, 2017

    I see that you are not taking anymore paid subscribers. Are you planning on taking on more clients in the future and if so, what are the benefits?


Leave a Reply