Writing ticks to a file from an EA without slowing OnTick
Posted: Fri Oct 23, 2026 1:03 am
Writing ticks to a file from an EA without slowing OnTick
I log every tick from a couple of symbols for later analysis, and the first version slowed the EA badly during busy minutes. Here's what fixed it.
The slow version opened the file, wrote one line, and closed it on every tick. FileOpen and FileClose are expensive compared with everything else in OnTick. At the London open, EURUSD can produce dozens of ticks a second on some feeds, and the EA was lagging behind.
The faster version:
Open the file once in OnInit with FILE_WRITE, FILE_READ, FILE_CSV and FILE_SHARE_READ, and seek to the end.
In OnTick, append the tick to a string buffer rather than writing directly.
Every 100 ticks, or every second using OnTimer, write the buffer with FileWriteString and call FileFlush.
In OnDeinit, write anything left and close the file.
Each line holds time in milliseconds from SymbolInfoTick, bid, ask, and the flags field.
The other improvement was rolling the file daily. One file per symbol per day keeps sizes manageable and makes it easy to load a single day into Python.
On a busy morning the buffered version keeps up easily. The trade logic in the same EA is no longer delayed by disk writes.
If you don't trade from the same EA, a separate logging EA on its own chart is even safer, since a slow write can't affect trading at all.
I log every tick from a couple of symbols for later analysis, and the first version slowed the EA badly during busy minutes. Here's what fixed it.
The slow version opened the file, wrote one line, and closed it on every tick. FileOpen and FileClose are expensive compared with everything else in OnTick. At the London open, EURUSD can produce dozens of ticks a second on some feeds, and the EA was lagging behind.
The faster version:
Open the file once in OnInit with FILE_WRITE, FILE_READ, FILE_CSV and FILE_SHARE_READ, and seek to the end.
In OnTick, append the tick to a string buffer rather than writing directly.
Every 100 ticks, or every second using OnTimer, write the buffer with FileWriteString and call FileFlush.
In OnDeinit, write anything left and close the file.
Each line holds time in milliseconds from SymbolInfoTick, bid, ask, and the flags field.
The other improvement was rolling the file daily. One file per symbol per day keeps sizes manageable and makes it easy to load a single day into Python.
On a busy morning the buffered version keeps up easily. The trade logic in the same EA is no longer delayed by disk writes.
If you don't trade from the same EA, a separate logging EA on its own chart is even safer, since a slow write can't affect trading at all.