Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

DataSnooping: SPA test, Reality Check, StepM and model confidence set - library for MetaTrader 5

Mohamed-amine Baddir
Mohamed-amine Baddir
I build Expert Advisors for MetaTrader 5, and I test them properly before you trade them.
What I do:
- Turn your strategy, TradingView script or written rules into a clean MT5 Expert Advisor
Views:
93
Rating:
(1)
Published:
Updated:
\MQL5\Include\DataSnooping\
DataSnooping.mqh (20.82 KB) view
\MQL5\Scripts\DataSnooping\
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

DataSnooping.mqh answers the question that follows every optimization: the best of many strategies (or of many parameter sets of one strategy) beat the benchmark, but was it better, or only the luckiest of the lot? It has Hansen's test for superior predictive ability (SPA), White's Reality Check, the Romano-Wolf StepM procedure that finds every strategy better than the benchmark, and the model confidence set of Hansen, Lunde and Nason. The bootstrap keeps the dependence of daily results (stationary bootstrap) and takes a seed. No DLL.

Usage

#include <DataSnooping\DataSnooping.mqh>

matrix returns;                            // one column per strategy (or parameter set), one row per day
vector benchmark;                          // returns of the benchmark on the same days (zeros: no position)
// ... fill returns and benchmark ...
CSuperiorAbility spa;
spa.SetData(returns, benchmark, true);     // true: higher is better
spa.Options(2000, 0.0, true, 1);           // draws, mean block (0: square root of the days), studentized, seed
if(spa.Compute())
  {
   double p = spa.PValue(1);               // consistent SPA p-value: is the best really better?
   int better[];
   int n = spa.StepM(0.05, better);        // all the strategies better than the benchmark at 5%
  }
CModelConfidenceSet mcs;
if(mcs.Compute(returns, true, 2000))
  {
   bool kept = mcs.Included(0, 0.1);       // is strategy 0 in the 90% set of the best?
  }

What is in it

  • CSuperiorAbility: the lower, consistent and upper p-values of the SPA test, the Reality Check (the upper p-value without studentization), critical values, the strategies above them, and StepM. Studentized (Hansen 2005: the best t statistic, with the long-run variances of the stationary bootstrap) or not.
  • CModelConfidenceSet: the p-value of every strategy and the set of the best at any size, with the 'R' statistic (pairs of strategies) or the 'max' statistic. The elimination keeps the largest pair of each draw in memory, so 200 strategies take 0.2 s.
  • CStationaryBootstrap: indices of the stationary bootstrap of Politis and Romano with a seed, and the means of the resamples from running sums (a whole block at a time).

A note on arch

The tests follow the Python package arch, and give the same p-values as arch when the same bootstrap draws are used. arch's SPA class has a studentize option, but in version 8.0 its computation does not divide by the standard deviations, so it returns the test without studentization whatever the option. Here, Options(..., studentize = false, ...) gives the same numbers as arch, and studentize = true gives Hansen's statistic, where a strategy with a high mean but very noisy results counts less than a steadier one.

Example

The script SnoopingExample.mq5 builds 100 moving average rules on a symbol (long while the fast average is above the slow one, flat otherwise, 10 fast and 10 slow lengths), or reads the returns of your own strategies from a CSV file, and compares the p-value of the best rule tested alone with the SPA, Reality Check, StepM and model confidence set results (picture 1). On US30 D1 the best rule has a t statistic of 1.72: alone, a p-value of 0.043; as the best of 100, an SPA p-value of 0.19 (picture 2).

Output of SnoopingExample.mq5

t statistics of the 100 rules

Picture 3: the t statistic of each rule, by fast and slow average. Neighbouring rules give similar numbers, because they are in the market on mostly the same days. The bootstrap of the SPA test and of the Reality Check keeps this dependence; a Bonferroni correction would ignore it and be far too strict.

t statistics of the 100 rules on a grid

Picture 4: the same best rule, SMA 5/160. Tested alone, its p-value is 0.043, below 5%. Taken as the best of the 100 rules that were tried, it is 0.244 (Reality Check) and 0.187 (SPA): nothing left to show that it beats staying out of the market.

p-values of the best rule

Checked

With the bootstrap indices drawn by arch 8.0 (stationary bootstrap, 1 000 draws) on three data sets (50 strategies of which 3 have a real edge, 200 correlated parameter sets without edge, the daily results of 9 sample Expert Advisors):

  • SPA without studentization: the lower, consistent and upper p-values identical to arch's, critical values within 2e-15, long-run variances identical, the same better strategies and the same StepM set;
  • SPA studentized: p-values identical to an independent numpy version of Hansen's test;
  • model confidence set, 'R' and 'max': the p-value of every strategy identical to arch's.

Files

DataSnooping.mqh goes to MQL5\Include\DataSnooping, SnoopingExample.mq5 to MQL5\Scripts\DataSnooping.

Basket Pair Closer Basket Pair Closer

Position manager for MT5 that closes the best winner together with the worst loser when their combined result reaches your minimum. Basket take profit and stop loss in money, magic filter, simple panel. Works with grid, martingale and manual trades. Open source code.

Cointegration: ADF, Engle-Granger and Johansen tests with a pair scanner Cointegration: ADF, Engle-Granger and Johansen tests with a pair scanner

Unit root and cointegration tests (ADF, Engle-Granger, Johansen) with MacKinnon p-values, hedge ratio, half-life and z-score of the spread, and a pair scanner.

Swap Markup Scanner Swap Markup Scanner

Shows the broker's hidden financing markup and the carry for every symbol, as annual % of notional.

Partial Close Multi TP Manager Partial Close Multi TP Manager

Partial close expert advisor for MetaTrader 5: closes a percent of the position at TP1, TP2, TP3, TP4 and TP5, moves the stop loss to breakeven and trails it. Works on manual trades and on positions of any EA, on any symbol, Gold (XAUUSD) included. Open source code.