FxMath_RFConsensus
Random Forest Expert Advisor with Multi-Timeframe Consensus Voting
How It Works
FxMath_RFConsensus is a fully self-contained Expert Advisor that trains its own Random Forest machine learning model directly inside MetaTrader — no external DLLs, Python, or APIs required.
Random Forest
A Random Forest is an ensemble of many decision trees. Each tree is trained on a random 80% sample of the data (with replacement — bagging), and at each split only a random subset of features are considered. This creates diverse trees that collectively produce more stable and accurate predictions than any single tree.
The EA uses Gini impurity as the splitting criterion:
Gini = 1 − (p_UP² + p_NOT_UP²)
The final output is the average probability across all trees (default: 20 trees).
A confidence score is derived as
|probability − 0.5| × 2, where 0 = random and 1 = absolute certainty.
Multi-Timeframe Voting
Three independent Random Forest models run on three configurable timeframes (e.g. M1, M5, M15). Each predicts the probability of an UP move. The EA combines them into a single vote:
- BUY when enough TFs agree the UP probability exceeds
InpBuyThreshold - SELL when enough TFs agree the UP probability is below
InpSellThreshold - NEUTRAL otherwise (no trade)
The voting weights each TF's prediction by its confidence, so a high-confidence
model has more influence. The InpVoteMin setting controls how many
TFs must agree (default: 2 of 3).
Trading Logic
The EA maintains at most one position at a time. When a new signal arrives:
- No position → open a new trade at market
- Same direction → do nothing
- Opposite direction → close existing position and open a new one (reversal)
All trades are filtered by:
- Spread check — skips trading during high-spread periods
- Time filter — optional trading hours restriction
- Minimum confidence — vote confidence must exceed threshold
Stop-loss and take-profit are calculated from ATR on the main timeframe. A money management mode can size positions as a fixed percentage of equity risk. Trailing stop and break-even are configurable and update on every tick.
Settings Reference
📊 Random Forest
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpNumTrees | int | 20 | 5–50 | Number of decision trees. More trees = more stable predictions, slower training. |
| InpMaxDepth | int | 8 | 3–15 | Maximum tree depth. Deeper trees capture more complex patterns but risk overfitting. |
| InpMinSamples | int | 5 | 2–50 | Minimum samples per leaf. Higher values prevent overfitting. |
| InpRFRetrainBars | int | 200 | 50–1000 | Retrain the model every N bars on the main timeframe. |
| InpHorizon1 | int | 3 | 1–50 | Number of bars ahead to look when generating training labels. |
🔧 Feature Settings
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpFeatLookback | int | 20 | 5–100 | Lookback period for the price-range feature (position in recent range). |
| InpFeatBBPeriod | int | 20 | 5–50 | Bollinger Bands period for the %B feature. |
| InpFeatRSIPeriod | int | 14 | 5–30 | RSI period. |
| InpFeatMACDFast | int | 12 | 5–30 | MACD fast EMA period. |
| InpFeatMACDSlow | int | 26 | 10–60 | MACD slow EMA period. |
| InpFeatMACDSignal | int | 9 | 5–20 | MACD signal line period. |
| InpTrainBars | int | 500 | 100–2000 | Number of historical bars used to train the model. |
⏱ Multi-Timeframe Voting
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpTF1 | enum | M1 | M1–MN | First timeframe for model training and prediction. |
| InpTF2 | enum | M2 | M1–MN | Second timeframe. |
| InpTF3 | enum | M3 | M1–MN | Third timeframe. |
| InpMainTF | enum | M2 | M1–MN | Main timeframe used for SL/TP calculation and new-bar triggers. |
| InpVoteMin | int | 2 | 1–3 | Minimum number of TFs that must agree to produce a trade signal. |
💰 Trading Thresholds
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpBuyThreshold | double | 0.55 | 0.50–0.90 | Buy signal if UP probability exceeds this value. |
| InpSellThreshold | double | 0.45 | 0.10–0.50 | Sell signal if UP probability is below this value. |
| InpMinConfidence | double | 0.20 | 0.05–0.50 | Minimum confidence required for any signal (0 = random, 1 = certain). |
| InpMaxSpread | int | 3000 | 1–10000 | Maximum allowed spread (in points) to enter a trade. Higher value = trades in higher spread. |
📐 Lot Size & Money Management
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpLotSize | double | 0.01 | 0.01–100 | Fixed lot size when money management is off. |
| InpUseMM | bool | false | — | Enable risk-based position sizing (% of account equity). |
| InpRiskPercent | double | 1.0 | 0.1–10.0 | Percentage of equity to risk per trade (only when MM is on). |
🛑 Stop-Loss & Take-Profit
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpATRPeriod | int | 14 | 5–50 | ATR period for SL/TP distance calculation. |
| InpSLMultiplier | double | 1.0 | 0.5–10.0 | Stop-loss = ATR × multiplier. 1.0 = 1× ATR from entry. |
| InpTPMultiplier | double | 2.0 | 0.5–10.0 | Take-profit = ATR × multiplier. 2.0 = 2× ATR from entry. |
🔁 Trailing Stop & Break-Even
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpTrailEnable | bool | false | — | Enable trailing stop. |
| InpTrailTriggerPct | double | 50.0 | 10–100 | Start trailing when price reaches X% of TP distance. |
| InpTrailDistPct | double | 25.0 | 5–100 | Trailing distance = Y% of TP distance. |
| InpBEEnable | bool | false | — | Enable break-even (move SL to entry price). |
| InpBETriggerPct | double | 30.0 | 10–100 | Move SL to entry when price reaches X% of TP distance. |
⏰ Time Filter
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpTimeFilterEnable | bool | false | — | Enable trading session time filter based on server time. |
| InpStartHour | int | 8 | 0–23 | Session start hour (server time). |
| InpEndHour | int | 20 | 0–23 | Session end hour (server time). Supports overnight sessions (e.g., start=22, end=6). |
🖥 Display & Identification
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
| InpShowOnChart | bool | true | — | Show the signal information panel on the chart. |
| InpMagicNumber | int | 202405 | 1–999999 | Unique magic number to identify this EA's orders. |
| InpTradeComment | string | "RFConsensus" | — | Base order comment prefix appended to all trades. Useful for identifying trades from this EA in the account history. |
Chart Panel
When InpShowOnChart = true, the EA draws an information panel on the
chart that updates every new bar:
- Vote result — BUY / SELL / NEUTRAL with agreement count
- Per-TF predictions — probability and confidence for each timeframe
- Entry / SL / TP levels
- Current position — direction and floating profit
InpBuyThreshold and InpSellThreshold
to control how aggressive the signals are.
FxMath_RFConsensus v4.0 · MQL4 / MQL5 · EA Settings Documentation