Basket Volatility Brake
A basket built one entry at a time can end up carrying more real risk than intended the moment volatility widens across several symbols at once - each leg was sized correctly when it was opened, but ATR moved on all of them together. This EA is a brake, not a panic flattener: it measures aggregate risk as each managed position's lot size times its own symbol's ATR times its own live tick value (never a hardcoded per-point figure, since that varies wildly between FX pairs, metals and indices), sums it across the book, and compares the total to a ceiling expressed as a percentage of account equity. When the ceiling is breached, every managed leg is reduced by the SAME proportional fraction via a partial close - a leg carrying 2 lots and a leg carrying 0.2 lots both come down by, say, 30%, preserving the book's relative shape rather than closing the largest position outright or closing everything. A leg is left alone rather than partially closed to nothing if doing so would breach the broker's minimum lot, and a cooldown period prevents the brake firing again immediately after an activation while volatility is still elevated. This EA never opens a position of its own - it only manages what is already open, filtered by magic number (or every open position, if InpMagicNumber is left at 0). Notes and limitations Risk is measured with ATR, a lagging volatility proxy - a genuinely sudden spike can still cause real slippage before the next evaluation cycle catches it. A leg the broker rejects for partial close (network error, requote) is logged and left as-is; the next evaluation cycle will attempt it again if the book is still over ceiling. This EA does not open, hedge or otherwise protect the book beyond proportional scale-down - it is not a stop-loss replacement and does not guarantee the ceiling is never exceeded intraday.
Expert advisor · MT5 · v1.0 · Free
About Basket Volatility Brake
Basket Volatility Brake is an expert advisor (trading robot) for MetaTrader 5 based on ATR. It has 8 settings, such as Magic number, Vol ceiling percent and Atr period. MQL source code included.
Files in the download: BasketVolatilityBrake.ex5, BasketVolatilityBrake.mq5, BasketVolatilityBrake.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic number | 0 | Magic number to manage, 0 = manage every open position |
| Vol ceiling percent | 3.0 | Portfolio volatility ceiling, % of equity |
| Atr period | 14 | ATR period used per symbol |
| Atr timeframe | PERIOD_H1 | Timeframe ATR is measured on |
| Min scale fraction | 0.20 | Never scale a leg down below this fraction of its current lots |
| Check seconds | 30 | How often the brake re-evaluates |
| Cooldown minutes | 15 | Minimum minutes between two brake activations |
| Log file name | BasketVolatilityBrake.csv | File written under MQL5\Files\ |
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 → Experts.
- 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 expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: ATR · Risk & Lot Size · MT5 Expert Advisor · MQL5 Source Code