FxMath_RFConsensus

EA Settings Documentation — v4.0 · MQL4 / MQL5 · DLL-Free
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.

At a glance: The EA collects price data, extracts 19 technical features (RSI, MACD, Bollinger Bands, moving averages, volatility, etc.), and labels each bar as UP or NOT-UP based on future price movement relative to ATR. It trains three separate Random Forest models on three timeframes, then combines their predictions via weighted voting to decide whether to BUY, SELL, or stay neutral.

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:

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:

All trades are filtered by:

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

ParameterTypeDefaultRangeDescription
InpNumTreesint205–50Number of decision trees. More trees = more stable predictions, slower training.
InpMaxDepthint83–15Maximum tree depth. Deeper trees capture more complex patterns but risk overfitting.
InpMinSamplesint52–50Minimum samples per leaf. Higher values prevent overfitting.
InpRFRetrainBarsint20050–1000Retrain the model every N bars on the main timeframe.
InpHorizon1int31–50Number of bars ahead to look when generating training labels.

🔧 Feature Settings

ParameterTypeDefaultRangeDescription
InpFeatLookbackint205–100Lookback period for the price-range feature (position in recent range).
InpFeatBBPeriodint205–50Bollinger Bands period for the %B feature.
InpFeatRSIPeriodint145–30RSI period.
InpFeatMACDFastint125–30MACD fast EMA period.
InpFeatMACDSlowint2610–60MACD slow EMA period.
InpFeatMACDSignalint95–20MACD signal line period.
InpTrainBarsint500100–2000Number of historical bars used to train the model.

⏱ Multi-Timeframe Voting

ParameterTypeDefaultRangeDescription
InpTF1enumM1M1–MNFirst timeframe for model training and prediction.
InpTF2enumM2M1–MNSecond timeframe.
InpTF3enumM3M1–MNThird timeframe.
InpMainTFenumM2M1–MNMain timeframe used for SL/TP calculation and new-bar triggers.
InpVoteMinint21–3Minimum number of TFs that must agree to produce a trade signal.

💰 Trading Thresholds

ParameterTypeDefaultRangeDescription
InpBuyThresholddouble0.550.50–0.90Buy signal if UP probability exceeds this value.
InpSellThresholddouble0.450.10–0.50Sell signal if UP probability is below this value.
InpMinConfidencedouble0.200.05–0.50Minimum confidence required for any signal (0 = random, 1 = certain).
InpMaxSpreadint30001–10000Maximum allowed spread (in points) to enter a trade. Higher value = trades in higher spread.

📐 Lot Size & Money Management

ParameterTypeDefaultRangeDescription
InpLotSizedouble0.010.01–100Fixed lot size when money management is off.
InpUseMMboolfalseEnable risk-based position sizing (% of account equity).
InpRiskPercentdouble1.00.1–10.0Percentage of equity to risk per trade (only when MM is on).

🛑 Stop-Loss & Take-Profit

ParameterTypeDefaultRangeDescription
InpATRPeriodint145–50ATR period for SL/TP distance calculation.
InpSLMultiplierdouble1.00.5–10.0Stop-loss = ATR × multiplier. 1.0 = 1× ATR from entry.
InpTPMultiplierdouble2.00.5–10.0Take-profit = ATR × multiplier. 2.0 = 2× ATR from entry.

🔁 Trailing Stop & Break-Even

ParameterTypeDefaultRangeDescription
InpTrailEnableboolfalseEnable trailing stop.
InpTrailTriggerPctdouble50.010–100Start trailing when price reaches X% of TP distance.
InpTrailDistPctdouble25.05–100Trailing distance = Y% of TP distance.
InpBEEnableboolfalseEnable break-even (move SL to entry price).
InpBETriggerPctdouble30.010–100Move SL to entry when price reaches X% of TP distance.

⏰ Time Filter

ParameterTypeDefaultRangeDescription
InpTimeFilterEnableboolfalseEnable trading session time filter based on server time.
InpStartHourint80–23Session start hour (server time).
InpEndHourint200–23Session end hour (server time). Supports overnight sessions (e.g., start=22, end=6).

🖥 Display & Identification

ParameterTypeDefaultRangeDescription
InpShowOnChartbooltrueShow the signal information panel on the chart.
InpMagicNumberint2024051–999999Unique magic number to identify this EA's orders.
InpTradeCommentstring"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:

Tip: Start with conservative settings (20 trees, depth 8, 2-of-3 vote minimum, 1% risk per trade) and observe performance on a demo account before going live. Adjust InpBuyThreshold and InpSellThreshold to control how aggressive the signals are.

FxMath_RFConsensus v4.0 · MQL4 / MQL5 · EA Settings Documentation