Amibroker: How To Backtest a Portfolio or Group of Stocks


Beginners Course  |  Intermediate Course  |  Amibroker Course

So now we have back-tested a single stock using Amibroker backtest function and some basic Amibroker Formula Language (AFL).  But what if we want to see if our method works over an entire portfolio of stocks?  Maybe we want to test it over the ASX Top 300, or the S&P 500 stocks in the United States, or a list of stocks from a Fundamental scan.  The good news is there is a simple way to do this that we will show you how.

How To Back-Test A Portfolio Of Stocks Using Amibroker AFL

First let’s start with the necessary Amibroker AFL (formula language) so we can run our back-test.  For this lesson we will use a simple Moving Average crossover of 25 days and 100 days (for example we buy when the 25 day Moving Average crosses above the 100 day Moving Average.

In the main screen of Amibroker, up the top:

  • Go to Analysis > Formula Editor
  • Enter the code below into the Formula Editor:

NumPos = 10;
SetOption( “MaxOpenPositions”, NumPos );
PositionSize = -100 / NumPos;
FastMA = MA(C, 25);
SlowMA = MA(C, 100);
Buy = Cross( FastMA, SlowMA);
Sell = Cross( SlowMA, FastMA);

  •  You may need to type it in, just in case the “” symbols change when the article is posted, but try copy and pasting it first.
  • Full credit to Howard Bandy on the Amibroker site for this code.  Check out his stuff, it is very good.

You can save it if you like, but then send it to the Automatic Analysis by going to Tools > Auto Analysis.

Now we move from left to right in the Automatic Analysis screen.

  • Under “Apply To”, select “Use Filter” and choose the watchlist you want to test
  • Under “Range”, select “From” and choose the date ranges you want to test
  • Then click settings, choose “Periodicity” > Daily, and under the report tab, select “Trade List”
  • Click OK
  • Then click on the arrow next to Backtest, ensure it is set to Backtest Portfolio, then click Back-test!

Seeing Your Results Using The Report Function

When your back-test comes up, you will see the results.  If you want all the results collated for you in a nice way, click the “results” button.  This will show you your total return, average yearly return, win percentage, loss percentage, average profit and loss per trade and much more.

If you click on the Chart tab up the top, you will see your equity curve and drawdowns as well.


Have A Play With It

You can, of course, change the values in the Moving Average back-test and see if they work better.  The good news is you can test it quickly and find what works within minutes, instead of hours, days or weeks.

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

2 Responses

  1. Stefan Mark - October 31, 2013

    Hi Dave
    Thanks for the great video and website

    I tried to set up a backtest using Amibroker 5.60 followin your instructions. I had problems, since the “Automatic Analysis” window did not open after copying the code into the formula editor and sending it as it apperas in your youtube video

    I wonder what I did wrong. Any help?
    Thanks in advance,

  2. Babu Kothandaraman - December 21, 2013

    Hi Dave,
    Another thing about your “user friendly” lessons. There is voice over in the video and the same methods are also explained in text.
    Babu Kothandaraman

Leave a Reply