거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Twitter에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
라이브러리

Copula: Gaussian, Student t, Clayton, Gumbel and Frank copulas with tail dependence - MetaTrader 5용 라이브러리

Mohamed-amine Baddir
Mohamed-amine Baddir
  • MT5 developer: EA testing and risk tools 에  Independent MQL5 developer
  • 모로코
  • 1701
Market의 MetaTrader 5 도구 (구매 전에 전략 테스터에서 무료로 사용해 볼 수 있습니다):
- Prop Risk Guard: 프롭 펌 계좌용 일일 손실 및 드로다운 한도
- TradeMirror: MT5 계좌 간 로컬 거래 복사기
- Portfolio Allocator, Portfolio Backtest Combiner: 여러 EA의 랏 배분과 합산 결과
- Backtest vs Live, Trade Excursion Analyzer: 실거래 결과가 백테스트와 다른 이유, 손절과 청산 위치
조회수:
214
게시됨:
\MQL5\Include\Copula\
Copula.mqh (19.53 KB) 조회
\MQL5\Scripts\Copula\
MQL5 프리랜스 이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

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.

Daily Loss Guard Daily Loss Guard

Closes all trades when your daily loss reaches your limit.