Common Folder Leader Election
Running the same EA on several terminals or several charts for redundancy creates a new problem: something has to be the ONE primary doing the actual work (placing trades, writing a shared log, driving a dashboard) while the rest sit as silent standbys, or every instance duplicates the same action. MQL4\Files\ (or MQL5\Files\ on the native MQL5 build)Common\ is the one folder every terminal instance on a machine can both read and write, which makes it the natural place for instances that do not otherwise know about each other to agree on a single leader. The lease record is an owner id, a last-renewal timestamp, and a term counter, written as a small CSV line. TryAcquireOrRenew() is both the election attempt and the heartbeat: called on a fixed interval well inside the TTL, it becomes (or stays) leader when the lease is free, already owned by this instance, or has gone stale past ttlSeconds since its last renewal - and does nothing when a different instance's lease is still live. A takeover bumps the term counter, a plain renewal does not, so any instance polling CurrentTerm() can tell "the same leader renewed" from "leadership actually changed hands" without comparing owner ids itself. This is the same owner-id/timestamp/TTL shape as this catalogue's generic FileLeaseLockManager advisory lock, deliberately reimplemented here rather than included from it - every submission in this catalogue is self-contained and never includes another project's file, so the lease logic is written fresh against this library's own leader-specific API (IsLeader, Resign, CurrentLeader) instead of wrapping a shared dependency. Notes and limitations - This is advisory election, not enforcement: nothing stops a program that never calls TryAcquireOrRenew() from acting as if it were leader anyway. It only coordinates instances that actually adopt the convention. - The lease has no built-in fencing token beyond the term counter - a leader that goes into a long stall past the TTL and then wakes up can briefly believe it is still leader until its next poll notices the term changed. Any leader-only action with real consequences should check IsLeader() again immediately before acting, not rely on a cached true from several polls ago. - MQL4\Files\ (or MQL5\Files\ on the native MQL5 build)Common\ is shared per machine, not per broker or account - running several unrelated deployments through the same lock filename on one machine will make them compete for the same lease. Use a filename specific to the deployment. - Resign() only releases the lease if it still shows this instance as the current owner, so it cannot accidentally delete a lease already taken over by someone else.
Script · MT5 · v1.0 · Free
About Common Folder Leader Election
Common Folder Leader Election is a script for MetaTrader 5. It has 1 setting, such as Ttl seconds. MQL source code included.
Files in the download: CommonFolderLeaderElection.mq5.mqh, CommonFolderLeaderElection.txt, CommonFolderLeaderElectionDemo.ex5, CommonFolderLeaderElectionDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Ttl seconds | 30 | Lease TTL in seconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Dashboards & Scanners · MT5 Script · MQL5 Source Code