Loading…

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 · MT5 · v1.0 · Free

About Kelly Criterion Sizing Demonstrator

Kelly Criterion Sizing Demonstrator is a script for MetaTrader 5. It has 7 settings, such as Magic filter, This symbol only and Lookback trades. MQL source code included.

Files in the download: KellyCriterionSizerEa.ex5, KellyCriterionSizerEa.mq5, KellyCriterionSizerEa.txt.

Settings

SettingDefaultWhat it does
Magic filter-1Magic number whose closed history is analysed, -1 = every magic number
This symbol onlyfalseRestrict the history sample to this chart's symbol only
Lookback trades50Most recent closed trades used for the win rate and R ratio
Kelly fraction0.25Fraction of full Kelly shown as the "fractional Kelly" recommendation
Reference stop pips20.0Stop distance assumed when converting risk money into a lot size
Illustrate loss streak5Consecutive average losses simulated at full Kelly, for the warning
Csv file nameKellyCriterionSizerEa.csvLog of each recompute, in MQL5\Files

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Risk & Lot Size · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria