Anti Averaging Down Sentinel
Averaging down - adding to a position that is already losing, in the hope the market turns before the account does - is one of the fastest routes from a manageable loss to an account-ending one. It is also something a trader (or a second EA sharing the account) can do by accident, one click at a time, without ever meaning to build a martingale. Neither MQL4 nor MQL5 gives one program a hook to intercept another program's trade request before it reaches the broker. This is honest about that: it cannot refuse the trade before it exists. What it does instead is scan the account on every tick, notice a brand-new trade the instant it appears, and check whether an older trade on the same symbol and direction is currently floating a loss. If so, the new trade is closed straight back out and the reason is logged and alerted. The window between the trade being placed and being closed again is real, not zero - this is detection-and-reversal, not prevention, and that limitation is the entire point of reading this before relying on it. Because it scans the whole account rather than one EA's own tickets, it catches averaging down from any source: a second EA, a manual click, or itself if misconfigured. The concept it detects - two separately-closeable trades on the same symbol and direction, one older and losing - only exists on a hedging account. On a netting account MetaTrader merges a same-direction add into the existing position before any program ever sees a second ticket, so there is nothing left for a sentinel like this to find. The MQL5 version checks the account's margin mode on startup and prints a clear warning if it is running on netting, rather than pretending to protect an account it cannot. Notes and limitations - This is reactive, not preventive, on both platforms. The averaging-down trade is placed, briefly exists in the account, and is then closed - it is never blocked from being sent in the first place, because neither platform lets one program intercept another's trade request. - On MQL5, this sentinel only functions on a hedging account. On netting it loads, warns once in the log, and then has nothing to detect for the life of the chart - it is not a silent no-op, but it is not doing anything useful either. - "Already losing" is read from the older trade's own floating P/L at the moment the new trade first appears, not from price action afterward, so a position that only turns negative later is not retroactively flagged. - Closing the new trade at market means the account still pays the spread and any slippage on both the open and the close - the averaging-down attempt is reversed, not made free. - Requires AutoTrading enabled to close anything; with it off, the EA can still detect and alert but cannot act, and says so in the log.
Expert advisor · MT5 · v1.0 · Free
About Anti Averaging Down Sentinel
Anti Averaging Down Sentinel is an expert advisor (trading robot) for MetaTrader 5. It has a news filter. It has 5 settings, such as Symbol filter, Magic filter and Dry run. MQL source code included.
Files in the download: AntiAveragingDownSentinel.ex5, AntiAveragingDownSentinel.mq5, AntiAveragingDownSentinel.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 |
|---|---|---|
| Symbol filter | Only watch this symbol; blank = every symbol in the terminal | |
| Magic filter | -1 | Only watch this magic number; -1 = every magic number (terminal-wide) |
| Dry run | false | true = alert only, never closes - use to observe before enforcing |
| Max retries | 5 | Attempts per close before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
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: Martingale · News Trading · MT5 Expert Advisor · MQL5 Source Code