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

RegimeHMM: market regimes with a hidden Markov model - MetaTrader 5용 라이브러리

Mohamed-amine Baddir
Mohamed-amine Baddir
MetaTrader 5용 Expert Advisor(EA)를 개발하고, 실제 거래에 사용하기 전에 충분히 테스트합니다.
제가 제공하는 작업:
고객의 전략, TradingView 스크립트 또는 문서화된 규칙을 깔끔한 MT5 Expert Advisor로 구현
기존 EA 수정 및 문제 해결: % 기준 리스크 포지션 사이징, 트레일링 스톱, 세션 및 시간 필터, 스프레드 필터, 멀티 심볼 포트폴리오, 드로다운 보호 기능
조회수:
79
게시됨:
업데이트됨:
\MQL5\Include\RegimeHMM\
RegimeHMM.mqh (22.24 KB) 조회
\MQL5\Scripts\RegimeHMM\
MQL5 프리랜스 이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

RegimeHMM.mqh finds market regimes with a hidden Markov model: the returns (or any features) come from one of a few states, each with its own normal distribution, and the state follows a Markov chain. The class fits the model by Baum-Welch, gives the probability of each regime at every bar, the most likely path, the expected duration of each regime and the probabilities of the regimes in the coming periods. One or several features, full covariance, no DLL.

Usage

#include <RegimeHMM\RegimeHMM.mqh>

double returns[];                          // for example 100 * log(close[i] / close[i-1])
// ... fill returns ...
CGaussianHMM hmm;
if(hmm.SetData(returns) && hmm.Fit(2))     // 2 regimes, 10 starts; regime 0 is the calmer one
  {
   const int last = hmm.Observations() - 1;
   double pCalm = hmm.Filtered(last, 0);      // probability of the calm regime now, from the data up to now
   double vol1 = MathSqrt(hmm.Covariance(1)); // volatility in the other regime
   double days = hmm.ExpectedDuration(1);     // mean length of a stay in it
   vector next;
   hmm.Predict(5, next);                      // probabilities of the regimes in 5 periods
  }

For several features, pass a matrix with one row per bar and one column per feature to SetData.

Filtered or smoothed

Filtered(t, k) uses only the data up to bar t: it is what was known at that time, and the one to use in a backtest or an Expert Advisor. Posterior(t, k) uses all the data, before and after t: it draws cleaner regimes on a chart, but a test that trades on it sees the future. The same holds for the Viterbi path. The parameters themselves come from all the data: for a fair test, fit on the past and apply to the bars after it.

How it works

  • Forward-backward with scaling (no underflow on long series), Baum-Welch updates of the start, transition, mean and covariance parameters; a small floor (1e-6 of the variance of the data) keeps the covariances positive.
  • Starts by k-means++ with a seed on scaled features, several starts, the best likelihood kept. The states are then sorted by the variance of the first feature, so regime 0 is always the calmest.
  • Viterbi path in logarithms, forecasts of the state by powers of the transition matrix, long-run shares of time, AIC and BIC to choose the number of regimes.

Example

The script RegimeExample.mq5 fits 1 to 4 regimes to the returns of a symbol, keeps the best by BIC, and prints for each regime the return and volatility a year, the mean duration and the share of time, then the transition matrix, the probability of each regime now and in 1, 5 and 20 periods (picture 1). It can write the regime of every bar to a CSV file; picture 2 shows US30 with the regime known on each day.

Output of RegimeExample.mq5

US30 and its regimes

Picture 3: how sure the model is each day about the regime. These are the filtered probabilities, computed only from the data up to that day, so they can be used in an expert without look-ahead. The nervous regime dominates most of 2022 and returns in March and April 2025 and 2026; the crash regime takes over on only a few days, in June 2020 and from 4 to 11 April 2025.

Probability of each regime of US30m

Picture 4: the daily returns grouped by the regime of the most likely path, one dot per day. Calm: 1321 days with a standard deviation of 0.54%. Nervous: 666 days, 1.19%. Crash: 13 days, 4.03%, mostly losses. These are three different distributions, not one distribution with a few outliers.

Daily returns of US30m in each regime

Checked

  • On a model of 3 states, 2 features and 7 bars, every one of the 2 187 paths of states was enumerated: the likelihood, the smoothed and filtered probabilities, the Viterbi path and its probability, and the forecast were equal within 3e-15.
  • From the same start, 30 Baum-Welch steps were compared with an independent Python version computed in logarithms, on 2 simulated and 3 market series with 2 and 3 states: log-likelihoods within 1e-12 and parameters within 3e-11 after 30 steps, and the likelihood never decreased.
  • Every full fit ends at a fixed point of the algorithm: one more step adds less than 1e-5 to the log-likelihood. On simulated data with volatilities of 0.6 and 2.0 the fit found 0.595 and 2.009.

A fit of 2 000 bars with 10 starts takes 20 to 160 ms with 2 regimes, up to about 1.2 s with 3.

Files

RegimeHMM.mqh goes to MQL5\Include\RegimeHMM, RegimeExample.mq5 to MQL5\Scripts\RegimeHMM.

CME equity index futures holiday calendar CME equity index futures holiday calendar

Holidays and early halts of the CME equity index futures (S&P 500, Nasdaq-100, Dow, Russell 2000, E-mini and Micro), computed from the US holiday rules for any year, with a script that checks the calendar against your broker's own M1 history.

VpsToolkit - safe order send and restart-safe state for VPS EAs VpsToolkit - safe order send and restart-safe state for VPS EAs

Include for EAs running 24/7 on an MQL5 VPS or PC: run-where switch, order send with OK/FAIL/UNKNOWN result (no duplicate orders after a timeout), restart-safe counters, positions closed while the EA was off, append-only log and optional Telegram. No trading logic inside.

Kalman: exact diffuse Kalman filter, local level and time-varying hedge ratio Kalman: exact diffuse Kalman filter, local level and time-varying hedge ratio

Kalman filter and smoother with an exact diffuse start, missing values and maximum likelihood: local level, local linear trend and a regression whose hedge ratio drifts.

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.