Skip to content
All library documents

Logging MetaTrader Trade Events to SQLite with the Built-In Database API

Article MQL5 articles

Summary

This article explains how to build a MetaTrader 5 Expert Advisor that records trade events in an SQLite database as they occur. It uses the terminal’s built-in database functions to create a table and time index, prepare parameterized SQL statements, and store deal identifiers, instrument and position details, prices, volume, costs, profit, account information, and event time. Incoming deals are categorized as position openings, closings, balance events, or other events.

The EA checks trade history on each trade callback and compares the current deal count with the previously logged count. At startup it compares database rows with account history to catch events missed while the EA was offline. The article explains how prepared statements and typed parameter binding support repeated inserts and safe handling of special characters. Its approach requires a sufficiently recent MetaTrader build and stores the database under the terminal’s files directory. Multiple writers may contend for access, and per-deal writes add overhead, which can matter for very high-frequency systems.

Key ideas

  • MetaTrader’s built-in database API can connect MQL5 programs to SQLite without an external DLL.
  • The logger stores structured deal events in one indexed table using prepared statements and parameter binding.
  • Trade-history counts help identify newly arrived deals and recover events missed during EA downtime.
  • Trade events can include openings, closings, balance operations, and other deal types.
  • Build requirements, simultaneous writer contention, and per-deal write overhead are practical constraints.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.