• Overview
  • Reviews
  • Comments (15)
  • What's new


WalkForwardOptimizer library allows you to perform rolling and cluster walk-forward optimization of expert advisers (EA) in MetaTrader 4.

To use the library include its header file WalkForwardOptimizer.mqh into your EA source code, add call provided functions as appropriate.

Once the library is embedded into EA, you may start optimization according to the procedure described in the User guide. When it's finished, intermediate results are saved into a csv-file and some special global variables. Then you can view and analyse the results by means of accompanying script WalkForwardReporter, which generates comprehensible reports as html-pages. The script is free.

Header file WalkForwardOptimizer.mqh

#define DAYS_PER_WEEK    7
#define DAYS_PER_MONTH   30

#define SEC_PER_DAY     (60*60*24)
#define SEC_PER_YEAR    (SEC_PER_MONTH*12)

#define CUSTOM_DAYS     -1

enum WFO_TIME_PERIOD {none = 0, year = DAYS_PER_YEAR, halfyear = DAYS_PER_HALF, quarter = DAYS_PER_QUARTER, month = DAYS_PER_MONTH, week = DAYS_PER_WEEK, day = 1, custom = CUSTOM_DAYS};

enum WFO_ESTIMATION_METHOD {wfo_built_in_loose, wfo_built_in_strict, wfo_profit, wfo_sharpe, wfo_pf, wfo_drawdown, wfo_profit_by_drawdown, wfo_profit_trades_by_drawdown, wfo_average, wfo_expression};

extern WFO_TIME_PERIOD wfo_windowSize = year;
extern int wfo_customWindowSizeDays = 0;
extern WFO_TIME_PERIOD wfo_stepSize = quarter;
extern int wfo_customStepSizePercent = 0;
extern int wfo_stepOffset = 0;
extern string wfo_outputFile = "";
extern WFO_ESTIMATION_METHOD wfo_estimation = wfo_built_in_loose;
extern string wfo_formula = "";

#import "WalkForwardOptimizer.ex4"
void wfo_setEstimationMethod(WFO_ESTIMATION_METHOD estimation, string formula);
void wfo_setHeader(string s);
void wfo_setPFmax(double max);
void wfo_setGVAutomaticCleanup(bool b);
void wfo_setCleanUpTimeout(int seconds);
int wfo_OnInit(WFO_TIME_PERIOD optimizeOn, WFO_TIME_PERIOD optimizeStep, int optimizeStepOffset, int optimizeCustomW, int optimizeCustomS, string optimizeLog);
int wfo_OnTick();
double wfo_OnTester(string payload = "");
void wfo_setCloseTradesOnSeparationLine(bool b);

Example of usage in your source code

#include <WalkForwardOptimizer.mqh>


int OnInit()

  wfo_setEstimationMethod(wfo_estimation,wfo_formula); // wfo_built_in_loose by default
  wfo_setHeader("EnvelopeRange,EnvelopeLength"); // "Payload" by default
  wfo_setPFmax(100); // DBL_MAX by default
  // can be set to true, only if genetics not used
  // wfo_setGVAutomaticCleanup(true); // false by default
  // wfo_setCloseTradesOnSeparationLine(true); // false by default

  // this is the only required call in OnInit, all parameters come from the header
  int r = wfo_OnInit(wfo_windowSize, wfo_stepSize, wfo_stepOffset, wfo_customWindowSizeDays, wfo_customStepSizePercent, wfo_outputFile);

double OnTester()
  // the passed string with optimizable work parameters of EA should match specified header in wfo_setHeader
  // the parameter is optional, you may have EA without parameters
  // the call to wfo_OnTester is required
  return wfo_OnTester(DoubleToStr(EnvelopeRange, 1) + "," + IntegerToString(EnvelopeLength));

void OnTick()
  int wfo = wfo_OnTick(); // required in OnTick
  if(wfo == -1) // this tick is before optimization window
  else if(wfo == +1) // this tick is after optimization window and forward test
  // your actual code goes here

No reviews
Version 1.5 2019.08.22
Fixed a bug with datetime increments overflow, which could lead to multiple empty forward passes with zero dates 1970.01.01.
Version 1.4 2017.06.13
Fixed an overflow error in calculation of forward steps for window sizes larger than approximately 2 years.
Version 1.3 2017.05.23
Performance is improved by means of early drop off of those optimization passes, for which in-sample data overlaps with ending date of the tester. Such passes will fail in OnInit with INIT_PARAMETERS_INCORRECT errors. This is an intended behavior.
Version 1.2 2016.08.29
New function void wfo_setCleanUpTimeout(int seconds) added, allowing you to simplify automatic deletion of old csv-files in Tester/Files and WF_-global variables, the presence of which could lead to incorrect data state after next optimization run. For details - see documentation and Comments section.

Processing of optimization windows which runs out of ending date of test period is fixed.