Tracking Order Events in MetaTrader 4 with State Comparisons
Summary
This article explains how an MQL4 Expert Advisor can detect changes to orders and positions by comparing current terminal state with a saved prior state. A simple order-count check can identify increases or decreases, while polling in a loop with a short pause avoids relying only on incoming ticks. The method can be filtered by chart symbol and MagicNumber so an EA monitors only its own orders.
The article develops the approach into event categories covering pending-order triggers and position closures by stop loss, take profit, or expiration. To identify which individual position disappeared, it recommends retaining open position tickets and checking the missing ticket in account history. Looking only for the most recently closed order can misclassify events if multiple orders close together. The resulting library exposes counters for other EAs to use. This is a polling-based workaround rather than a native event callback, and detection depends on the EA continuing to run and on correct history matching.
Key ideas
- Order events can be inferred by comparing current order state with a stored snapshot from an earlier check.
- Polling allows checks between ticks, while symbol and MagicNumber filters narrow monitoring to relevant orders.
- Order-count changes alone cannot identify pending-order activation or the reason a position closed.
- Tracking tickets and matching disappeared positions in history is more reliable than inspecting only the latest closure.
- An event library can expose detected event counters for use by other Expert Advisors.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.