Kelly Criterion Position Sizer
Full Kelly (f* = W - (1-W)/R, where W is win probability and R is average win divided by average loss) maximises long-run geometric growth in theory, but only when W and R are known exactly. Estimated from a rolling sample of real trades they are noisy, and full Kelly is notoriously punishing when the estimate is even slightly optimistic - it is a growth-optimal bet size, not a survivable one. Most published Kelly snippets stop at the formula and leave the trader to remember that full Kelly is not what anyone actually trades. This header treats the raw fraction as an upper bound rather than a target. KellySizer_LotsFromFraction takes an explicit fractional multiplier (0.5 for half Kelly, 0.25 for quarter Kelly - the multiples practitioners actually use) and two independent hard caps: a maximum fraction of equity ever risked, and a maximum absolute lot size. Whichever cap binds tightest wins, regardless of what the Kelly maths alone produced. CKellyRollingStats accumulates win rate and payoff ratio from a stream of closed-trade profit figures fed to it one at a time, so the inputs to the formula come from what a system actually did rather than a number typed in from a stale backtest report. A negative estimated edge correctly returns a zero fraction, never a negative lot size or a flipped-direction bet. Money-per-point is always read from the symbol's own tick value and tick size and multiplied by the lots being sized, never a hardcoded contract size, so lot sizing is correct on metals, indices and JPY crosses as well as plain FX, on both platforms. The header is self-contained. No dependencies, no DLLs. Notes and limitations - Kelly assumes independent, identically distributed trade outcomes. Real systems have regime changes and serial correlation, which is exactly why this header defaults every worked example to a fraction well below 1.0 and still layers hard caps on top. - A rolling sample small enough to react to a recent regime is also small enough to be statistically noisy. There is no fixed "enough trades" answer here - widen the sample or lower the multiplier, not both blindly. - CKellyRollingStats treats an exact-zero result as a scratch trade, counted in neither the win nor the loss side, since it carries no information about edge or payoff. - No money is at risk in either demo. Both print numbers only and never call OrderSend / CTrade. - Money-per-point uses the CURRENT tick value/tick size, which some brokers vary intraday on certain instruments (metals in particular) - refresh it near the point of use rather than caching it for long periods, on either platform.
Script · MT5 · v1.0 · Free
About Kelly Criterion Position Sizer
Kelly Criterion Position Sizer is a script for MetaTrader 5. It has 4 settings, such as Equity, Stop pips and Max fraction equity. MQL source code included.
Files in the download: KellyCriterionSizer.mq5.mqh, KellyCriterionSizer.txt, KellyCriterionSizerDemo.ex5, KellyCriterionSizerDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Equity | 10000.0 | Account equity used in the worked examples |
| Stop pips | 20.0 | Stop distance for the lot-sizing examples |
| Max fraction equity | 0.25 | Hard cap: maximum fraction of equity ever risked |
| Max lots | 5.0 | Hard cap: maximum lot size regardless of Kelly (0 = no cap) |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 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 · MT5 Script · MQL5 Source Code