Amibroker Q & A: How to Code Trend Pilot: Five Consecutive Days Above or Below a Moving Average

Line1

Beginners Course  |  Intermediate Course  |  Amibroker Course Home

This week’s Amibroker question comes from Brendan, who asks how to code five consecutive days above or below a moving average.  This is similar to how the Trend Pilot trading system works, although once we know how, we can use it for any other trading system or model we like.

In the video, we use the “Ref” function, which “refers” to previous bars.

We also back-test the Trend Pilot trading system quickly, with some good results for a solid trend following trading system.

Check it out!

UPDATE:  There are also three faster ways to code this, pointed out by some great viewers!

Hope you enjoyed, and 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

January 24, 2016  Tags: , , , , , , ,   Posted in: Amibroker Q & A

3 Responses

  1. Gordon - January 25, 2016

    Hi Dave,

    A much easier way would simply be to use following formula:

    Buy = BarsSince(Cross(Close, movingAv)) == 4 AND Close > movingAv;

    Thanks,

    Gordon

  2. David McLachlan - January 26, 2016

    Hey Gordon,

    That is awesome, thank you! Brilliant. Maurice at Youtube also gave two more methods, credited to the great Cesar Alvarez. Can’t wait to do a video explaining how simple things can be, in the right hands! 🙂 But it’s great to have such cool readers to help everyone out too.

    movingAv = MA(C,100);

    movingAvAbove = C > movingAv;
    movingAvBelow = C < movingAv; Buy = LLV(movingAvAbove, 5); Sell = LLV(movingAvBelow, 5);

  3. David McLachlan - January 26, 2016

    The comments are formatting it a little funny, but here is the second one too:

    movingAv = MA(C,100);
    movingAvAbove = C > movingAv;
    movingAvBelow = C < movingAv; Buy = sum(movingAvAbove, 5) == 5; Sell = sum(movingAvBelow, 5) == 5; Happy trending! Dave

Leave a Reply