Loading…

Ticket Lineage Tracker

A partial close in MQL4 does not keep one ticket alive for the life of the trade. The order's remaining volume stays on its original ticket, but the CLOSED portion is written into history under a brand-new ticket number - one that shares the original's OrderOpenTime(), OrderOpenPrice(), OrderSymbol(), OrderType() and OrderMagicNumber(), but is otherwise a completely separate history record. A P/L report or a win-rate calculator that treats each history ticket as one trade silently overcounts every position that was ever partially closed, and understates the true holding period of the position as a whole. There is no server-side field, and no comment text MetaTrader writes on its own, that records "this ticket came from that ticket" - the catalogue was checked for an existing partial-close comment convention before writing this, and none exists, nor does MT4 generate one itself. What the server does reliably preserve across every fragment of one position is the shared open time. This header groups on that: given any one ticket belonging to a lineage, it scans history for every closed record sharing that ticket's open time, symbol, type and magic number (within a two-second tolerance, since some brokers' fragment records have been observed to differ from each other by a server-clock second), adds the still-open remainder if the position has not fully closed yet, and sums profit+swap+commission across the closed legs into one realised P/L figure. Because the key is the shared open time rather than a comment tag a caller would have to set at trade-open time, this works retroactively across an account's entire existing history - it needs no cooperation from whatever code originally opened the trade. Notes and limitations - Grouping is by open time + symbol + type + magic, not by a guaranteed unique key - in the extremely unlikely case that two genuinely unrelated orders on the same symbol, same type, same magic happen to open within the same two-second window, they would be merged into one reported lineage. This has not been observed in practice but is worth stating plainly since it is the one assumption the whole header rests on. - Only orders still present in the terminal's locally retained history are visible - if the account's history window has aged out an old fragment, that fragment simply will not appear in the returned chain, and the realised P/L total will understate the true figure with no way to detect that it happened. - Read-only by design - no function in this header calls OrderClose, OrderModify or OrderDelete. It exists purely to answer "what happened to this trade", not to act on it. - TLT_OPEN_TIME_TOLERANCE_SECONDS (2 seconds) is a compile-time constant. If your broker's fragment records are observed to drift further apart than that, widen it - too wide a tolerance risks merging unrelated same- second trades, so this was kept as tight as the confirmed drift needs.

Script · MT4 · v1.0 · Free

About Ticket Lineage Tracker

Ticket Lineage Tracker is a script for MetaTrader 4. It has 1 setting, such as Lookup ticket. MQL source code included.

Files in the download: TicketLineageTracker.mqh, TicketLineageTracker.txt, TicketLineageTrackerDemo.mq4.

Settings

SettingDefaultWhat it does
Lookup ticket0Optional: a specific ticket to trace (0 = skip this section)

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

MQL4 & MQL5 scripts · PolyMaria