Volatility Targeted Position Sizing
A fixed lot size or a fixed percent-of-equity risk both share a blind spot: neither looks at how wide the market is currently moving. The same 0.10 lots represents a small dollar swing in a quiet week and a large one the moment volatility triples, yet the position size never adjusted at all. Volatility targeting inverts the question. Instead of "how many lots", it asks "how much dollar volatility do I want a bar to represent", then divides that target by however wide bars currently are. Size grows automatically when the market goes quiet and shrinks automatically when it gets loud - the dollar swing an average bar produces stays roughly where the target says it should, through regimes a static size sails straight through. Two ways to measure current volatility are supported. The common case reads an ATR value in points, cheap to compute (iATR on MQL4, an iATR handle plus CopyBuffer on MQL5) and already familiar to most EAs. The alternative, VolT_StdDevPoints, computes a sample standard deviation directly from a caller-supplied series of point returns - useful when ATR's fixed exponential-style smoothing period is not the specific lookback window you actually want. An annualised target is also supported: VolT_PerBarTargetFromAnnual converts a percent-of-equity annual volatility target into the equivalent per-bar dollar figure using square-root-of-time scaling, the standard way volatility is known to compound across time horizons. That per-bar figure then feeds the same lot conversion as the direct target. Every money conversion goes through the symbol's own tick value and tick size, never a hardcoded per-pip dollar figure, so results are correct on JPY crosses, metals and indices, not only plain FX pairs. VolT_LotsForMoneyTarget returns 0.0 - a refusal, not a substitution - when even the broker minimum lot would already represent more dollar volatility than the target allows. The header is self-contained. No dependencies, no DLLs, no classes - plain functions you call from a script, an indicator or an EA. Notes and limitations - ATR and realised standard deviation both describe the recent past. Neither predicts the next bar's actual range; volatility targeting reduces the variance of outcomes, it does not eliminate it. - The square-root-of-time scaling in VolT_PerBarTargetFromAnnual assumes returns are roughly independent bar to bar. Markets with strong autocorrelation or regime persistence will not scale exactly this way - treat the annualised conversion as a reasonable starting point, not an exact law. - barsPerYear defaults to 252 (daily bars). Pass a different count explicitly for other timeframes - the default is wrong for anything but a daily series. - No money is at risk in the demo. It prints numbers only and never sends an order. - No thread or state concerns: every function is pure and takes its inputs as parameters.
Script · MT5 · v1.0 · Free
About Volatility Targeted Position Sizing
Volatility Targeted Position Sizing is a script for MetaTrader 5 based on ATR. It has 4 settings, such as Target money vol, Annual target pct and Account equity. MQL source code included.
Files in the download: VolatilityTargetSizer.mq5.mqh, VolatilityTargetSizer.txt, VolatilityTargetSizerDemo.ex5, VolatilityTargetSizerDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Target money vol | 100.0 | Target dollar move per bar's worth of volatility |
| Annual target pct | 10.0 | Annualised target, percent of equity, for the second example |
| Account equity | 10000.0 | Equity used for the annualised example |
| Atr period | 14 | ATR period read from the chart |
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 · ATR · MT5 Script · MQL5 Source Code