Loading…

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 · MT4 · v1.0 · Free

About Basket Volatility Brake

Basket Volatility Brake is a script for MetaTrader 4 based on ATR. It can alert you with a pop-up, sound, email or push message. It has 8 settings, such as Magic number, Vol ceiling percent and Atr period. MQL source code included.

Files in the download: BasketVolatilityBrake.ex4, BasketVolatilityBrake.mq4, BasketVolatilityBrake.txt.

Settings

SettingDefaultWhat it does
Magic number0Magic number to manage, 0 = manage every open position
Vol ceiling percent3.0Portfolio volatility ceiling, %% of equity
Atr period14ATR period used per symbol
Atr timeframePERIOD_H1Timeframe ATR is measured on
Min scale fraction0.20Never scale a leg down below this fraction of its current lots
Check seconds30How often the brake re-evaluates
Cooldown minutes15Minimum minutes between two brake activations
Log file nameBasketVolatilityBrake.csvFile written under MQL4\Files\

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 · Risk & Lot Size · Alerts · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria