Walk-forward optimization library for MetaTrader: Estimator formula

15 August 2016, 19:42
Stanislav Korotky
0
140

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

Estimator formula reference

Expressions for performance estimator can contain the following variables:

NP - net profit

GP - gross profit

GL - gross loss

PF - profit factor

SHARPE - sharpe 

TRADES - number of trades

DDMAX - maximum drawdown

DDREL - maximum relative drawdown

WIN - number of winning trades

LOSS - number of losing trades

DEPOSIT - initial balance

AR - average return, i.e. average % of return on a deal (profit/loss amount divided by current balance, averaged on all trades) - this is a measure of balance curve slope, used in sharpe (MT5, version 1.6+)

STDEV - standard deviation of balance curve (MT5, version 1.6+)

All mathematical and logical operators are supported, as well as common functions such as abs, exp, max, min, pow, sqrt. Here is an example of expression for modified PROC (Pessimistic Return on Capital):

(GP * (1 - 1/sqrt(WIN + 1)) + GL * (1 + 1/sqrt(LOSS + 1))) + NP / TRADES


Starting from version 1.6 for MT5 the following option is available.

If required calculations can not be expressed with a formula (for example, contains logical conditions), one may implement a custom callback of type:

typedef double (*FUNCPTR_WFO_CUSTOM)(const datetime startDate, const datetime splitDate, const wfo_string2double_map &map);

in the expert adviser and pass it to the library via wfo_setCustomPerformanceMeter(FUNCPTR_WFO_CUSTOM funcptr). Then WFO will call it from OnTester with the dates and the map filled with actual data. The function should return a custom value for OnTester (TesterStatistics can not be used inside!).

The map is defined in the library as follows:
/**
 * Optional Custom Performance Meter support (introduced in version 1.6)
 *
 * Built-in type to hold a pair of estimator [name,value]
 */
struct wfo_string2double_pair
{
  string key;
  double value;
};

/**
 * Built-in container of [name,value] pairs.
 * The map is returned to EA as output parameter of FUNCPTR_WFO_CUSTOM function (see below)
 * It can contain predefined estimators (such as PF, SHARPE, etc., see formula description in the docs)
 */
class wfo_string2double_map
{
  protected:
    wfo_string2double_pair array[];
  
  public:
    /**
     * Returns size of the map
     */
    int size() const
    {
      return ArraySize(array);
    }
    
    /**
     * Returns key name by index
     */
    string operator[](const int i) const
    {
      return array[i].key;
    }
    
    /**
     * Returns value by key name
     * If given key does not exist, returns DBL_EPSILON
     */
    double operator[](const string k) const
    {
      int i, n = ArraySize(array);
      for(i = 0; i < n; ++i)
      {
        if(array[i].key == k) return array[i].value;
      }
      return DBL_EPSILON;
    }
    
};

The list of supported names of the keys in the map corresponds to the variables names shown above.

Share it with friends: