Idempotent Order Intent Ledger
Sending an order has a gap almost nobody codes around: the request can leave the terminal, the server can fill it, and the confirmation can be lost to a timeout, a disconnect or a crash before the ticket ever reaches your EA. Seeing an error and no ticket, the EA retries. Now there are two positions instead of one, and the account is carrying twice the intended risk with no warning. A retry counter does not fix this - it only slows down how fast the duplicate gets sent. The actual fix is a ledger: write the intent to a file BEFORE sending, keyed by something that identifies the trading decision itself - symbol, direction, size and the bar time the signal fired. Check that key first. A key already marked FILLED or FAILED means skip, not retry. A key still marked SENT means the previous attempt's outcome is simply unknown yet, and the same key should be reused rather than a new one minted. The reason this must live in a file rather than in an array is the restart case. A terminal crash or an unexpected close is exactly the moment a stuck intent is most likely to be forgotten and resent on the next EA init - an in-memory ledger loses everything at the one moment it matters most. The header brackets whatever send call the EA already makes - OrderSend on MQL4, OrderSend/CTrade on MQL5. It does not place orders itself, so it drops into any existing trading wrapper without requiring a rewrite of the send logic. Notes and limitations - The ledger does not send an order and does not know whether a "SENT" record actually reached the server. Reconciling SENT records against real broker state after a restart is the caller's job - pair this with the companion Order Lifecycle Machine library (L119) for that half of the problem. - CSV storage is plain text in the sandbox, not encrypted. Do not journal account credentials or anything sensitive - only trade identifiers. - File I/O on every BeginIntent/MarkFilled call is a deliberate trade-off: durability over raw speed. For a scalper sending many orders per second, batch the saves instead of calling SaveAll on every state change. - Prune only removes terminal-state records past the age threshold; SENT records are kept indefinitely so an unresolved intent is never silently dropped.
Script · MT4 · v1.0 · Free
About Idempotent Order Intent Ledger
Idempotent Order Intent Ledger is a script for MetaTrader 4. It has 1 setting, such as Ledger file. MQL source code included.
Files in the download: OrderIntentLedger.mqh, OrderIntentLedger.txt, OrderIntentLedgerDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Ledger file | OrderIntentLedgerDemo.csv | Sandbox filename for the journal |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code