Logging EA trades to SQLite instead of a CSV file
Posted: Mon Oct 19, 2026 9:08 am
Logging EA trades to SQLite instead of a CSV file
CSV logs are fine until you have a few months of them. Then you're merging files, fixing broken rows when the EA crashed mid write, and writing scripts to answer simple questions. For anyone running EAs seriously, a small SQLite database is a cleaner home for trade logs.
MQL5 has built in database functions, DatabaseOpen, DatabaseExecute and DatabasePrepare, so an EA can write straight to a SQLite file without any external library. Python reads the same file with the standard sqlite3 module, and so do plenty of free viewer tools.
A minimal setup is one table for trades and one for events. Trades get the ticket, symbol, direction, entry time and price, exit time and price, stop, target, spread at entry, slippage and the setup tag. Events get everything else that matters: the EA starting, rejected orders, spread filter blocks, news pauses.
The big advantage is querying. Average slippage by hour of day, results by setup tag, how many entries the spread filter blocked last month. Each is a single query instead of a spreadsheet session.
Wrap writes in transactions and keep them short, so a crash can't leave half a row. And keep the database file outside the terminal's tester folder, or the tester will happily overwrite it during a backtest.
CSV logs are fine until you have a few months of them. Then you're merging files, fixing broken rows when the EA crashed mid write, and writing scripts to answer simple questions. For anyone running EAs seriously, a small SQLite database is a cleaner home for trade logs.
MQL5 has built in database functions, DatabaseOpen, DatabaseExecute and DatabasePrepare, so an EA can write straight to a SQLite file without any external library. Python reads the same file with the standard sqlite3 module, and so do plenty of free viewer tools.
A minimal setup is one table for trades and one for events. Trades get the ticket, symbol, direction, entry time and price, exit time and price, stop, target, spread at entry, slippage and the setup tag. Events get everything else that matters: the EA starting, rejected orders, spread filter blocks, news pauses.
The big advantage is querying. Average slippage by hour of day, results by setup tag, how many entries the spread filter blocked last month. Each is a single query instead of a spreadsheet session.
Wrap writes in transactions and keep them short, so a crash can't leave half a row. And keep the database file outside the terminal's tester folder, or the tester will happily overwrite it during a backtest.