Kelly And Optimal F From History
Kelly and optimal f both answer the same underlying question - what fraction of capital, risked consistently, would have grown a given trade sequence fastest - but the two formulas use different inputs and most traders only ever see one of them explained. This script computes both from the same closed-trade history in one run, alongside the trade statistics each one is built from, so they can be compared rather than taken on faith. Kelly needs only three numbers - win rate, average win and average loss - and has a closed-form answer. Optimal f is different: it treats the whole historical sequence of trade results as fixed and searches, fraction by fraction, for the exact bet size that would have produced the highest geometric growth rate on THAT sequence, measured against the single biggest loss in the sample. There is no shortcut formula for it; the script performs the search directly, the same way the original technique was described. Both figures are the FULL-Kelly and FULL-optimal-f numbers, which is the size that maximises long-run growth and is also famous for producing account swings most traders find unbearable to sit through. Every serious piece of writing on the subject recommends trading at a fraction of the full figure, so this script prints the half-size alongside the full one rather than leaving that judgement call out. The other honesty check built in: optimal f is curve-fit to the past by definition. A thin sample dresses up as a confident-looking number that is really statistical noise, so the script refuses to report anything below a configurable minimum trade count rather than hand over a fraction nobody should act on. Notes and limitations - Both figures assume the future resembles the historical sample. A strategy whose edge has changed - improved or decayed - will not show that until enough new trades accumulate. - Optimal f as reported is a fraction applied against the biggest historical loss, not a percent of equity directly. Converting it into a lot size for today's account requires knowing today's per-lot loss at the same stop distance; this script does not attempt that conversion, deliberately, because doing it wrong would be worse than leaving it undone. - If historical lot sizes varied across the sample, both figures are a risk-fraction guide rather than a literal sizing instruction. - The terminal's history view only shows what has been loaded/selected. A short-looking trade count is often a loading issue, not a short history. - A sample with no losing trade cannot produce either figure - both formulas divide by a loss term that does not exist yet in that case.
Script · MT5 · v1.0 · Free
About Kelly And Optimal F From History
Kelly And Optimal F From History is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 8 settings, such as Symbol filter, Magic filter and From date. MQL source code included.
Files in the download: KellyOptimalFromHistory.ex5, KellyOptimalFromHistory.mq5, KellyOptimalFromHistory.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Symbol filter | Symbol filter ("" = every symbol) | |
| Magic filter | -1 | Magic number filter (-1 = every magic) - long, MQL5 magic numbers are 64-bit |
| From date | 0 | Earliest close time (0 = all loaded history) |
| To date | 0 | Latest close time (0 = up to now) |
| Min trades | 20 | Minimum closed trades required before reporting |
| F step | 0.01 | Optimal-f search step (0.01 = test every 1%) |
| Export csv | false | Also write the f / growth curve to a CSV file |
| Csv delimiter | , | CSV delimiter (first character is used) |
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 · Alerts · MT5 Script · MQL5 Source Code