Risk Of Ruin Estimator
Risk of ruin answers a question every position-sizing decision quietly depends on: given a system's edge and how much of the account is risked per trade, what is the actual probability of losing it all before the edge has time to play out? A system can have a genuinely positive expectancy and still carry an uncomfortably high risk of ruin if the per-trade risk fraction is too aggressive for how volatile the edge is - and that number is worth knowing before capital is committed, not after a drawdown answers the question the hard way. This library computes it three ways. RiskOfRuinFeller is the classical gambler's ruin result for a symmetric 1R-for-1R payoff: (q/p)^N, where N is starting capital expressed in whole risk units. RiskOfRuinDiffusion extends this to an asymmetric payoff ratio using the standard normal-approximation result from ruin theory - treating the cumulative R-multiple outcome as a random walk with drift and matching variance, giving a closed-form exp(-2*mu*N/sigma^2). RiskOfRuinSimulated makes no such approximation at all: it runs a genuine Monte Carlo of independent random trade sequences and reports the fraction that actually go bust, at the cost of the precision being bounded by the usual 1/sqrt(runs) Monte Carlo error. The part that actually matters in practice, and the part every other version of this idea online gets wrong, is converting a stop distance and a lot size into a dollar figure. RR_ValuePerPoint reads the broker's own tick value and tick size for the specific symbol (MODE_TICKVALUE/MODE_TICKSIZE on MQL4, SYMBOL_TRADE_TICK_VALUE/SYMBOL_TRADE_TICK_SIZE on MQL5), rather than assuming a 100,000-unit FX contract - a hard-coded 100000 multiplier is simply wrong for metals, indices and most CFDs, and produces a risk-of-ruin number that looks precise while being built on the wrong dollar amount. RR_DollarRiskPerTrade then scales that money-per-point figure by the ACTUAL lot size passed in, so doubling the position size correctly doubles the computed risk rather than silently ignoring it. The included demo's scaling check (half lot / full lot / double lot) is the concrete proof of this on whichever platform you run it. Notes and limitations - RiskOfRuinDiffusion is an APPROXIMATION, not an exact formula - it assumes the cumulative outcome behaves like a continuous random walk with drift, which is accurate for a large number of small, frequent trades and progressively less accurate for a strategy with very few, very lumpy trades. RiskOfRuinSimulated makes no such assumption and is the more trustworthy figure whenever the two disagree by more than a rounding amount - the demo prints both side by side for exactly this comparison. - capitalUnits for RiskOfRuinFeller must be a whole number of risk units - pass MathFloor of the fractional figure RR_CapitalUnitsFromAccount returns, since a fractional unit of capital cannot absorb one more full loss. - All three risk-of-ruin functions assume win probability, payoff ratio and risk fraction stay CONSTANT for the life of the simulated account. Compounding position sizes, a strategy whose edge decays, or a trader who changes risk fraction mid-stream are not modelled here. - This library measures a probability of the account going to zero under fixed-fractional risk; it does not recommend a risk fraction, size a position, or manage an open trade. Combine it with a position-sizing library and re-run it whenever the win rate, payoff ratio or account equity changes materially.
Script · MT4 · v1.0 · Free
About Risk Of Ruin Estimator
Risk Of Ruin Estimator is a script for MetaTrader 4. It has 7 settings, such as Win probability, Payoff ratio and Account equity. MQL source code included.
Files in the download: RiskOfRuinCalculator.mqh, RiskOfRuinCalculator.txt, RiskOfRuinCalculatorDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Win probability | 0.45 | Historical or assumed win rate |
| Payoff ratio | 1.8 | Average win size / average loss size, in R |
| Account equity | 10000.0 | Equity to size the risk-of-ruin calculation against |
| Stop loss pips | 25.0 | Planned stop distance, in pips |
| Lots | 0.10 | Planned lot size |
| Max trades | 500 | Trades per simulated run |
| Num simulations | 20000 | Monte Carlo run count |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code