Regime Lab
- Библиотеки
-
Shipra Gupta
Я — Шипра Гупта, трейдер и разработчик количественных стратегий с сильной подготовкой в области математики, экономики и алгоритмических торговых систем. За годы работы я получила обширный опыт на рынках Forex, золота, индексов, сырьевых товаров и криптовалют, сосредоточившись на создании торговых - Версия: 1.0
RegimeLab – Hidden Markov Model & Market Regime Detection
Most EAs fail because they run the right logic in the wrong regime. RegimeLab tells your EA which regime it is in, using published statistics instead of another moving average.
Features
- Hidden Markov Model – 2-state Gaussian HMM fitted by Baum-Welch: probability of the turbulent state, state means, volatilities, persistence and expected duration.
- Hurst exponent – Rescaled-range estimate on any symbol/timeframe or on your own array.
- Variance ratio z-test – Lo-MacKinlay heteroskedasticity-robust test of momentum vs mean reversion.
- Efficiency ratio – Kaufman path efficiency from 0 (noise) to 1 (straight line).
- Volatility percentile – Current Wilder ATR ranked against its own history.
- Composite label – TREND_UP, TREND_DOWN, MEAN_REVERT, RANDOM or HIGH_VOL in one call.
Quick start
1. Buy and download the library. MetaTrader installs it to MQL5\Libraries\RegimeLab.ex5.
2. Paste the import block below at the top of your EA, indicator or script.
3. Call the functions.
void OnTick() { int r=RL_Regime(_Symbol,PERIOD_H1,500,1,0.70,1.96); if(r==RL_REGIME_HIGH_VOL) return; // stand aside if(r==RL_REGIME_TREND_UP || r==RL_REGIME_TREND_DOWN) RunTrend(r); if(r==RL_REGIME_MEAN_REVERT) RunMeanRevert(); }
Import block (copy into your code)
#define RL_OK 0 #define RL_ERR_PARAM 1 #define RL_ERR_DATA 2 #define RL_ERR_SYMBOL 3 #define RL_ERR_FIT 4 #define RL_REGIME_UNKNOWN 0 #define RL_REGIME_TREND_UP 1 #define RL_REGIME_TREND_DOWN 2 #define RL_REGIME_MEAN_REVERT 3 #define RL_REGIME_RANDOM 4 #define RL_REGIME_HIGH_VOL 5 #import "RegimeLab.ex5" int RL_LastError(); double RL_Hurst(const string symbol,const ENUM_TIMEFRAMES tf,const int bars,const int shift); double RL_HurstArray(const double &series[],const bool is_price); bool RL_VarianceRatio(const string symbol,const ENUM_TIMEFRAMES tf,const int bars,const int q, const int shift,double &vr,double &z); double RL_EfficiencyRatio(const string symbol,const ENUM_TIMEFRAMES tf,const int period,const int shift); double RL_VolPercentile(const string symbol,const ENUM_TIMEFRAMES tf,const int atr_period, const int lookback,const int shift); double RL_HMMFit(const string symbol,const ENUM_TIMEFRAMES tf,const int bars,const int shift, const int max_iter); bool RL_HMMParams(const int state,double &mean,double &stdev,double &stay_prob); double RL_HMMExpectedDuration(const int state); double RL_HMMLogLikelihood(); int RL_Regime(const string symbol,const ENUM_TIMEFRAMES tf,const int bars,const int shift, const double hivol_prob,const double z_crit); string RL_RegimeName(const int regime); #import
Notes
- Uses completed bars (shift ≥ 1): no repaint, no look-ahead.
- 64 to 20,000 bars per call; bounded for optimisation.
- Any symbol and timeframe, so multi-symbol and multi-timeframe filters fit in one EA.
Technical
- Pure MQL5. No DLL, no WebRequest, no file I/O.
- Every exported function validates its inputs. RL_LastError() returns the reason for any failed call.
- Works in the Strategy Tester and live, on any broker, symbol and account currency.
Past performance shown in screenshots is from historical data and does not guarantee future results.
