Trading System: How to Code a Bollinger Band Breakout Trading System


Beginners Course  |  Intermediate Course  |  Amibroker Course Home

This lesson is based on how to code a Bollinger Band breakout trading system.  This particular Bollinger Band breakout idea was made famous by master Australian trader Nick Radge in his book Unholy Grails.  Nick is an absolutely stand-up guy as any Australian trader or investor will tell you, and I highly recommend his book and service at The Chartist.

Apart from that, the Bollinger Band breakout system is fairly easy to code in Amibroker Formula Language (AFL), even for a novice part-time coder like myself.  Of course it is only very basic code with none of the bells or whistles that other more experienced traders might add on.

It yields some solid results in back-testing.  Check out the video below!

The Results:

The results of the Bollinger Band Breakout trading system:

  • On an ASX 200 list over 13 years:
  • Win Percent: 43%
  • Average Annual Return: 23% p.a.
  • Maximum System Drawdown: 36%

Obviously, it is wise to test this trading system yourself, add parts or modify to suit your own needs, being aware of your maximum drawdown pain threshold and understand the need to test on out-of-sample data.

But it is a great look at another trading system and how to code it in Amibroker!  We can also see the back testing results within seconds, instead of testing it ourselves and taking weeks or months.  These are the great benefits of automatic testing – fast, free and easy.

Like this post?  Leave a comment in the section below.

I hope this helps, happy trending and enjoy!

– 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 30, 2013  Tags: , , , , ,   Posted in: Amibroker, Amibroker Trading System, Trading System

12 Responses

  1. ian - November 12, 2014

    Hello Dave,
    I like this BB breakout system. I bought the book as mentioned. I have no experience in programming but wanted to know how to set an index filter and how that would change the results over the 13 year period. In his book he also mentions the flipper system. Do you have a video on that. I am using prorealtime charts. Do you know them ? I think your followers might like it as the EOD data is free and covers most wrld markets.
    Just for your info I am not affiliated in any way with the company.
    I like the videos.

  2. Dave McLachlan - December 10, 2014

    Thanks Ian,
    No problem, I think Incredible Charts is another popular free / low cost charting tool that is from all accounts pretty good.
    I have a video on the Index Filter, but not the Flipper. I have played around with it, but didn’t get very good results at all.
    Thanks for the compliments, keep playing the greatest game 🙂

  3. Mark - January 12, 2015


    I tried the backtest but how come I’m not getting the same results?

    I get like 8% instead of 20%?

    Am I using the wrong asx list or something?

  4. Dave McLachlan - January 25, 2015

    Hey Mark!
    It could be a number of things – not sure without seeing your code, but the most common one is setting position sizing. From memory I used 20 positions and 5% in each. If you’re only taking one trade at a time, it will mess with your backtesting results. Pretty sure I have a video on position sizing here… 🙂
    Let me know if it’s not that.
    Cheers, Dave

  5. Kyle - May 5, 2015

    Hi Dave,
    Great videos mate!
    I am having the same issues as Mark. This ran around 6% for me and the coding is smack on. In fact my results are significantly under for all of your tutorials.
    Any other setting or filter glitches we should watch out for?
    I am currently only running the free trial for both AmiBroker and premium data. Would this be an issue?

    Mark did you work out what was going wrong?


  6. Kyle - May 5, 2015

    Could it have anything to do with backtest ranking? All my purchases and sales start with A##…
    Seems to be in alphabetical order.

  7. Dave McLachlan - May 16, 2015

    Hey Kyle,

    I’ve checked it again, and it’s definitely correct. The A## is standard – that is all good. Unless an update of Amibroker has changed the meaning of the code… but I feel that is unlikely.

    First, make sure you’ve got the position sizing code in there. That’s the code that goes:

    SetOption(“MaxOpenPositions”, 20);

    If you have that, then check the settings – make sure you are testing “Daily” charts, and buy on next day’s open after the signal. This emulates a real world situation of scanning EOD data after market hours and placing a trade for the next day.

    Let me know if you have both these things, and we can go from there.

    Cheers, Dave

  8. Yadi - August 16, 2015

    Hi Dave,

    I wrote the same language into the trial version of Amibroker 6.0. Between 2000 and 2015, I get only 3.22 % return with 32% exposure on the ASX200, it is just way too dismal. Anyway, I am using a trial version of Amibroker and premium data ($30 mo).

    Do you know why it is the case?

  9. Dave McLachlan - August 20, 2015

    Hey Yadi,
    A few things I can think of:
    Did you include position sizing code? You might only be taking one trade at a time.
    The Stock universe also makes a difference: Trading the Top 100 brings slower gains than the Top 500/small caps, for example.
    Hope this helps,

  10. Steve - June 2, 2016

    Hi Yadi,

    I think you will find the reason is the trial version of Amibroker only allows backtesting of reduced amount of stocks and also if you haven’t purchased the premium data 10 year historical data, you are only backtesting 1 year of data in any stock with a code starting from B to Z.


  11. David McLachlan - June 7, 2016

    Nice one Steve! Thank you.

    Cheers – Dave

  12. Youness - September 15, 2016

    Hi Dave,

    Thank you for the great videos. After watching your video I got an idea about a trading system as below but I haven’t had much success in creating the AFL code for it.

    The system says:
    Buy when:
    1. you have three days of increasing volume in a row in past three days
    2. Volume has been greater than MA(V,60) at least one day in past three days
    3. RSI is less than or equal to 40 at least one day in past three days
    4. Price is lower than BBandBot(C,50,1)

    Sell when:
    Price is higher than BBandTop(C,50,6);

    I have created the below code but it is not really working. I was wondering if you could kindly have a look on it.

    SetTradeDelays (0,0,0,0);

    Volumeincrease = IIf (V > Ref(V,-1) AND Ref(V,-1)>Ref(V,-2),True,False);
    Volumeincrease1 = IIf(Ref(V,-1)>Ref(V,-2) AND Ref(V,-2)>Ref(V,-3),True, False);
    Volumeincrease2 = IIf(Ref(V,-2)>Ref(V,-3)AND Ref(V,-3)>Ref(V,-4),True,False);

    Volumegreatness = IIf(V>MA(V,60) OR Ref(V,-1)>Ref(MA(V,60),-1) OR Ref(V,-2)>Ref(MA(V,60),-2),1,-1);

    RSIvalue = IIf(RSI(9)<=40 OR Ref(RSI(9),-1)<=40 OR Ref(RSI(9),-2)<=40,1,-1); //do we have the RSI(9) under 40 in past 3 days


    Buy = C1 AND RSIvalue>1 OR Volumeincrease =True OR Volumeincrease1 =True OR Volumeincrease2 =True;
    Sell = C>=bollitop;

    Thank you very much in advance!

Leave a Reply