Discussing the article: "CSV Data Analysis (Part 1): CSV Export Engine for MQL5 Multi-Core Optimizations"

 

Check out the new article: CSV Data Analysis (Part 1): CSV Export Engine for MQL5 Multi-Core Optimizations.

Multi-core optimization in MetaTrader 5 can silently drop results when parallel agents contend for the same CSV file. A reusable MQL5 export engine applies an iteration-based spin-lock to acquire the file handle reliably and append rows without loss. It persists custom metrics such as the Sortino Ratio, average trade duration, and signal-quality measures (lag and whipsaws) into a consolidated CSV for downstream analysis.

This is the framework's central technical problem; it should be understood before reviewing the solution.

When the MetaTrader 5 Strategy Tester runs an optimization with "Use multi-core processing" enabled, it distributes individual parameter combinations across multiple parallel agent processes. Each agent runs an independent EA instance with a specific parameter set. At the conclusion of each run, OnDeinit() is called on that instance.

If the optimization spans multiple filter types or parameter ranges, several agents will reach their OnDeinit() call at approximately the same time. When multiple agents call OnDeinit() and try to open the same CSV for writing, the following occurs:

  1. Agent A calls FileOpen() on MultiFilter_InSample_EURUSD_H1.csv. The operating system grants Agent A the write lock. The file handle is valid.
  2. Agent B calls FileOpen() on the same file a few microseconds later. Because Agent A holds the write lock, Agent B receives INVALID_HANDLE. The error code from GetLastError() is typically ERR_CANNOT_OPEN_FILE (5004).
  3. Agent B's export call silently fails. That entire row of results — representing a complete optimization pass — is permanently lost.

Fig. 1: A concurrency conflict occurring during multi-core optimization.

Author: Ushana Kevin Iorkumbul

 
You don't need things like "Parallel File-Lock" at all - just send data from the agents in frames and save them into cumulative CSV-file from the single place - the terminal chart running your EA in the frame mode.