Kelly Criterion Sizing Demonstrator
The Kelly criterion tells you the fraction of capital to risk that maximises long-run growth, given a win rate and a win/loss size ratio. Quoted alone as a formula it looks tame; the number it actually produces, sized against a real account, usually is not. This EA computes the real number from your own history instead of a hypothetical one, and then simulates the part that makes full Kelly unsurvivable in practice: a short losing streak at full size. Win rate and the average-win-to-average-loss ratio (R) are measured from the most recent closed trades matching the magic number and symbol filters. The classic formula f* = W - (1-W)/R then gives the full Kelly fraction of balance to risk per trade. A configurable fractional Kelly (a quarter Kelly by default, the level many practitioners consider more survivable) is shown alongside it. Both are converted into an actual lot size using a reference stop distance and tick value/tick size - never a hardcoded contract size, which mis-sizes metals, indices and JPY crosses. The part most Kelly explanations skip is shown directly: because a fixed FRACTION of a shrinking balance is risked on every consecutive loss, N average losses in a row at full Kelly size multiply the account by (1-f*)^N, not subtract N times f*. At even a modest full Kelly fraction, a handful of losses in a row - well within normal variance for any real system - removes the majority of the account. That number is computed and shown directly, which is the whole point of calling this a demonstrator. Kelly's original 1956 formula assumes a binary bet with a fixed win amount and a fixed loss amount. Real trading has variable-sized wins and losses, so f*=W-(1-W)/R is the standard approximation used in trading contexts, not an exact result - stated here so the number is not mistaken for more precision than the method actually has. Notes and limitations This EA places no orders and manages no positions - it is a calculator only. A negative full Kelly fraction means the sampled history shows no positive edge at all; Kelly's answer in that case is not to size down, it is not to take the bet, and the comment says so rather than showing a negative lot size. Results depend entirely on the quality and stability of the sampled history - a small or recent sample can show a flattering ratio that does not hold going forward, and Kelly itself assumes the edge stays constant, which real strategies rarely do for long.
Expert advisor · MT4 · v1.0 · Free
About Kelly Criterion Sizing Demonstrator
Kelly Criterion Sizing Demonstrator is a script for MetaTrader 4. It has 7 settings, such as Magic filter, This symbol only and Lookback trades. MQL source code included.
Files in the download: KellyCriterionSizerEa.ex4, KellyCriterionSizerEa.mq4, KellyCriterionSizerEa.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic filter | -1 | Magic number whose closed history is analysed, -1 = every magic number |
| This symbol only | false | Restrict the history sample to this chart's symbol only |
| Lookback trades | 50 | Most recent closed trades used for the win rate and R ratio |
| Kelly fraction | 0.25 | Fraction of full Kelly shown as the "fractional Kelly" recommendation |
| Reference stop pips | 20.0 | Stop distance assumed when converting risk money into a lot size |
| Illustrate loss streak | 5 | Consecutive average losses simulated at full Kelly, for the warning |
| Csv file name | KellyCriterionSizerEa.csv | Log of each recompute, in MQL4\Files |
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: Risk & Lot Size · MT4 Expert Advisor · MQL4 Source Code