Connecting MQL Expert Advisors to SQL Server Through a C# DLL
Summary
This article presents an integration pattern for storing and analyzing trading data from MQL4 or MQL5 Expert Advisors in Microsoft SQL Server. Since MetaTrader does not provide direct database-server access, the approach uses a C# class library that exports unmanaged functions, which the MQL expert imports. The article walks through project setup, platform targeting, dependencies, exported static functions, and the use of a database connection and commands.
The example is an educational bridge for passing simple values and database operations between MQL and .NET. It highlights interoperability constraints, including supported data types and limitations around generic collections, and discusses configuration issues that can affect building the DLL. The author reports that the approach has been used with many simultaneous experts, but the sample itself is deliberately limited and may need substantial extension for real systems. It is infrastructure guidance rather than a trading strategy, and gives no evidence about strategy performance or database reliability under particular workloads.
Key ideas
- MQL experts can communicate with SQL Server through functions exported from a C# DLL.
- The DLL must expose unmanaged functions that MQL can import, with static functions used in the example.
- The sample limits exchanged values to integers, strings, and doubles to reduce type compatibility problems.
- A database can preserve trading records and make filtering, aggregation, and statistical analysis easier.
- The example is educational and requires added features for more demanding production use.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.