Advanced Guide To NinjaTrader: Strategy Analyzer

The NinjaTrader Strategy Analyzer allows traders to run historical analysis on NinjaScript-based automated trading strategies. To open the Strategy Analyzer, within the NinjaTrader Control Center select File > New > Strategy Analyzer, as shown in Figure 17.

Backtest a StrategyTraders can use NinjaTrader's Strategy Analyzer to a strategy to determine how it would have performed on historical data. To run a backtest, traders will need access to historical data and a NinjaScript strategy. The NinjaTrader platform is installed with several pre-defined sample strategies; conversely, traders can create their own custom strategies by programming in NinjaScript or working with a qualified NinjaScript programmer.

To run a backtest:

1. Right-click the desired instrument or instrument list and select "Backtest" from the menu, as shown in Figure 19; or click the "b" icon (Run Backtest) in the toolbar.

Figure 19: Right-click the desired instrument or instrument list and select "Backtest" from the menu.

By default, the Strategy Analyzer uses a conservative and more realistic approach to filling limit orders and stop-limit orders. Using this approach, limit orders are filled only if the limit price is penetrated, and limit orders are always filled at the limit price specified (and never better). Traders can choose the Liberal algorithm that takes a more aggressive approach to filling limit and stop limit orders during backtesting. The Liberal approach will fill limit orders if the limit price is touched (rather than if it is penetrated), and on gap bars, limit orders will fill at the most favorable price level (such as at the high of the gap down bar for buy limit orders, or at the low of the gap up bar for sell limit orders). To choose the type of fill the backtest will utilize, select either "Default" or "Liberal" from the drop-down menu next to "Fill Type" under Historical Fill Processing, as shown in Figure 22.

Figure 22: Under Historical Fill Processing, choose either "Default" or "Liberal" to define how limit and stop limit orders will be filled during backtesting.

The properties that are available in the Backtest window are shown in Figure 23, with a brief description of each.

Figure 23: The available properties in the Backtest window.

Optimize a StrategyTraders can optimize a strategy to determine the input parameters that produce the best results. The optimization process tests a range of input values through iterative backtests to find the optimal values during the historical testing period. To optimize a strategy, traders need access to historical data (on which to test the strategy) and a NinjaScript strategy. The NinjaTrader platform is installed with several pre-defined sample strategies; conversely, traders can create their own custom strategies by programming in NinjaScript or working with a qualified NinjaScript programmer.

To Run an Optimization1. In the Strategy Analyzer window, right-click on an instrument or instrument list and select "Optimize" from the menu, as shown in Figure 24; or, click the "o" (Run Optimizer) icon in the toolbar.

Figure 24: Right-click on an instrument or instrument list and select "Optimize."

2. Select a strategy from the Strategy slide out menu, as shown in Figure 25.

Figure 25: Select a strategy from the Strategy slide-down menu.

3. Set the backtest properties (see Figure 26).

Figure 26: Set the backtest properties.

4. Click "Run Optimization" to begin the test.

5. The optimization progress will be shown in the Status Bar of the Control Center.

Sub-parametersThe strategy input parameters can be expanded to show the following sub-parameters, as shown in Figure 27:

Min. Value - The starting value for the test

Max. Value - The last value for the test

Increment - The step value used in the test

Figure 27: The strategy input parameters can be expanded to show several sub-parameters used in the optimization.

The input value "Fast" in the above figure, for example, has a Min. Value of 10 and a Max. Value of 40 with an increment of 1. This means that the first value tested will be 10 and each subsequent test will increase the value by 1 (the increment) until the Max. Value of 40 has been reached. The optimization will perform iterations based on these value ranges to determine the optimal combination of input values.

Optimization CriterionOptimization is based on the best result criterion selected by the trader. There are more than a dozen different optimization criteria from which to choose, as shown in Figure 28. If a trader selects "max. profit factor" for the "Optimize on …" criterion, for example, the optimizer will seek the optimal input values that return the best maximum profit factor.

Figure 28: Choose a result criterion on which to base the optimization.

Optimization ResultsAfter the optimization test has completed, the Strategy Analyzer window will show an "Instruments" tab and an "Optimizer" tab. The Instruments tab, shown in Figure 29, displays the best optimization result for each instrument included in the test. The Optimizer tab displays the top X number of results based on the value set under the "Keep best # results" property in the Optimizer dialog window.

Figure 29: The Instruments tab displays the best optimization result for each instrument included in the test. Image courtesy: NinjaTrader, LLC.

Most of the optimization properties are identical to the ones found in the backtest properties window, with the exception of the following (shown in Figure 30):

Aggregated - If set to True, NinjaTrader will find the optimal results for the whole basket of instruments. The COMBINED row in the results tab will display an aggregation of results for the basket of instruments. (Note: this parameter is available only if an Instrument List is selected for optimization.)

Keep best # results - Sets the number of best results to display after the optimization.

Optimize data series - If True, the Data Series Value property will be available for optimization.

Optimize on - Sets the optimization criterion on which to base the optimization results.

Optimizer - Sets the optimization algorithm that is used as either Default or Genetic. The Default algorithm tests every possible combination of parameters within the optimization range to find the best results. The Genetic algorithm is able to approximate the best solution without testing every combination.

Figure 30: Optimization specific parameters.

Walk Forward Optimization of a StrategyWalk forward optimization is a process that optimizes strategy input parameters on a set of historical data, then tests the strategy on data that follows the historical data (by "walking forward in time") using the optimized values. The process is repeated by moving the optimization and test segments forward in time.

To Run a Walk Forward Optimization1. In the Strategy Analyzer window, right-click the desired instrument or instrument list and select "Walk Forward" from the menu, as shown in Figure 31; or, click the "w" (Run Walk Forward Optimizer) icon in the toolbar.

Figure 31: Right-click an instrument or instrument list and select "Walk Forward" from the menu.

2. Select a strategy from the Strategy slide-out menu.

3. Set the Walk Forward properties.

4. Click "OK."

5. The Walk Forward progress will be shown in the Status Bar of the Control Center.

The Walk Forward properties are identical to those in the Optimization properties with the exception of the following, shown in Figure 32:

Optimization period (days) - Sets the number of days used for the "in sample" optimization data set.

Test period (days) - Sets the number of days used for the "out of sample" real backtest using the optimized input values generated from the "in sample" period.

Figure 32: Optimization period and Test period are Walk Forward-specific parameters.