Trade Context Cooperative Mutex
MT4's trade context is one shared resource across the whole terminal: while any EA is inside a trade operation, every other EA's OrderSend(), OrderClose() or OrderModify() call gets ERR_TRADE_CONTEXT_BUSY back. IsTradeContextBusy() tells an EA that contention currently exists, but it says nothing about ORDER - two EAs both polling it and both seeing it free can still both fire in the same instant, right back into the same collision. Something more is needed than "check, then act" when there is real coordination to do, not just retry-after-busy. This header adds a real mutual-exclusion layer using terminal global variables, which are visible to every program running in the same terminal and support GlobalVariableSetOnCondition() - an atomic compare-and-set the platform provides specifically for this kind of coordination. TryAcquire() checks the lock's current holder; if it is free, it attempts to claim it with a compare-and-set against the expected previous value, so two EAs racing to acquire at the same instant cannot both win - one's compare-and-set necessarily fails because the other's succeeded first. A lock left behind by a removed or crashed EA must not deadlock every other EA that cooperates over the same name forever. GlobalVariableTime() gives the terminal's own last-write timestamp for a global variable for free, with no need to store one manually - TryAcquire() reads it and, once a lock is older than its configured stale timeout, treats it as abandoned and attempts an atomic takeover rather than waiting on it indefinitely. TryAcquire() is reentrant for the same owner: calling it again while already holding the lock simply confirms ownership rather than failing, so a caller can call it every tick without tracking acquisition state itself. Notes and limitations - This is cooperative locking, not enforced locking. It only protects code that actually calls TryAcquire() first - an EA that ignores the mutex entirely can still act on the resource regardless. Every participant must use the same lock name and actually check the result. - A held lock does not, by itself, stop ERR_TRADE_CONTEXT_BUSY from a different, non-cooperating program (a script, a manually placed trade, an EA from a different codebase entirely). It coordinates only among programs that use this same mutex. - Stale-timeout takeover is a deliberate trade-off: too short and a slow but healthy EA can be mistaken for crashed and have its lock stolen mid- operation; too long and a genuinely crashed EA blocks the others for that whole window. Choose the timeout relative to how long a real critical section under this lock is expected to take. - Global variables are per-terminal, not per-account or per-symbol. Two terminals running side by side (e.g. two accounts) do not share the same lock namespace at all - this only coordinates programs within one running terminal instance. - Global variables persist until deleted or the terminal clears them (GlobalVariablesFlush interval / manual reset) - Release() removes the demo's variable at both the end of the run and after the stale-takeover section, so repeated runs do not leave it behind.
Script · MT4 · v1.0 · Free
About Trade Context Cooperative Mutex
Trade Context Cooperative Mutex is a script for MetaTrader 4. It has 2 settings, such as Lock name and Stale timeout seconds. MQL source code included.
Files in the download: TradeContextMutex.mqh, TradeContextMutex.txt, TradeContextMutexDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Lock name | DemoLock | Shared lock name both simulated EAs contend over |
| Stale timeout seconds | 2 | Short on purpose so the demo finishes quickly |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code