Hidden Markov Model
- Experts
-
Robert Davis Waweru Mugure
experienced algo trader .. 4 years of experience coding my own EAs and using them for myself. Found it wise to share some of my best EAs at a small token of appreciation .
also Washignton Accorded MECHATRONICS ENGINEER. - Versione: 4.2
- Attivazioni: 7
📊 Core Concept
This Expert Advisor combines Markov Chain probability analysis with ATR-based risk management to identify high-probability trading opportunities. It uses historical price patterns to predict market direction and executes trades with built-in protection mechanisms.
🚀 Key Advantages
1. Intelligent Market Prediction
-
Uses a Markov Transition Matrix to analyze price patterns over a rolling window
-
Calculates the probability of bullish/bearish continuation based on recent market behavior
-
Only executes trades when confidence exceeds your specified threshold (default: 55%)
2. Dual-Strategy Approach
-
Momentum Following: When strong trends are detected, trades in the direction of momentum
-
Mean Reversion (Fade): When probabilities suggest reversal, can trade against the prevailing move
-
Automatically adapts to changing market conditions
3. Dynamic Risk Architecture
-
ATR-based Stop Loss: Uses Average True Range to set stop-loss distances that adapt to market volatility
-
Risk Bracket System: Stop-loss placed as a separate pending order for superior execution control
-
Fixed Risk-to-Reward: Maintains a 1.5:1 reward ratio (adjustable) for consistent expectancy
4. Robust Safety Features
| Feature | Protection |
|---|---|
| Spread Gatekeeper | Halts trading when spread exceeds threshold |
| Time Decay Liquidation | Automatically closes positions exceeding maximum open time |
| Position Neutralization | Prevents hedging on netting accounts |
| Single Position Policy | Only one position at a time (reduces exposure) |
5. Live Performance HUD
Displays real-time information directly on your chart:
-
Current spread status
-
ATR value
-
Markov probability percentages
-
Net profit/loss
-
Win/loss statistics
⚙️ Input Parameters
Gatekeepers (Risk Controls)
| Parameter | Default | Description |
|---|---|---|
| maximum spread | 35 | Maximum allowable spread in points. Trades are blocked when spread exceeds this value to protect against unfavorable conditions. |
| Probability threshhold | 55.0% | Minimum confidence level required to execute a trade. Higher values = fewer but higher-quality signals. |
Execution Engine
| Parameter | Default | Description |
|---|---|---|
| InpLotSize | 0.01 | Fixed position size per trade (in lots). |
| InpWindow | 200 | Number of candles used to build the Markov transition matrix. Larger windows = more stable but slower adaptation. |
Risk Architecture
| Parameter | Default | Description |
|---|---|---|
| InpATRPeriod | 14 | Period used for ATR calculation (14 candles). |
| InpATRMultiplier | 1.5 | Multiplier applied to ATR to determine stop-loss distance. Higher = wider stops. |
| InpRewardRatio | 1.5 | Ratio of take-profit to stop-loss. TP = SL × RewardRatio. |
| InpMaxBarsOpen | 10 | Maximum number of 15-minute bars before automatic position closure (time decay). |
| InpMagicNumber | 8803 | Unique identifier for this EA's trades. Prevents interference with other EAs. |
🧠 How It Works
-
Every 5-minute candle close, the EA analyzes the last 200 candles
-
Builds a Markov matrix tracking transitions between bullish and bearish states
-
Calculates probabilities for the next candle direction based on current state
-
Checks gatekeepers: spread, existing positions, confidence threshold
-
Executes trade with market entry and separate stop-loss pending order
-
Monitors position: time decay, take-profit hits, pending order management
-
Displays live statistics in the chart's upper-left corner
💡 Best Use Cases
-
Timeframes: Optimized for 5-minute chart analysis (M5)
-
Instruments: Works best on major forex pairs with good liquidity
-
Conditions: Performs well in trending and ranging markets due to dual-strategy approach
-
Risk Preference: Suitable for traders who prefer fixed-ratio risk management
⚠️ Important Notes
-
Netting Accounts: Position management automatically closes opposite positions to maintain net-zero exposure
-
Pending Orders: Stop-loss is placed as a separate pending order for execution reliability
-
History Tracking: Performance statistics only track trades with the EA's Magic Number
-
No Martingale/Grid: Each trade is independent with fixed risk
