Loading…

Position Reconciliation Against File

Anything that keeps a record of what should be open outside the terminal itself - a copier, a signal service, a manual trade log, a risk desk that tracks exposure in a spreadsheet - can drift from what is actually open. A copier misses a fill during a reconnect. A manual entry uses 0.1 lots where the plan said 0.01. A position gets closed by a stop and the outside record is never updated. None of these show up unless something checks both sides against each other. This script reads a CSV you maintain - ticket, symbol, type, volume - and matches every row against the live book by ticket number, the one field both sides are guaranteed to agree on if the record is genuine. Three outcomes are reported separately rather than lumped into one pass/fail: a record with no matching live ticket is MISSING, a live ticket with no matching record is EXTRA, and a ticket present on both sides with a different symbol, type or volume is a MISMATCH. Treating these as three categories rather than one matters, because the fix for each is different - a missing position needs re-entering, an extra one needs investigating, and a mismatch usually means the volume was typed wrong on one side. Volume comparison uses a small tolerance rather than exact equality, because a lot size that has been through broker-side rounding can differ in the last decimal place without being a real discrepancy. The tolerance is an input, not a hardcoded constant, since lot step varies by symbol and broker. The live book is read with a ticket-collect loop rather than any form of "select by symbol" call, on both platforms. On MQL5 this matters more than it sounds: PositionSelect(symbol) is ambiguous the instant a hedging account allows more than one open position per symbol, so this script always walks PositionsTotal()/PositionGetTicket(i) and, when pending orders are included, OrdersTotal()/OrderGetTicket(i) - the same pattern this catalogue uses for every order-pool scan, applied here to reading rather than acting. Notes and limitations - Matching is by ticket number only. A record file that predicts a future ticket it does not yet know cannot be reconciled - this is an audit of what is currently open, not a trade planner. - Symbol comparison is an exact string match. A broker suffix difference (EURUSD versus EURUSD.a) will show as a mismatch even if the underlying instrument is the same; normalise the record file's symbol column to match this terminal's names before running. - The live book is snapshotted once at the start of the run. A position that closes while the script is printing its report is not re-checked. - This script only reads. It never places, closes or modifies an order.

Script · MT4 · v1.0 · Free

About Position Reconciliation Against File

Position Reconciliation Against File is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 6 settings, such as Record file name, Delimiter and Include pending orders. MQL source code included.

Files in the download: PositionReconciliationAgainstFile.ex4, PositionReconciliationAgainstFile.mq4, PositionReconciliationAgainstFile.txt.

Settings

SettingDefaultWhat it does
Record file nameposition_record.csvRecord file, from MQL4\Files
DelimiterDelimiterCommaRecord file column separator
Include pending ordersfalseAlso reconcile pending orders
Volume tolerance0.001Lot difference still counted as a match
Write report filetrueWrite a discrepancy report CSV
Alert on discrepancytrueAlert when the two sides disagree

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 · Buy/Sell Arrows · Trade Copier · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria