When you develop a strategy, many times you need to assign a value to a specific Buy or Sell condition. You probably select this number after playing with the Charts module and checking different values to a technical indicator and back test. However, there is no assurance that this selected number results in a strategy with best performance, and there may be a value that if selected, will result in better performance for the strategy. You may change it manually and back test, but may be a very time consuming task with no guarantee to get the optimized result. This is when Optimization function, comes to help.

Instead of choosing a number, you may define variable and tell the Optimizer in which range this number should change, the Optimizer will create many different strategies - "strategy variant" (for each possible number in the range) and will compare the backtest result, showing the best performing variant first and revealing the optimize number. *Note that currently we support up to 2 optimization variable with a predefined name $x and $y. You must use these names as other variables will be ignored during the optimization.*

For example, consider the following buy rule:

if ( MP == 0 and

Close() == MIN(10) ) { # if Close is MIN of last 10 days

Buy( );

}

if ( MP != 0 and

Close() == MAX(5) ) { # if Close is MAX of last 5 days

Sell();

}

Now, lets say that we would like to optimizer to choose the best possible values instead our our mentioned 5 and 10. We will add a $x and $y variables and the result will look like:

# Strategy MINMAX with 2 optimization parameters

my $x;

my $y;

if ( MP == 0 and

Close() == MIN($x) ) { # if Close is MIN of last X days

Buy();

}

if ( MP != 0 and

Close() == MAX($y) ) { # if Close is MAX of last Y days

Sell();

}

*Important:* You cannot use $x or $y variable inside a comment including a Buy or Sell comments! For example, you may not use:

#

# This strategy uses $x variable ; == ERROR CANNOT USE $x in a comment

#

Buy(" Var Y=$y "); == ERROR CANNOT USE $x in a Buy comment

Sell(" Value of X is $x "); == ERROR CANNOT USE $x in a Sell comment

my $x; # comment: $x is my first optimization variable: == ERROR CANNOT USE $x in a general comment

Doing so will fail the optimization.

Now we can save the strategy to keep changes and Edit again:

Select Optimize and define optimization parameters as following:

Now, we need to select optimization parameters:

Symbol - select a symbol to backtest. (ability to optimized for Universe of symbols is reserved to Power Users only)

$x Start value - in the example 1

$x End value - in the example 25

$x Delta value (or incremental) - in the example 1. This means we have 25 possibilities from variable x.

Time range for backtesting - up to 10 years.

$y Start value - in the example 1

$y End value - in the example 25

$y Delta value (or incremental) - in the example 1

Report size max - since some optimization may result in thousands of variants sorted by performance and since we are interested only in best performing one, we may limit the number of variants displayed as result. Note that as more you select slower the rendering and you will wait longer for the result.

So in this example we will have 25x25=625 strategy variants. Click Run to execute:

After a minute we will find a screen with a table. Note that optimized parameters are part of the strategy variant name (as shown below). First number is optimized x and second is optimized y. We can also see the P&L result for the selected time range: