Walk-forward optimization library for MetaTrader: Functions

15 August 2016, 19:42
Stanislav Korotky

This post is a part of documentation of WalkForwardOptimizer library for MetaTrader4/5 - here is the table of contents.


void wfo_setEstimationMethod(WFO_ESTIMATION_METHOD estimation, string formula)

selects a method to estimate EA performance; available options: wfo_built_in_loose (default) and wfo_built_in_strict - composite indicators comprising sharpe, profit factor, profit amount and number of trades, wfo_profit - net profit amount, wfo_sharpe - sharpe ratio, wfo_pf - profit factor, wfo_drawdown - inversed drawdown (100 - DD) calculated on balance curve, wfo_profit_by_drawdown - profit amount divided by drawdown, wfo_profit_trades_by_drawdown - profit amount multiplied by number of trades and divided by drawdown, wfo_average - average profit of a single trade, wfo_expression - custom formula for performance estimator, specified in formula; the function is optional, should be called in OnInit;

void wfo_setHeader(string s) (MT4 only; MT5 provides names of parameters for the header automatically)

assigns a string, which will be outputted to resulting CSV file as ending part of header with work parameters of EA; by default - "Payload"; the function is optional, should be called in OnInit;

void wfo_setPFmax(double max)

assigns a maximal value for profit factor for cases when losses are missing; by default - DBL_MAX; the function is optional, should be called in OnInit;

void wfo_setGVAutomaticCleanup(bool b) (MT4 only; in MT5 the global variables are cleared at WF optimization startup automatically)

sets a flag for automatic deletion of existing global variables with "WF_" prefix at start-up of new optimization; by default - false; can be set to true, only if you do not use genetic algorithm; the function is optional, should be called in OnInit;

int wfo_OnInit(WFO_TIME_PERIOD optimizeOn, WFO_TIME_PERIOD optimizeStep, int optimizeStepOffset, int optimizeCustomW, int optimizeCustomS, string optimizeLog) (MT4)

int wfo_OnInit(WFO_TIME_PERIOD optimizeOn, WFO_TIME_PERIOD optimizeStep, int optimizeStepOffset, int optimizeCustomW, int optimizeCustomS) (MT5)

OnInit event handler; passes all required data into the library: window size, forward step, rolling forward step number, custom window size in days, custom step size in percent of window, name of csv-file for results; must be called in OnInit; returns 0 on success, or error code on error, for example, INIT_PARAMETERS_INCORRECT if passed parameters are incorrect;

int wfo_OnTick()

OnTick event handler; manages EA optimization inside window and testing on forward step; must be called in OnTick; returns 0 if current tick is inside moving optimization window and EA is allowed to trade; returns -1 or +1 if current tick is outside the window and trading should be skipped; -1 means that trading window is not yet started, +1 means that the trading window is already ended;

double wfo_OnTester(string payload = "") (MT4)

double wfo_OnTester() (MT5)

OnTester event handler; manages EA optimization by selected custom estimator; must be called in OnTester; the optional parameter can send arbitrary stringified parameters of EA to the library, so that they will be saved into resulting csv-file in the colums corresponding to the headers sent via wfo_setHeader function; returns the estimator value to be passed back to the tester; MT5 provides the values of parameters automatically;

void wfo_setCloseTradesOnSeparationLine(bool b)

sets a flag to close all market and pending orders at the moment of crossing the boundary between optimization window and forward test; by default - false; the function is optional, should be called in OnInit;

void wfo_setCleanUpTimeout(int seconds) (ver.1.2+, MT4 only; in MT5 all the stuff is cleared automatically at every WF optimization startup)

assigns a timeout in seconds; when new optimization starts and requested csv-file does already exist with modification time older for more than given 'seconds', the file is deleted; if WF_-global variables are marked by a timestamp which more than given 'seconds' in past, the variables are also deleted; by default (if function is not called), timeout is 0, and the feature is disabled; consider setting the timeout to a value, which is deliberately larger than an average time of a single tester pass for your EA in selected mode (such as open prices, ticks, etc). Usually 60 seconds is ok, but then the feature will work only if you have 1 or more minutes between successive optimizations. The function should be called from OnInit handler.

void wfo_OnTesterPass() (MT5 only)

OnTesterPass event handler.

void wfo_OnTesterInit(string optimizeLog) (MT5 only)

OnTesterInit event handler; provides a name of csv-file with results.

void wfo_OnTesterDeinit() (MT5 only)

OnTesterDeinit event handler.

void wfo_setCustomPerformanceMeter(FUNCPTR_WFO_CUSTOM funcptr) (MT5, version 1.6+)

optionally assigns a custom callback to calculate performance estimator, defined as a function of type FUNCPTR_WFO_CUSTOM in the expert source code; it's useful in cases when required performance mark is too complex and can not be expressed via formula in wfo_setEstimationMethod; find details in the formulae description.

Share it with friends: