Loading…

Portfolio State Time Machine

Most trade journals only record what happened at open and close. They cannot answer "what did the whole book look like at 14:00 on Tuesday" - by the time you ask, the orders may have closed, modified their stops, or never existed at that exact moment in the terminal's own history at all. This EA solves that by taking its own periodic photographs. On a timer, at an interval you choose, it walks every open and pending order (iterating the order pool backwards and checking each OrderSelect result, since the pool re-indexes as soon as anything leaves it) and writes one row per order, plus one row carrying the account's balance, equity, margin and free margin, into a single append-only CSV log. All rows from the same pass share one timestamp, so that shared timestamp is both the snapshot's boundary marker and the key used to reconstruct it later. Turning InpQueryMode on switches the EA from recorder to viewer. Give it a date and time and it scans the log for the ACCOUNT row closest to that moment, then re-reads every row that shares that row's timestamp and displays the reconstructed book on screen and in a plain-text result file. If nothing in the log falls within the tolerance you set, it says so plainly rather than presenting the nearest snapshot as if it were an exact match - a common failure mode of naive "closest record" lookups is to silently answer with something a day away and let the user assume it was accurate. The naive alternative - one file per snapshot, named by timestamp - was deliberately avoided. At a five-minute interval that is over a hundred files a day, unmanageable to browse, back up or delete. A single growing CSV with a row-type column keeps the whole history in one place and lets any spreadsheet or script scan it just as easily. Notes and limitations Snapshot granularity is exactly the interval you choose - nothing that opened and closed entirely between two snapshots will ever appear in the log. A five-minute interval will simply never see a trade that lasted ninety seconds. The log file only grows; nothing is ever pruned or rotated automatically. Left running for months at a short interval on a busy account it will become a large file, which also makes every query slower because a query currently reads the file from the start in a single pass. This is a monitoring tool for a bounded window of history, not an archival database - trim or archive the CSV by hand if it grows too large. Gaps are shown honestly, not hidden. If the terminal, the EA or the chart it is attached to was offline for a stretch (platform restart, EA removed, no internet), that stretch has no snapshots at all, and a query landing inside it returns whatever the InpQueryToleranceSeconds setting allows - either the nearest snapshot on either side of the gap, correctly labelled with how far away it actually is, or an explicit refusal if that distance is too large. It never fabricates data for a moment nothing was recorded. The file lives in the terminal's own MQL4\Files sandbox (no FILE_COMMON), so each terminal installation keeps its own independent log; it is not shared between multiple terminals pointed at the same broker account. Order comments containing a comma or line break have those characters replaced with a semicolon or space before being written, since MQL4's CSV writer does not quote fields - this keeps the column layout intact at the cost of not reproducing the comment byte-for-byte in rare cases. This EA never opens, modifies or closes an order. It only reads the order pool and account state and writes them to a file.

Expert advisor · MT4 · v1.0 · Free

About Portfolio State Time Machine

Portfolio State Time Machine is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 5 settings, such as Snapshot interval seconds, Query mode and Query date time. MQL source code included.

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

Settings

SettingDefaultWhat it does
Snapshot interval seconds300Seconds between snapshots (live mode, ignored in query mode)
Query modefalsetrue = look up a past snapshot instead of recording new ones
Query date timeRequested date/time, format yyyy.mm.dd hh:mi:ss (query mode only)
Query tolerance seconds900Max distance from a real snapshot to accept as an answer, seconds
Log file nameLog file name; blank = auto-named per account number

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: Pending Orders · Alerts · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria