• Overview
  • Reviews (1)
  • Comments (73)
  • What's new

WalkForwardOptimizer MT5

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

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. Also a comprehensible report is generated as HTML page.

Warning! Do not pause optimization process. Clicking Start button (even if it resumes suspended optimization) will rewrite WF results from scratch. MQL5 API does not allow to distinguish new optimization from resumed old one.

WalkForwardOptimizer.mqh Header File

#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};

#import "WalkForwardOptimizer.ex5"
void wfo_setEstimationMethod(WFO_ESTIMATION_METHOD estimation, string formula);
void wfo_setPFmax(double max);
void wfo_setCloseTradesOnSeparationLine(bool b);
void wfo_OnTesterPass();
int wfo_OnInit(WFO_TIME_PERIOD optimizeOn, WFO_TIME_PERIOD optimizeStep, int optimizeStepOffset, int optimizeCustomW, int optimizeCustomS);
int wfo_OnTick();
double wfo_OnTester();
void wfo_OnTesterInit(string optimizeLog);
void wfo_OnTesterDeinit();

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

Example of Usage

#include <WalkForwardOptimizer.mqh>


int OnInit(void)
  // your actual code goes here

  wfo_setEstimationMethod(wfo_estimation, wfo_formula); // wfo_built_in_loose by default
  wfo_setPFmax(100); // DBL_MAX 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);

void OnTesterInit()
  wfo_OnTesterInit(wfo_outputFile); // required

void OnTesterDeinit()
  wfo_OnTesterDeinit(); // required

void OnTesterPass()
  wfo_OnTesterPass(); // required

double OnTester()
  return wfo_OnTester(); // required

void OnTick(void)
  int wfo = wfo_OnTick();
  if(wfo == -1)
    // can do some non-trading stuff, such as gathering bar or ticks statistics
  else if(wfo == +1)
    // can do some non-trading stuff

  // your actual code goes here
Winsor Hoang
Winsor Hoang 2017.05.23 21:54 

This is a must tool for all system developers. I waited 5 years for someone at Metaquotes to develop a Walk Forward Optimization. Stanislav is a godsend. He managed to create this WFO library utilizing cloud computing. Finally, we have a similar system development tool compared to TradeStation, Multicharts, NinjaTrader and etc. I received a great product and responsive support from the Developer. I highly endorse this product.

Version 1.7 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.6 2017.12.19
Improvement: New function wfo_setCustomPerformanceMeter(FUNCPTR_WFO_CUSTOM funcptr) is added. It allows you to pass a reference to your special custom callback into the library. This callback function will be called by the library to get your custom trade efficiency mark (OnTester analogue). This can be handy if the existing approach with formula assigned via wfo_setEstimationMethod(WFO_ESTIMATION_METHOD estimation, string formula) is not sufficient for you. In the expert code one should implement a function of type FUNCPTR_WFO_CUSTOM (consult with the documentation for details).
Version 1.5 2017.06.12
Bugfix: an overflow in calculation of forward steps for large window sizes (larger than approximately two years).
Version 1.4 2017.06.01
Improvement: if optimization window size or forward step size is constant, cluster report is shown as a single table, which contains all performance indicators line by line.
Version 1.3 2017.05.31
Fixed an error in calculation of the variance.