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

Optimizes a 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 displayxed. 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.

By using several optimize calls in the code, any number of parameters can be optimized. 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 do not optimize more than 3 or 4 parameters for a trade entry signal. 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/alfo combination in each loop is optimized.


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). If both 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 width is added at every step; a negative step value is interpreted as a percentage to be added (f.i. -20 adds 20% per step). 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.


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





// trend trading with Moving Averages and optimized parameters
function run()
// 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