Guarda come scaricare robot di trading gratuitamente
Ci trovi su Telegram!
Unisciti alla nostra fan page
Script interessante?
Pubblica il link!
lasciare che altri lo valutino
Ti è piaciuto lo script? Provalo nel Terminale MetaTrader 5
Librerie

Copula: Gaussian, Student t, Clayton, Gumbel and Frank copulas with tail dependence - libreria per MetaTrader 5

Mohamed-amine Baddir
Mohamed-amine Baddir
  • MT5 developer: EA testing and risk tools al  Independent MQL5 developer
  • Marocco
  • 1701
I miei strumenti per MetaTrader 5 nel Market (ognuno si può provare gratis nel tester di strategia prima dell'acquisto):
- Prop Risk Guard: limiti di perdita giornaliera e drawdown per conti di prop firm
- TradeMirror: copiatore locale di operazioni tra conti MT5
Visualizzazioni:
174
Pubblicato:
\MQL5\Include\Copula\
Copula.mqh (19.53 KB) visualizza
\MQL5\Scripts\Copula\
Freelance MQL5 Hai bisogno di un robot o indicatore basato su questo codice? Ordinalo su Freelance Vai a Freelance

Copula.mqh measures how two series move together beyond their correlation, in particular on their worst and best days. It fits five bivariate copulas by maximum likelihood on the ranks of the data (Gaussian, Student t, Clayton, Gumbel and Frank), gives Kendall's tau, the tail dependence, the probability of a joint extreme day, the AIC to choose a family, the conditional probabilities used in copula pairs trading, and random draws. No DLL; the normal and t functions come from the standard library (Math\Stat).

Usage

#include <Copula\Copula.mqh>

double x[], y[];                     // returns of two symbols on the same bars
// ... fill x and y ...
double u[], v[];
CCopula::Ranks(x, u);                // pseudo-observations in (0, 1)
CCopula::Ranks(y, v);
CCopula c;
c.SetData(u, v);
if(c.Fit(COPULA_STUDENT))
  {
   double lower = c.LowerTail();      // limit of P(y crashes | x crashes)
   double joint = c.JointLower(0.05); // P(y in its worst 5% | x in its worst 5%)
   double aic   = c.AIC();
   double h     = c.H(u[ArraySize(u) - 1], v[ArraySize(v) - 1]);   // P(U <= u | V = v) for the last bar
  }

What is in it

  • Families: Gaussian (no tail dependence), Student t (the same in both tails), Clayton (lower tail: crashes together), Gumbel (upper tail), Frank (none). The t copula is fitted by the profile likelihood in its degrees of freedom (2.1 to 100).
  • Tau, LowerTail, UpperTail: Kendall's tau and tail dependence implied by the fitted copula; KendallTau of data (tau-b, with ties).
  • Cdf, JointLower, JointUpper: C(u, v) (closed form for the Archimedean families, integrated for the elliptical ones) and the probability that one series is in its worst (best) q when the other one is.
  • LogDensity, H (conditional distribution, the "mispricing index" of copula pairs trading), Sample (draws with a seed).

Example

The script CopulaReport.mq5 takes two symbols, fits the five copulas to their returns, ranks them by AIC and prints the tail dependence and the probability of a joint worst and best 5% day for each, next to what the data show, and the conditional probabilities of the last bar (picture 1). For gold and silver on D1 the Student t copula is the best; the Gaussian one, which has no tail dependence, puts the joint 5% days at 50% when the data show 67% (picture 2).

Output of CopulaReport.mq5

Ranks of the returns of gold and silver

Checked

Against an independent scipy version on four simulated samples (Gaussian, Student t, Clayton and Gumbel dependence) and on gold and silver H4 returns, 25 fits:

  • ranks and Kendall's tau identical to scipy; log densities and h functions within 1e-13;
  • the maximum likelihood of every family equal to scipy's optimum (within 1e-6 of log-likelihood), parameters within 5e-7;
  • C(u, v) within 1e-11 of scipy (Gaussian) and of the closed forms, within 2e-8 for the t copula (scipy's own t integration);
  • the Kendall's tau of 10 000 draws within 0.02 of the fitted value.

Files

Copula.mqh goes to MQL5\Include\Copula, CopulaReport.mq5 to MQL5\Scripts\Copula.

ChangePoint: Bayesian online change point detection ChangePoint: Bayesian online change point detection

Online detection of changes in the mean or volatility (Adams-MacKay): probability of a change in the last k bars, current segment length, forecast, and an indicator.

MeanReversion: Ornstein-Uhlenbeck fit and optimal trading bands MeanReversion: Ornstein-Uhlenbeck fit and optimal trading bands

Exact maximum likelihood of an Ornstein-Uhlenbeck spread and Bertram's optimal entry and exit levels for a given cost, with the expected length of a trade cycle.

Supply Demand Zones Supply Demand Zones

Supply and demand zones for Gold (XAUUSD), EURUSD and any symbol: zones from swing highs and lows sized by ATR, fresh or tested, merged, broken ones removed, alerts and 5 buffers for EAs. Open source code.

MACD Signals MACD Signals

Indicator edition for new platform.