Automated Momentum Strategy for NinjaTrader7 (with code)

Discussion in 'Automated Trading' started by brucelevy, Sep 17, 2018.

  1. Hey traders, I wanted to share a strategy I developed for momentum trading.

    Below you'll find the How To Guide so you can replicate the strategy yourself and learn in the process how to build it. Included is the actual working code that you can copy and paste.

    It came about because I wanted something that I could use to automatically trade a trend. I had been reading about the success of many of these hedge fund trend traders using systematic and computerized systems to trade alongside the trend. I have developed a few different ones but this one appears to be quit robust for the most part and produces decent backtest results on many markets and time-frames. I've run the backtest shown here on a 15 minute chart for day trading but it also seems to work well for swing. You'll find that I've incorporated variable parameters for the optimization tool if needed.

    The system is always in the market and exits on a signal reversal, or by end of day.


    Creating a Momentum System with NT7

    How it works:
    The momentum indicator fluctuates above and below the zero line. As the current price rises faster, relative to the prior price rises, the momentum indicator will spike to the upside. A cross above the 0 line indicates an uptrend, a cross below indicates a downtrend. This indicator is very similar to the Rate of Change indicator.

    The idea being the momentum indicator is to know when price is accelerating at a faster rate, which indicates additional capital coming into a market.
    [​IMG]
    Figure 1 - Momentum indicator
    Upon observation we can clearly see that the momentum line (black) crosses above and below the zero line multiple times, giving many false signals. There are a few options we can work with to reduce the noise and false signals.

    Filter Option 1 – Create a filter threshold above/below the zero line.

    Filter Option 2
    – Apply another indicator(s) to smooth out the raw momentum line.

    To take our strategy to the next level and ensure maximum robustness we will want to create variables in order to allow automatic optimization of the 2 filters.

    Using the strategy wizard, do the following:

    Create a new strategy named MomentumTrader.

    Set Calculate On Bar Close to False.

    Next, enter the following into the user defined inputs…

    User Defined Inputs
    [​IMG]
    Figure 2 - User defined inputs for the optimizer

    Next in the condition builder we will add the following rules:

    If the KAMA (adaptive moving average) of the momentum (user defined period) crosses above the (user defined variable), enter long.

    Flip for shorts.

    The long entry condition set is shown below with the steps numbers 1-5.[​IMG]
    Figure 3- Apply the adaptive moving average to the momentum indicator

    Hit OK and select CrossAbove in the center column.

    In the right column find the User defined inputs and select ‘MomoHigh’

    Press OK

    Now be sure to add in the Long Entry condition and do the same for shorts. Be sure to select ‘CrossBelow’ MomoLow for the short side.

    Compile and unlock/open your new strategy.

    For reference, this is what the smoothed momentum will look like when compared to the original momentum
    [​IMG]
    Figure 4- Raw Momentum (20 Period) Top, Smoothed 20 Period Momentum (Adaptive) Bottom

    PRO TIP: A smoothed momentum pairs well with a manual support resistance strategy. For instance, if price action is coming back to the neckline of a head and shoulders formation and shows resistance, this will be shown very clearly on the adaptive momentum graph as it will not be able to cross above the zero line. It makes for an excellent systematic manual strategy.


    Code for the Momentum Trader Strategy:
    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class MomentumTrader : Strategy
        {
            #region Variables
            // Wizard generated variables
            private double momoHigh = 0.01; // Default setting for MomoHigh
            private double momoLow = -0.01; // Default setting for MomoLow
            private int period = 20; // Default setting for Period
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    
                CalculateOnBarClose = false;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (CrossAbove(KAMA(Momentum(Period), 2, 10, 30), MomoHigh, 1))
                {
                    EnterLong(DefaultQuantity, "L1");
                }
    
                // Condition set 2
                if (CrossBelow(KAMA(Momentum(Period), 2, 10, 30), MomoLow, 1))
                {
                    EnterShort(DefaultQuantity, "S1");
                }
            }
    
            #region Properties
            [Description("defines the filter above the momo line")]
            [GridCategory("Parameters")]
            public double MomoHigh
            {
                get { return momoHigh; }
                set { momoHigh = Math.Max(0, value); }
            }
    
            [Description("defines the filter below the momo line")]
            [GridCategory("Parameters")]
            public double MomoLow
            {
                get { return momoLow; }
                set { momoLow = Math.Max(-0.09, value); }
            }
    
            [Description("Adjustable lookback period")]
            [GridCategory("Parameters")]
            public int Period
            {
                get { return period; }
                set { period = Math.Max(1, value); }
            }
            #endregion
        }
    }
    
    
    Backtest using the following settings:
    [​IMG]
    Figure 5 - Backtest settings

    NASDAQ FUTURES 15 MINUTES - EXIT ON CLOSE
    [​IMG]
    Figure 6 - Backtest of NQ on 15 min chart

    DISCLAIMER: This is a simulated backtest, these orders were not executed on a live exchange.
     
    Last edited: Sep 17, 2018
  2. JSOP

    JSOP

    Has this system been tested in real trading environment or has it just been back-tested?
     
  3. It says at the bottom that the backtest was ran on simulation, which means not a real trading environment.
     
  4. fan27

    fan27

    The major red flag I see with this strategy is that your backtest shows just over three months of data. Can you show a backtest results chart with how this strategy would have performed over 5, 10 or better yet 15 years of data?