optimize (var default, var min, var max, var step, var bias) : var

Optimizes a trading strategy parameter for the current asset and algo combination. In [Test] and [Trade] mode the function returns the previously optimized parameter value. In [Train] mode the function performs an optimization process to find the most robust parameter value between min and max

Several optimization methods (Ascent, Genetic, Brute Force, Extern) are available through TrainMode. The optimization target can be individually defined with a user-supplied objective function. The optimization process consists of many cycles. For every cycle the optimize function generates a different value between min and max and tests its effect on the strategy. At the end of the optimization, a parameter chart is plotted that reveals the effect of a certain parameter on the strategy (see below). The best or most robust parameter values are then selected and stored in a .par file.

The chart above is a result of a parameter optimization with the Ascent algorithm. The parameter is a stop distance. It varies from 2 to 10 in 10% steps. At any step the number of winning trades (black bars), losing trades (grey) and performance (red) of the parameter are displayed. We can see that high stop distances increase the number of winning trades, but have only a small effect on the overall performance. The Ascent optimizer would here likely select 8 as the most robust parameter value, rather than the single peak at 4.29. This behavior can be changed with TrainMode.
  

The contour chart above is generated by optimizing two parameters with the Brute Force algorithm. The first parameter, a time period, runs from 20 to 60 in steps of 5; the second, a signal threshold, from 0.6 to 1.4. Green areas indicate high performance, red areas low performance of the parameter combination. The optimizer would here likely select the 35 / 0.8 parameter combination. If more than two parameters are optimized, the first two are used for the contour chart.

The size of parameter or contour charts can be set up with PlotHeight2. By using multiple optimize calls in the code, any number of parameters can be optimized (up to 16 per asset/algo component). The more parameters, the more trades are needed for a meaningful optimization, and the longer will it take. As a rule of thumb, have at least 40 trades per parameter and WFO cycle, and do not optimize more than 3 or 4 parameters for a trade entry signal and 2 or 3 for exit. Too many optimized parameters generate an overfitted strategy and reduce the out-of-sample performance. Dependent on the method, the best value of a parameter can affect the optimization of the next parameters. If the script contains loop calls, a separate parameter set for the asset/algo combination in each loop is optimized.

Training also produces a log with the results of any training step. Here's an example log of the first cycle of an Ascent parameter walk-forward optimization:

Walk[1] Bar 400..23372

Parameter 1 step 1: 0.10 => 0.96 1729/1933
Parameter 1 step 2: 0.20 => 0.91 1290/1540
Parameter 1 step 3: 0.30 => 0.87 990/1184
Parameter 1 step 4: 0.40 => 0.83 840/1034
Parameter 1 step 5: 0.50 => 1.06 747/759
Parameter 1 step 6: 0.60 => 1.01 669/715
Parameter 1 step 7: 0.70 => 0.87 604/712
Parameter 1 step 8: 0.80 => 0.86 490/616
Selected p1[5] = 0.504 => 0.96

Parameter 2 step 1: 1.00 => 1.06 471/1035
Parameter 2 step 2: 1.50 => 1.08 553/953
Parameter 2 step 3: 2.00 => 1.08 599/907
Parameter 2 step 4: 2.50 => 1.07 646/860
Parameter 2 step 5: 3.00 => 1.06 676/830
Parameter 2 step 6: 3.50 => 1.07 725/781
Selected p2[3] = 2.000 => 1.07

Parameter 3 step 1: 1.00 => 0.97 883/623
Parameter 3 step 2: 1.50 => 0.99 806/700
Parameter 3 step 3: 2.00 => 1.02 755/751
Parameter 3 step 4: 2.50 => 1.06 711/795
Parameter 3 step 5: 3.00 => 1.04 657/849
Parameter 3 step 6: 3.50 => 1.06 622/884
Selected p3[5] = 3.01 => 1.05

EUR/USD: 0.504 2.000 3.01=> 1.112

Parameters stored in OptTest_1.par
The log displays the first walk-forward cycle (Walk[1]) with 3 parameters. The numbers after '=>' are the objective return value and the numbers of won and lost trades. The selected parameter value and its objective are interpolated between steps. The second-last line, beginning with "EUR/USD:", is the content added to the parameter file.   

Parameters:

default Default value of the parameter, returned when the PARAMETERS flag is not set in [Test] or [Trade] mode. This should be the estimated best value of the parameter, normally in the middle of its range. Must be >= 0.
min, max Parameter range, minimum and maximum values (>= 0). For optimizing negative parameter ranges, subtract an offset or add a minus sign to the optimize call. If both min and max are 0, the default value is stored in the parameter file.
step Optional step width. If at 0 or omitted, in Ascent optimization 10% is added to the parameter for every step; in Brute Force or Genetic optimization 1/10 of the min..max range is added. A positive step value is added at every step.A negative step value is interpreted as a percentage to be added, for instance -20 adds 20% per step (Ascent optimization only). Using a percentage gives a higher step resolution to smaller values at the begin of the range. For best results, select the step value so that every parameter has about 10..15 optimization steps. The recommended minimum number of steps is 5, the maximum number of steps (MAX_STEPS) can be found in trading.h.
bias Optional preference of low or high parameter values (Ascent optimization only). When 0 or omitted, select the optimal parameter. When > 0, prefer higher parameter values even when their rank is lower by the given bias in percent. When < 0, prefer lower parmeter values even when their rank is lower by abs(bias) in percent. Preferring values from the low or high range can make sense f.i. for setting a Stop as tight as possible even when its value is not optimal.

Returns

Current, default, or optimized parameter value, depending on training or test/trade mode.
   

Remarks:

Examples:

// trend trading with Moving Averages and optimized parameters
function run()
{
  set(PARAMETERS);
// for optimizing time periods, set the LookBack variable to the
// maximum possible value (here, TimeCycle 100 * TimeFactor 5)
  LookBack = 100*5;
var TimeCycle = optimize(30,10,100,5); var TimeFactor = optimize(3,1,5);
// allow 3% tolerance for preferring low stop distances Stop = ATR(10) * optimize(3,1,10,0.5,-3); vars Price = series(price(0)); vars MA1 = series(SMA(Price,TimeCycle)); vars MA2 = series(SMA(Price,TimeCycle*TimeFactor)); plot("MA1",*MA1,0,BLUE); plot("MA2",*MA2,0,BLUE); if(crossOver(MA1,MA2)) enterLong(); else if(crossUnder(MA1,MA2)) enterShort(); }

See also:

Training, TrainMode, loop, NumParameters, NumTrainCycles, Workshop 5, Statistics

► latest version online