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

Optimizes a strategy parameter for the current asset and algo, and returns the optimized parameter value. In [Train] mode the function performs an optimization process to find the best or most robust parameter value between min and max

Several optimization methods (Ascent, Genetic, Brute Force) 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 chart displays the number of winning trades (bright blue bars), losing trades (dark blue) and overall performance (red) of the parameter. The best or most robust parameter values are then selected and stored in a .par file.

The chart above is a typical result of an optimize call in [Train] mode with the Ascent algorithm. The strategy parameter to be optimized here is used for a buy/sell threshold. It varies from 0.200 to 3.00 in constant steps of 0.1. We have a small performance peak around 0.90 and the highest peak at 1.60. The best parameter value however is 1.70, with a performance of 2.7 and 125 winning trades. Although its adjacent bars are higher, the optimizer normally selects the center of the broad peak that represents not the most profitable, but the most robust parameter value (this behavior can be changed with TrainMode). Using the value of a very narrow peak or of a single line bears the danger of overfitting the strategy.

By using several optimize calls in the code, any number of parameters can be optimized at the same time. The more parameters are optimized, the more cycles are needed and the longer will it take. Dependent on the method, the best value of a parameter can affect the optimization of the next parameters. If the script trades multiple assets or algorithms and contains loop calls, a separate parameter set for 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, given by its minimum and maximum values (>= 0).
step Optional step width. When 0 or omitted, 10% is added to the parameter for every optimization step (Ascent optimization only; in Brute Force or Genetic optimization the range is divided in 10 equal steps). When a positive step value is given, add this constant value every step; when a negative step value is given, add its magnitude as a percentage every step (f.i. -20 adds 20% per step). Adding a percentage instead of a constant value gives a better step resolution 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(); }
// alternative objective function based on Calmar ratio
var objective()

// alternative objective function based on Sharpe ratio
var objective()
  if(!NumWinTotal && !NumLossTotal) return 0.;
  return ReturnMean/ReturnStdDev;

// alternative objective function for binary options
var objective()
  return ((var)(NumWinLong+NumWinShort))/max(1,NumLossLong+NumLossShort);

See also:

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

► latest version online