Optimal F And Secure F Sizer
Optimal f is the fraction of equity, expressed against the single worst trade in a sample, that maximises the geometric growth rate of an account. It is correct mathematics and a well known trap in practice, because the fraction that maximises growth also implies a historical drawdown of fifty to ninety percent that essentially no live account or trader temperament survives. Publishing optimal f alone, the way most textbook implementations do, hands someone a number that will get an account blown up if traded literally. This library computes optimal f with a plain grid search over the terminal wealth relative, because once win and loss sizes vary trade to trade there is no convenient closed form - every serious implementation of this idea scans. It then computes secure f: the largest fraction, up to the true optimum, whose historical drawdown stays under a limit the trader actually chooses. That is the number worth trading. A detail the textbook version skips: Vince's method assumes every trade in the sample was taken at the same position size, because it is comparing each trade's result to the single worst one as a ratio. A series recorded at varying lot sizes has to be normalised to profit-per-lot first with NormalizeTradeProfit, or the "worst trade" the whole calculation pivots on is meaningless. Skipping that step is the single most common way this method gets implemented wrong. Converting the chosen f into an actual lot size is done through the broker's own tick value and tick size, never a hardcoded per-pip dollar guess, so the sizing is correct on JPY crosses, metals and indices as well as plain FX pairs. FloorLots never rounds up - a request that would spend more than f of equity is refused with a zero, not silently rounded into the minimum lot. Both versions are self-contained. No dependencies, no DLLs, no classes - plain functions and one small state-free helper set. Notes and limitations - The trade series in the demo is a fixed synthetic array so the output is identical on every machine. Feed it your own closed trade history (normalised to one lot) to get a figure meaningful for a real strategy. - Optimal f and secure f describe a fixed historical sample. Markets change; a fraction computed on last year's trades is not a guarantee for next year's. Recompute periodically rather than fixing it once. - The grid search steps f in 1% increments. That is precise enough for position sizing and keeps the scan fast on a tick-driven chart. - No money is at risk in the demo on either platform. It prints numbers only and never sends a trade. - No thread or state concerns: every function is pure and takes its inputs as parameters.
Script · MT5 · v1.0 · Free
About Optimal F And Secure F Sizer
Optimal F And Secure F Sizer is a script for MetaTrader 5. It has 3 settings, such as Max drawdown pct, Account equity and Stop pips. MQL source code included.
Files in the download: OptimalFCalculator.mq5.mqh, OptimalFCalculator.txt, OptimalFCalculatorDemo.ex5, OptimalFCalculatorDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Max drawdown pct | 20.0 | Drawdown ceiling for SecureF, percent |
| Account equity | 10000.0 | Equity for the worked lot-sizing example |
| Stop pips | 30.0 | Stop distance for the worked example, in pips |
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: MT5 Script · MQL5 Source Code