4.6.4 Backtest and Optimization

Backtest

By default strategies are calculated in Backtest mode and calculated on historic chart bars.

To see the result of strategy calculation on historic data, in the MultiCharts .NET main menu select View and click Strategy Performance Report. In this report you will find detailed information concerning the performance and results of the strategy calculation.

In Backtest mode the strategy handles all the chart bars one by one. And at each bar, the calculation of all the strategy signals that are specified in Format Object dialog window Signals tab are called. After the strategy has calculated all historic bars of the chart symbol, it proceeds to Realtime mode calculation.

Strategy calculation on the next bar in sequence will result in a list of orders generated by the signal that includes all order for which the Send() method was called during the calculation. Then the orders process to the broker emulator to be filled based upon the current strategy and price conditions of the bar.

Optimization

Usually, a strategy consists of several signals, and each signal has its own inputs for each symbol, which affect the strategy operating result. It is possible to manually select the input values for each symbol, to achieve maximum strategy effectiveness, but this is timing consuming and inconvenient to do. In MultiCharts .NET you can optimize signal inputs automatically and receive a report with the optimization results, displaying the strategy key performance indices for each inputs combination (Net Profit, Total Trades, %Profitable, etc.). To do this, open the Format Object dialog window, select the Signals tab and then click on the Optimize button. There you can choose optimization mode:

  1. Exhaustive –calculation of all input values in the specified limits with the specified step. This optimization mode requires a considerable amount of time, however, none of the input combinations will be missed. It is also helpful, if the optimization results are analyzed by 3D Optimization Chart.
  2. Genetic – searches for the best input values of a genetic algorithm that helps to find the best inputs for the minimum amount of time. Also chooses the best range and steps for signal inputs calculations.

The optimization results are available in MultiCharts .NET main menu: click View and select Strategy Optimization Report. To apply inputs, you need to double click on the line in the optimization report and set the input combination which will be applied to the strategy.

Optimization can be done upon one of the several standard criteria: Net Profit, Total Trades, Profit factor … and others. Among these parameters there is the Custom Fitness Value parameter. It is a manual parameter, which can be calculated right in the signal script during the strategy calculation. The strategy optimization will then be executed based upon this parameter.

The value of the Custom Fitness Value parameter is set through the double CustomFitnessValue { set; } signal property. The last value is used during the optimization, the value that was set by the signal to the end of the strategy calculation on all the data series. If CustomFitnessValue is calculated by several strategy signals, then as the calculation result the value, set by the signal, that was calculated last, is used. During the strategy calculation, in optimization mode, there are several features, which should be noted, when you write signal scripts:

  1. Several strategy instances are created in parallel during the optimization. And they are calculated in different threads, that is why global (static) variables should be used with care. MultiCharts .NET for optimization creates the amount of threads equal to the amount of cores of your computer. If it is necessary to do the optimization in one thread, you can use the following key: «NumberOfThreadsOnOptimization», upon the register path 'HKEY_CURRENT_USER\Software\TS Support\Power Language\StudyRunner.
  2. Signals cannot draw and get access to drawings during the optimization (DrwTrendLine, DrwArrow and DrwText).
  3. Also, it is not recommended to use input-output during optimization (console operations, Output, files, etc.), as it can significantly slow down the optimization process.

Information, regardless of whether a signal is in optimization mode or not, can be seen in the Environment.Optimizing property, if true is returned the calculation is in optimization mode if false is returned the calculation is not in optimization mode.