Loading…

Trade History JSON Exporter

MQL4 keeps no field linking a partially-closed ticket to the new ticket the broker opens for the remaining volume - the two show up in history as two separate, unrelated closed orders. A trade log that lists them as unrelated events misrepresents what actually happened: one position, closed in stages. This exporter reconstructs that link with an explicit, documented heuristic rather than pretending the data contains a hard fact it does not. A continuation candidate must share the same symbol, magic number and direction as a parent, open at a price within a small tolerance of the parent's own open price, and open within a short number of seconds of the parent's close time. Among all candidates the closest match by that gap is chosen as the parent. This is stated plainly in the JSON output itself (each chain carries a legs array showing every ticket it absorbed) and here: it is a reconstruction, not a certainty, and an unrelated trade that happens to reopen at nearly the same price within the tolerance window can be mis-chained. The JSON itself is hand-emitted with a real escaping routine - quotes, backslashes, the short named escapes, and any raw control byte below 0x20 via \u, since a broker's free-text order comment is not guaranteed to be JSON-safe as-is. Numbers are written with the traded symbol's own digit count, read via MarketInfo(), rather than a hardcoded 4 or 5. Notes and limitations - Only closed market orders (buy/sell) are exported. Pending orders that expired or were cancelled carry no fill and are not trades. - Chain reconstruction is a heuristic, not a guarantee. It can mis-link two genuinely unrelated trades that happen to reopen on the same symbol, magic and direction at a very similar price within the configured gap window, and it can fail to link a genuine continuation if the broker's timing or repricing falls outside the tolerances. - A leg's own "lots" field is the volume that specific ticket recorded as closed, not the position's original full size - MQL4's history gives no other reliable source for the original size once it has been split across multiple tickets by partial closes. - String concatenation is used to build the JSON in memory. For an extremely large history (many thousands of closed trades) this can take a noticeable moment; it is not built for real-time use.

Script · MT5 · v1.0 · Free

About Trade History JSON Exporter

Trade History JSON Exporter is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 8 settings, such as From date, To date and Symbol filter. MQL source code included.

Files in the download: TradeHistoryJsonExporter.ex5, TradeHistoryJsonExporter.mq5, TradeHistoryJsonExporter.txt.

Settings

SettingDefaultWhat it does
From date0From close time (1970.01.01 = no lower bound)
To date0To close time (1970.01.01 = up to now)
Symbol filterSymbol filter, blank = every symbol
Magic filter-1Magic filter, -1 = every magic
Chain partial closestrueGroup partial-close legs under one parent position
Chain price epsilon points2Unused on MQL5 - kept for input-surface compatibility, see header
Chain max gap seconds5Unused on MQL5 - kept for input-surface compatibility, see header
Output file nameTradeHistoryExport.jsonOutput file, written to MQL5\Files

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: Volume · Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria