Binomial_MetaBacktest.ex4 Binomial_MetaBacktest.mq4 EURUSD-M1-Setting.set
Report-Binomial-Distribution-EURUSD-M1.zip
In quantitative trading, strategies that combine technical indicators with statistical reasoning often outperform purely signal-driven systems. While many MetaTrader 4 (MQL4) traders rely on exponential moving averages (EMAs) for directional bias, few integrate probabilistic confidence models to adapt their position sizing dynamically.
This article explores a simple yet powerful approach:
n
tradesHow the binomial decision is made
Two modes:
UsePValueMethod = true
: compute , where is the number of wins in the last LookbackN
trades and (p_0` is a null probability (default 0.5). If this survival probability is ≥ DecisionThresholdP (default 0.70), we treat the recent window as “good” and increase next lot.
Interpretation: observing at least W
wins is relatively likely under the assumption of p_0. This is a heuristic — you can tune p0 and the threshold to be conservative or aggressive.
UsePValueMethod = false
: compute Beta posterior mean and compare to PosteriorThreshold
. This smooths the estimate for small N and avoids extreme swings. BetaAlphaPrior
/BetaBetaPrior
default to 1 (uniform).
Both are heuristics for adaptive sizing. You can easily swap or add more sophisticated logic (e.g., require both tests to pass).
Why a sliding window of closed trades (LookbackN)?
It gives a recent performance snapshot. Choose LookbackN
as a tradeoff: small N reacts quickly but is noisy; large N is stable but slow to react. Typical values: 20–100 depending on trade frequency.
What counts as a 'win'?
In this EA, a closed trade with total profit (profit + swap + commission) ≥ 0 is considered a win. You can change this criterion (e.g., require profit >= some positive threshold or use net returns percent).
Lot sizing policy
This EA resets to BaseLot
when the binomial test is not satisfied, and multiplies by LotMultiplier
when good
. Alternative strategies:
Gradual increase (increment rather than multiply).
Add decay (reduce lot gradually rather than full reset).
Cap total exposure by MaxOpenTrades
and MaxLot
— both included.
Numerical stability & PMF
PMF uses recurrence and computes tail sums via BinomialSurvival
which sums the smaller tail for numerical stability. This is robust for LookbackN
up to hundreds/thousands with double precision. If you use extremely large windows (e.g., >10k), switch to log-domain or normal approximation.
How closed-trades are processed
The EA marks all history tickets present at OnInit
as processed (so restarting the EA doesn't retroactively change sizing based on old data). It only processes newly closed trades after it starts. If you want to include historical trades for quicker bootstrap, remove or change that block in OnInit
.
Risk management
This EA is a demo of binomial-based sizing. In live systems ensure:
Maximum drawdown protections,
Overall portfolio exposure caps,
Equity/loss-based stop triggers,
Proper money management (Kelly-based or volatility sizing) if required.
Backtest & validation
Backtest to measure how quickly the system reacts to changing win rates and its effect on drawdown/returns.
Unit tests to verify BinomialPMFArray
sums to ~1, mean ~ n*p, survival matches direct sums for small n.
Extending the decision rule
Replace the simple multiplier with a sizing function that maps posterior mean (or p-value) into a continuous lot factor, e.g. lot = base * (1 + k*(postMean - 0.5))
clipped to [min, max]
.
Practical tuning suggestions
Conservative: LookbackN=50, DecisionP0=0.5, DecisionThresholdP=0.9, LotMultiplier=1.5
Aggressive: LookbackN=20, DecisionP0=0.5, DecisionThresholdP=0.65, LotMultiplier=2.0
After n
trades, we can compute:
W
= number of wins
L
= number of losses = n - W
p̂
= W / n (the empirical probability of winning)
With this empirical win probability, we can model the sequence of outcomes using the Binomial distribution:
This gives the probability of achieving k
wins out of n
trades given your observed success rate.
The Binomial distribution offers a statistical measure of confidence in your trading performance.
It tells you whether your recent success is likely random noise or a genuine statistical edge.
For example:
If your recent 20 trades yielded 15 wins (p̂ = 0.75),
The probability of achieving ≥15 wins by random chance (assuming a neutral 0.5 win rate) can be computed via the Binomial tail probability.
If this probability is low, it means your performance is unlikely due to chance — a potential real edge.
If it’s high, recent success may be random fluctuation — so you avoid overconfidence.
Thus, we can design a dynamic lot-sizing rule:
Statistical Confidence | Trading Action |
---|---|
Strong (e.g., P < 0.05) | Increase lot size by small factor |
Neutral (0.05 < P < 0.2) | Maintain current lot size |
Weak (P > 0.2) | Reduce lot size to preserve capital |
Below is a simplified yet fully functional version of this logic.
It uses EMA8/EMA21 signals, tracks trade outcomes, and adapts lot size using a Binomial-based confidence test.
This trading framework doesn’t rely on magical parameters or curve-fitting.
It’s a systematic method for adjusting trade exposure based on quantified evidence of an edge.
Advantages:
Mathematically grounded: uses the binomial model to measure confidence
Emotion-free scaling: increases or decreases lot size only when statistically justified
Self-adaptive: responds automatically to market regime shifts
Simple structure: easy to backtest, extend, and deploy
Keep HistoryTrades
between 20–50 for short-term learning.
Use fixed SL/TP to maintain binary outcomes.
Update the binomial probability after each closed trade.
Integrate BinomialTail()
checks into your performance analytics dashboard.
For large-scale systems, you can approximate using a normal approximation to save computation.
Integrating the Binomial distribution into a trading system bridges the gap between signal-based logic and statistical validation.
By pairing a simple EMA crossover with binomial confidence, traders gain a dynamic, evidence-based mechanism for risk scaling — entirely within MQL4.
This method embodies the philosophy of MetaBacktest.com:
“Backtesting is not just about finding signals — it’s about validating probability.”
Binomial_MetaBacktest.ex4 Binomial_MetaBacktest.mq4 EURUSD-M1-Setting.set
Report-Binomial-Distribution-EURUSD-M1.zip
2025-09-10 09:12:41
2025-09-13 20:46:41
2025-10-15 08:42:39
For the binomial test: using p0 = 0.5 is okay, but wouldn’t a custom prior (say 0.55) calibrated to your strategy be stronger?
Yup. Using beta priors or empirical p0 might boost responsiveness.
The binomial sizing logic is quite clever. But what if the strategy’s win rate shifts over time, won’t old trades bias the decision?
That’s why smoothing or a decayed weighting (more weight to recent trades) might help.