Loading…

Volatility Sizing Curves

"Size down when volatility is high" is easy to state and hard to agree on the exact shape of. A single fixed linear ramp is the usual naive implementation, but traders reasonably disagree on the curve: some want a hard cutoff at a threshold, some want a middle plateau where sizing stays full and only tapers at the extremes, some want an exponential curve that punishes very high volatility much harder than moderate volatility. This library keeps the percentile-ranking step - deciding how "high" the current volatility actually is relative to its own recent history, which is itself a whole separate design decision (ATR percentile, standard-deviation percentile, and so on) - deliberately separate from the curve-shape step, which is what this header owns. Four curve shapes are provided: LINEAR_INVERSE (a straight-line fall from the maximum multiplier to the minimum), STEP (a hard cutoff at a configurable threshold), PLATEAU (full multiplier held flat across a configurable middle band, tapering only outside it) and EXPONENTIAL (smooth decay that gets sharper as volatility rises, tuned by a sharpness parameter). Every curve is rescaled so percentile 0 lands exactly on the configured maximum and percentile 100 lands exactly on the configured minimum, so switching curve shape never silently changes the extreme-case multipliers a caller has already tuned and tested. A small percentile-rank helper is included so the demo, and any caller without one already, can turn a plain history array (e.g. a rolling window of past ATR readings) into the 0-100 percentile this library's curves expect as input. Notes and limitations - Input percentiles outside [0,100] are clamped to the nearest endpoint, not extrapolated - a caller passing a raw z-score or an un-normalised figure by mistake gets a bounded, sane multiplier rather than a wild or negative one. - VolatilitySizingCurveApplyToLots() does not normalise the result to the symbol's MODE_LOTSTEP/MODE_MINLOT/MODE_MAXLOT - lot normalisation is symbol- and broker-specific and belongs to the caller, which already has the target symbol in context. - VolatilitySizingCurvePercentileRank() is an O(n) plain rank with no sorting or caching - fine for the history-window sizes typical of a volatility lookback (tens to low hundreds of bars), not intended for ranking against tens of thousands of values on every tick. - The PLATEAU curve treats a misconfigured plateauLow > plateauHigh by silently swapping them rather than producing an inverted or undefined band - documented behaviour, not a validation error, since the config struct has no error-return path of its own. - This library performs no trading action and reads no live market data itself except in the demo's ATR-based worked example - the core header is pure curve arithmetic over a percentile the caller supplies.

Script · MT4 · v1.0 · Free

About Volatility Sizing Curves

Volatility Sizing Curves is a script for MetaTrader 4 based on ATR. It has 3 settings, such as Atr period, History bars and Base lots. MQL source code included.

Files in the download: VolatilitySizingCurves.mqh, VolatilitySizingCurves.txt, VolatilitySizingCurvesDemo.mq4.

Settings

SettingDefaultWhat it does
Atr period14ATR period used to measure volatility
History bars200how many past ATR readings to rank the current one against
Base lots1.00base lot size the worked example scales

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: ATR · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria