Amibroker: How To Optimize A Trading System


Beginners Course  |  Intermediate Course  |  Amibroker Course

Amibroker is probably one of the best “bang for your buck” charting packages available.  Apart from the back-testing capabilities allowing you to easily back-test a stock or portfolio of stocks, it has another amazing feature that saves you time and you are absolutely going to love.

That feature is optimization.

What Is Optimizing A Trading System In Amibroker?

Optimizing a trading system in Amibroker allows us to back-test our chosen stock, or group of stocks to see if our method works.  But further to this, in Optimizing our trading system we automatically get performance data on a large range of different parameters so we can choose the best combination of rules to use.  This optimizing process often takes only a few minutes.

For example:

Let’s say we are using a simple Moving Average crossover system, where we buy when the 25 Day Moving Average crosses above the 100 Day Moving Average.

We back-test out results, and the results are OK.  Nothing stellar.

But then we use a simple “Optimize” command, we can also test the Moving Averages between 0 and 200 to see which Moving Average works best.  The results are finished within minutes, and we can instantly see that there is a better Moving Average combination we could be using.

How To Optimize A Trading System In Amibroker Using Amibroker AFL

So let’s get to it.  First, as always, we need a little bit of Amibroker AFL code to get us started.

  • From the main screen of Amibroker, go to Analysis > Formula Editor.
  • Enter the following code into the formula editor:

FastMALength = Optimize( “FMA”, 100, 5, 100, 1);
SlowMALength = Optimize( “SMA”, 25, 1, 100, 1);
FastMA = MA(C, FastMALength );
SlowMA = MA(C, SlowMALength );
Buy = Cross(FastMA, SlowMA);
Sell = Cross(SlowMA, FastMA);

  • If it doesn’t copy and paste correctly, you may need to type it out into Amibroker, the code definitely works.
  • The Amibroker Formula Language quickly explained: The first section sets up our Optimize rules.  It tells Amibroker that our Array word “FastMALength”, when we use it, will equal our Optimization that follows.  Then with “Optimize”, it first gives it a name (for the output table in our results), then our preferred value (100), our minimum value (5), maximum value (100) and the increments we go up by with each new test (1).  You can change these number to suit you, and the Optimize function, once assigned to an ARRAY (like our FastMALength example), can be used with many different tools.

You can save it if you like, then click Tools > Send To Auto Analysis.

The Automatic Analysis window appears.  Now we work from left to right before we ultimately press the Optimize button.

  • Under “Apply To”, select either “Use Filter” and choose the watchlist you want to test, or “current stock” if you want to test the stock you are currently viewing
  • Under “Range”, select “From” and choose the date ranges you want to test
  • Then click settings, choose “Periodicity” > Daily (or Weekly if you prefer trading on a weekly timeframe), and under the report tab, select “Trade List”
  • Click OK
  • Then click on the arrow next to Optimize, ensure it is set to Optimize Portfolio, then click Optimize!

Viewing Your Optimization Results

Once the system has finished optimizing, it will display a series of results for you to sort through.  You will see things like CAR (Compound Annual Return), Maximum trade or system drawdown (losing streaks), Win Percentage, total trades, average profit and average loss and much more.

One of the most common methods of judging a trading system is the CAR / MDD.  This is the Compound Annual Return divided by the Maximum Drawdown, and basically helps us pick the system with the highest annual return versus the maximum drawdown.  If you click on the CAR / MDD column, it should show an arrow pointing down and show you the results from top to bottom.

You can then scan over the rest of the results, see which combination of moving averages worked best, and see how many trades were taken overall.

Try It Out For Yourself

So now you have the basics of optimizing a trading system in Amibroker.  Try it out with different indicators or methods, and see if you can optimize a trading system for yourself!

Happy trending,

Dave McLachlan

Beginners Course  |  Intermediate Course  |  Amibroker Course

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

7 Responses

  1. Rajandran R - June 4, 2012

    Nice explaination about optimization of a trading system. But to check the robustness of the trading system optimization is not enough we need to go for walk forward testing. Hope you will be writing on walk forward testing also. Which could be benificial for everyone interested in developing a trading system.

  2. Dave McLachlan - June 5, 2012

    Rajandran that is so true!

    As you know what works in one year (or decade) might not work in another. Looks like my next post will have to be on walk forward testing 🙂

  3. THIRU MURUGAN - October 16, 2012

    Hi Dave,

    You really doing a very nice job here, your efforts are much appreciated. From the above explanation i am able to understand how optimization works and how useful it is. But i have difficulties in finding out how to do optimization for other indicators, say for example stochastic oscillator. can you please explain how to apply this optimization functionality for other indicators as well. Or you can at least direct me to a resource where i can find more about this. Thanks in Advance. God bless you.

  4. Dave McLachlan - October 20, 2012

    Absolutely Thiru, and thanks for the kind words.

    I would recommend above anything else “Introduction to Amibroker” by Howard Bandy. There you will find instructions on pretty much all the indicators available in Amibroker, as well as optimizing instructions simlar to above. His work is the best.

    Hope this helps! Dave

  5. Dennis O'Flynn - January 6, 2013

    Hi Dave,
    I’m President of the ATAA Newcastle Chapter and we are having a meeting this coming Saturday, 12th January.

    I’d like to use/mention some of your Amibroker usage material with of course reference to ASX Market Watch and yourself.

    Would that be OK?

    My number is [number removed]

    best regards


  6. MARC - September 1, 2014


    Thank you very much for your tutorials, I would be lost without them.

    With the above code, I keep getting the “Error 31. Syntax error, unexpected IDENTIFIER, expecting ‘}’ or ‘,’ “. And, it is over the S in SMA. No matter how many times I re-write it, change spaces, it still comes up with this error.

    Any ideas?



  7. Dave McLachlan - September 2, 2014

    Hey Marc,

    You copied and pasted, didn’t you? 🙂 Ah, it is so much easier, but alas, gives us problems in the code. Luckily I know where the problem is.

    Take out the quotation marks “” around FMA and SMA, and write them in yourself. I reckon that should do it. The website makes them left and right ones, instead of neutral ones that Amibroker needs.

    Keep me posted, if it doesn’t work I’ll have another look.

    Cheers, Dave

Leave a Reply