Loading…

Trade Grouping Engine

A magic number alone is often too coarse a grouping key. One EA can easily run several independent baskets under the same magic number at once - grid step 1 and grid step 2 of the same strategy, or the same strategy running on two symbols - and "close everything with this magic" then closes more than intended. Splitting every basket onto its own magic number works but pushes bookkeeping outward onto the caller and burns through magic-number space fast. This header resolves a group id for any order three ways, in order of precedence: an explicit RegisterTicket(ticket, groupId) call the caller makes once (typically right after OrderSend), a "grp:<id>" tag embedded anywhere in OrderComment() and parsed on demand, or - if neither is present - an implicit "magic:<n>" group so every order still resolves to some group with zero setup. A strategy that already encodes a group id in its comments needs no extra bookkeeping at all; one that wants a clean separate registry, or needs to group orders whose comments it does not control, can register tickets explicitly instead. On top of grouping, the header exposes the operations a "logical trade" actually needs: total floating profit, gross exposure (buy+sell lots added, the true margin-relevant figure) and net exposure (buy-sell lots, the true directional exposure) across the group, and two bulk actions - close every market order and delete every pending order in the group, or apply one new stop-loss to every market order in it. Both bulk actions collect the group's tickets first and act on the collected list afterward, rather than acting while still scanning the live pool, so a rejection partway through cannot desynchronise the scan the way acting during iteration can. Notes and limitations - CloseGroup()/ModifyGroupStopLoss() send real OrderClose/OrderDelete/ OrderModify calls - these are acting functions, not pure computation. Confirm GetGroupTickets() returns the set you expect before calling either on a group id you did not fully control the population of. - The implicit "magic:<n>" fallback group has no notion of symbol unless you also pass a symbolFilter - a magic number reused across several symbols resolves to ONE implicit group spanning all of them unless you narrow every call with symbolFilter. - A comment-tag group id is read fresh from OrderComment() on every resolve call, so it always reflects the order's current comment; an explicit registration is static until UnregisterTicket() or a further RegisterTicket() call changes it. - Pending orders are included in GetGroupTickets() and in CloseGroup()'s delete pass, but are deliberately excluded from GetGroupExposure() and ModifyGroupStopLoss() - neither "exposure" nor "stop-loss" has a meaningful value for an order that has not filled yet.

Script · MT4 · v1.0 · Free

About Trade Grouping Engine

Trade Grouping Engine is a script for MetaTrader 4. MQL source code included.

Files in the download: TradeGroupingEngine.mqh, TradeGroupingEngine.txt, TradeGroupingEngineDemo.mq4.

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: Grid · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria