Loading…

File Locking Collision Demonstrator

"Two EAs writing the same signal file sometimes can't open it" is usually read as a race condition, and coded around with retry loops and delays. It is almost always FILE_SHARE flags instead: MQL4 opens a file EXCLUSIVELY by default, so a second FileOpen() on the same path fails outright while the first handle is still open - not occasionally, every time, from any program including a second call from the SAME program. Rather than describe that behaviour, this script reproduces it directly, in two stages, on one real file in the sandbox. Stage one opens the file twice in a row with plain FILE_WRITE|FILE_BIN flags; the second open fails while the first handle is open, and the exact GetLastError() code is printed, not assumed. Stage two repeats the same two-open sequence on the same file, this time adding FILE_SHARE_READ | FILE_SHARE_WRITE to both calls - both succeed, and the script proves they are genuinely sharing the same file (not just two handles that happen to both be open) by writing a marker value through the first handle and reading it back through the second while both remain open. The practical rule the demonstration supports: sharing only works when EVERY caller that touches the file adds the FILE_SHARE_* flags, not just the first one written. One program omitting them locks every other caller out, even ones that specified the flags correctly. Notes and limitations Both handles in this demonstration are opened by the same script instance, because two genuinely separate running programs cannot be launched from one CodeBase submission. This does not change the behaviour being shown - MT4 enforces exclusive access per FILE path at the terminal/OS level, the same mechanism that blocks two separate EAs, so a second handle from the same script collides for exactly the same reason a second program would. The test file lives in this terminal's MQL4\Files sandbox and is deleted at the end of the run; if the script is stopped mid-run (unlikely, it completes in milliseconds) a leftover file may need manual deletion.

Script · MT5 · v1.0 · Free

About File Locking Collision Demonstrator

File Locking Collision Demonstrator is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 1 setting, such as File name. MQL source code included.

Files in the download: FileLockingCollisionDemo.ex5, FileLockingCollisionDemo.mq5, FileLockingCollisionDemo.txt.

Settings

SettingDefaultWhat it does
File nameFileLockingCollisionDemo_test.binSandbox file used for the demo (deleted afterwards)

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Buy/Sell Arrows · Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria