Incremental Export Updater
A script that re-exports full bar or trade history every time it runs gets slower and heavier every day it is used - the file it produces grows, and rewriting the whole thing from position zero every single run wastes work on rows that were already written correctly the last time. This script instead remembers exactly where it left off and only ever appends the rows that are genuinely new. A small state file, named after the bars export with a .state suffix, stores the last bar time and last closed-trade close time already written. On each run, only bars newer than the stored bar time and only closed trades whose close time is newer than the stored deal time are appended - the rest of the file is left untouched. MQL4 truncates a file the instant it is opened with FILE_WRITE, so appending genuinely means opening the file with FILE_READ|FILE_WRITE (no truncation) and seeking to the end; the very first run for a file creates it with a header row first, and every run after that only ever seeks and writes. Closed trades are additionally sorted by close time before being appended, since MODE_HISTORY's own position order is not guaranteed to match chronological order, and an out-of-order append would make the running file harder to read incrementally later. Notes and limitations - If the export CSV is deleted but its .state file is left behind, the next run resumes from the old watermark and recreates the CSV missing every row before it - delete the .state file together with the CSV to start over cleanly. - The bars export only covers whatever is currently loaded on the attached chart; it does not request additional history from the server. - Deals are appended by close time; a position that was open during one run and closes before the next run is picked up on the run after it closes, not the run during which it was still open. - Running two copies of this script against the same output files at once (two charts pointed at identical filenames) is not coordinated - each would race to seek and append, and interleaved writes could corrupt the file. Use distinct filenames per symbol or run one at a time.
Script · MT4 · v1.0 · Free
About Incremental Export Updater
Incremental Export Updater 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 Export bars, Export deals and Bars file name. MQL source code included.
Files in the download: IncrementalExportUpdater.mq4, IncrementalExportUpdater.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Export bars | true | Append newly closed bars on this chart's symbol/timeframe |
| Export deals | true | Append newly closed trades on this account |
| Bars file name | Bars export file, blank = auto name from symbol and period | |
| Deals file name | closed_trades_export.csv | Deals export file |
| Magic filter | 0 | Only this magic for deals (0 = all magics) |
| Magic filter on | false | Apply the magic filter above |
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: Alerts · MT4 Script · MQL4 Source Code