Fresh MQL5 code release – perfect for enhancing your MT4/MT5 strategies.
OrderGuard is an include file for people who write their own Expert Advisors for MetaTrader 5. Its purpose is to make the way an EA sends its orders safe. It is not a robot and it has no trading strategy inside. It comes with a small example EA that shows it in use. It sends market buy and sell orders only.
It does four main things:
- The same signal is never traded twice after a restart, a reconnect or a retry.
- The limit on how many trades may be open is counted from the broker, not from a number that is lost when the EA restarts.
- No orders are sent while the terminal has lost its connection, and the outage is written to the log once, not on every tick.
- Stops and targets that sit too close to price are moved out to the broker's minimum distance and rounded properly.
Before every order it also makes these checks:
Watch in-depth trading tutorials and EA reviews on the RobotFX YouTube channel. Subscribe for free tips.
- The lot size is fitted to what the symbol allows. It is never rounded up.
- No order without enough free margin, none while the market is closed, none the symbol does not allow.
- It picks a way of filling the order that the symbol accepts.
- If the broker refuses an order, it waits a minute before trying that signal again, then longer each time (15 minutes at most).
- A skipped signal is written to the log once, not on every tick.
How to use it: put OrderGuard.mqh in MQL5\Include\OrderGuard\ and add #include <OrderGuard\OrderGuard.mqh> to your EA. Create one COrderGuard object and call Init() with your magic number in OnInit(). Then send every order through Open() with a key that names the signal. The example EA uses the direction plus the bar's open time as the key. Open() makes all the checks above and sends the order only if that key has not been traded before.
These are faults I found and fixed on my own trading system. The example EA's entry rule (two moving averages crossing) is only there to give OrderGuard something to guard. It is not a strategy and makes no claim of profit. OrderGuard works on whatever symbol and timeframe the EA that uses it trades. Free to use and change.
Testing: Strategy Tester, H1, 1 September to 20 November 2026, hedging account. EURUSD 13 orders sent and 13 filled, USDJPY 32 and 32, US30 34 and 34, GER30 21 and 21. On all four, no signal was traded twice and no order was refused by the broker. With its memory of past signals switched off on purpose it still made 13 trades with none doubled. Asked for 500 lots, it sent no order. The picture shows these results as a table, with three lines it wrote to the journal.
Not tested: a real restart of a live terminal, a lost connection (the tester is always connected), netting accounts, gold, any other broker, real money.
Inputs of the example EA (defaults):
- Magic number: 240117
- Lots: 0.01
- Fast MA period: 20
- Slow MA period: 50
- Stop distance, points: 300
- Target distance, points: 600
- Max open positions for this EA (counted from the broker): 1

Thanks for reading. Discover premium MetaTrader solutions at RobotFX.
80048