Loading…

Max Orders Guard

Every EA that can open more than one position eventually needs an exposure cap, and the naive version of that cap is a single global count check, which cannot express "at most 3 per symbol" or "at most 2 per magic number" at all. This header separates the count from the limit: internal scan functions do the actual pool scan, and every public counting function is a thin filter over them, so the scan logic exists in exactly one place rather than being copied into every EA that needs a different slice of the same count. By default, only market positions are counted, not pending orders. That is a deliberate choice, not an oversight: "how much am I already exposed" and "how many pending entries are staged" are usually different risk questions with different limits, and folding them into one number hides that distinction. An includePending flag opts into counting both together for the cases where a single combined ceiling genuinely is what's wanted. MogCanOpenNew checks total, per-symbol and per-magic limits together with a sentinel, MOG_NO_LIMIT (-1), for any dimension the caller does not want capped. That covers the common real pattern of capping exposure per symbol-per-magic while leaving the account-wide total uncapped, without forcing every call site to pass three real limits every time. Notes and limitations - Every counting function performs a fresh pool scan; there is no caching. That is correct for a guard checked once before sending a trade, but calling it in a tight loop hundreds of times per tick is unnecessary work - count once per tick and reuse the value. - On MQL4, includePending counts pending orders using the same slot in the trade pool as market orders (there is no separate pending pool on MQL4). On MQL5, includePending adds a second, genuinely separate scan over the pending-orders pool - see the differences section above. - MOG_NO_LIMIT is -1. Do not pass a genuine negative number as a limit expecting it to mean "always blocked" - MogCanOpenNew treats -1 specifically as "dimension disabled", not as a limit of negative one. - This header only counts and compares; it does not send, close or modify any trade on either platform, and takes no action on its own when a limit is reached. - On an MQL5 netting account, MogCountByMagic and any limit built from it answer a coarser question than on hedging - see the differences section.

Script · MT5 · v1.0 · Free

About Max Orders Guard

Max Orders Guard is a script for MetaTrader 5. It has 1 setting, such as Sample magic. MQL source code included.

Files in the download: MaxOrdersGuard.mq5.mqh, MaxOrdersGuard.txt, MaxOrdersGuardDemo.ex5, MaxOrdersGuardDemo.mq5.

Settings

SettingDefaultWhat it does
Sample magic20260816Magic number used in the worked examples

How to install

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

MQL4 & MQL5 scripts · PolyMaria