Optimizing Strategy Parameters

From FxCodeBaseWiki
Jump to: navigation, search

What is Optimization the Strategy Parameters?

Optimization of the strategy parameters is an attempt to find the parameters would be the best for using the strategy on some historical market. Since the market is not absolutely random, you may expect that the patterns of the price behavior you may see in past, would appear again for the same or for the related instruments.

In fact, the only way to find the best parameters is to try all possible set of the parameters (in other words backtest the strategy). Such continuous probing of all possible combination is called exhaustive optimization.

However, if the strategy has only two integer parameters and you would like to try the first parameter in range 1-15 and the second in range 15-50, it give us 576 various sets. If we try all of them and each takes just 1 minute, the finding of the best parameters will take 10 hours. Long enough, huh?.

There are a number of methods to speed up that process. Often we don't need a really best parameters, a good enough might be enough for us. Or we may look for the smaller range of the parameter values, which can be refined in future using exhaustive optimization. The SDK 2.0 implements Genetic optimization of the parameters.

How To Use?

Check Performance First

First, check the strategy performance on the range of the price data you are going to optimize. The strategy must be fast enough to be testing in 1 minute or less. The longer backtesting time may make optimization too long. The absolute majority of the strategies may be developed with such performance measures. Optimize your strategy or contact to a strategy developer if the strategy is too slow.

Start Optimizing Parameters

Then choose to the Tools->Optimize command of the Integrated Lua EditorDebugger. The list of the strategies (as for backtesting) will be shown. Please note that you may optimize parameters as for user's as well as the standard strategies. When the strategy to optimize is chosen, the optimization parameters form will be shown.

The form looks very similar to the strategy parameters in debugging or backtesting. The first category of parameters shall be filled exactly in the same way as for debugging or backtesting.

The second category of the parameters configures the optimizer:

Optimize1.png

The first parameter manages the method of the optimization. Choose Genetic for approximation of the best parameters range inside of the big set of parameters and Exhaustive for finding the best parameters in the small range.

The second parameter manages the measurement which will be used to recognize whether the parameter set is better or not:

  • The Highest balance looks for the parameters set which provides the highest balance of the account at the end.
  • The Profit factor looks for the parameters set which produces more profitable trades than non-profitable.
  • The Balance and profit factor looks for the parameters set which provides the highest value of the product of balance and profit factor. Profit factor is calculated as a ratio of total profit to total loss. That is, this criterion maximizes the following value:
    <math>

FinalBalance \times{\dfrac{TotalProfit}{TotalLoss}} </math>

  • The Balance and average profit per trade looks for the parameters set which provides the highest value of the product of balance and average profit per trade:
    <math>

FinalBalance \times{\dfrac{TotalProfit - TotalLoss}{TradeCount \times{InitialBalance}}} </math>

  • The Balance and minimal fall of balance looks for the parameters set taking into account the balance and fall of balance:
    <math>

FinalBalance \times{(1 - \dfrac{MaxFallBalance}{InitialBalance})} </math>

The third parameter manages the resource usage. By default, the optimizer will try to use all available resources. If you are running some time-critical applications you may want to leave a part resources for them. In that case choose the number of processor cores from the list.

That's all. Then you must configure the parameter of the strategy. The parameters looks very close to the debugging or backtesting, but for the parameters you can optimize you may specify a range of the values instead of one value. Such parameters has a small plus sign at left of the parameter name:

Optimize2.png

If you click a plus sign, the input fields for minimal and maximal values, and the step will be shown. If you enter different values into minimal and maximal values, the optimization will be performed for that parameter.

Optimization

When optimizer started it runs a bunch of backtesters and looks for the best result. You can see the number of combination tested in the progress bar. If you break the process you will see the result of all already passed attempts.

When all combination are probed for exhaustive optimization or further testing don't give any significant improvements for genetic optimizer, the optimization will be stopped.

The optimization result shown on:

Output Tab

The output tabs shows the optimization parameters used:

  • The parameters to be optimized and their ranges
  • For genetic algorithm:
    • Size of Populations (the number of random combination probed initially)
    • Number of Generations (the maximum number of attempts to improve)
  • The number of the set which shown the best result during optimization
Optimize4.png

Optimize Result

The Optimize Result tab contains the detailed table of all parameter sets probed with all statistics collected during the probe. The parameter sets which were not profitable at all shown in red. The best found parameter set is shown in green.

Optimize5.png

Optimize Result Graph

The Optimize Result Graph tab shows the optimization result as two-dimension map. You can choose any two of the parameters chosen for optimization as X and Y axis. The green cells show the positive results and greener is better, the red cells show the negative result. The white cells show the sets which weren't tried at all.

If there is multiple results in a cell (for example if there were three parameters to be optimized), a cell may be splitter into smaller cells, and each of them may be colored differently.

Click on a cell to get detailed information.

Optimize6.png

Tuning Population/Generation Parameters

If you don't like the optimization result you may re-run optimization and specify the genetic optimization parameters (size of the initial population and number of generations) instead of default values:

Optimize3.png

The bigger initial population forces to try bigger number of sets on the first attempt. If the initial population is equal to the size all possible parameter sets the genetic optimization works exactly like exhaustive, trying all possible variants.

The smaller population size makes optimization process faster but increases the risk that some parameter group won't be probed at all.

The number of generations manages how long the optimizer will try to find the better variant. The biggest value makes process longer but increases the chance that better parameter will be found.

The default values for the initial population and the number of generations are calculated as:

<math>

\mbox{population} =
\begin{cases}
\mbox{set size} & \mbox{set size} < 25 \\
\frac {\mbox{set size}} {2} & 25 \le \mbox{set size} < 256 \\
2^{\operatorname{log}_{10}(\mbox{set size} + 1.5)} & \mbox{otherwise}
\end{cases}

</math>


<math> \mbox{generations} =

\begin{cases}
  1 & \mbox{set size} < 256 \\
  2^{\operatorname{log}_{10}(\mbox{set size} + 2)} & \mbox{otherwise}
\end{cases}

</math>

where set size for a set of n parameter is:

<math> \mbox{set size} = \sum_{i=1}^n {(\frac{\mbox{max value}_i - \mbox{min value}_i} {\mbox{step}_i} + 1)} </math>

See Also

Optimizing Strategy Parameters (Step by Step)

This Article in Other Languages

Language: English  • español • français • русский • 中文 • 中文(繁體)‎