Reentrancy Safe Tick Template
MQL4's terminal is single-threaded - it never calls OnTick() on a chart a second time while a previous call on that same chart is still executing. That fact leads a lot of EA authors to assume no reentrancy guard is ever needed. It is still worth building one, for two reasons this template makes concrete. First, a slow OnTick() that itself triggers something which re-enters the same code path (a chart object's OnChartEvent handler wired to call trading logic, a nested retry loop that lets the message queue process a stray click) is a real, if unusual, way to end up running trading logic twice while logically "inside" one tick. Second, and far more common in practice: the same tick calling the same decision logic twice through an ordinary code path bug (a signal check duplicated in two branches, a function called from both OnTick() and OnTimer()) has exactly the same symptom - a duplicated order - and needs the same fix. Three guards, each independently useful, are shown together. GUARD 1 is a busy flag set the instant real work starts and cleared through a single exit path in OnTick() itself - if it is ever observed already true, that is the reentrancy case, and the call is skipped rather than trusted. GUARD 2 is an idempotency key: "have we already acted on this exact (bar time, signal direction) pair" is checked before placing an order, so a duplicate call for the same signal instance is a safe no-op rather than a second trade. GUARD 3 is an intent-then-confirm state machine around the order send itself - the state is set to PENDING before the blocking OrderSend() call, not after, so a durable record exists that "this bar already tried" regardless of whether the call succeeds, fails, or the terminal is interrupted between deciding and confirming. None of the three guards depend on each other; a real EA can adopt one, two or all three depending on how much of its own logic touches shared state across more than one code path. Notes and limitations The moving-average signal exists only to give the guards something to protect; it is not a tuned trading rule. GUARD 1's busy flag cannot currently be observed tripping under normal MQL4 execution, since the terminal itself already serialises chart events - it is included as defensive engineering for the rarer indirect-reentry path (an OnChartEvent-driven UI element calling into trading logic) and as the general discipline worth teaching, not because this specific demo can trigger it. GUARD 2 and GUARD 3's state resets on every bar transition by design - the guards protect against acting twice on the SAME decision, never against acting once per bar, which remains the EA's own intended behaviour.
Expert advisor · MT4 · v1.0 · Free
About Reentrancy Safe Tick Template
Reentrancy Safe Tick Template is an expert advisor (trading robot) for MetaTrader 4 based on Moving Average. It has 4 settings, such as Trade symbol, Lots and Magic. MQL source code included.
Files in the download: ReentrancySafeTickTemplate.ex4, ReentrancySafeTickTemplate.mq4, ReentrancySafeTickTemplate.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Trade symbol | Symbol to trade, blank = current chart symbol | |
| Lots | 0.01 | Lot size for the demo entry |
| Magic | 48701 | Magic number for this EA's own orders |
| Slippage points | 30 | Max slippage, points |
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 → Experts.
- 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 expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Buy/Sell Arrows · Moving Average · MT4 Expert Advisor · MQL4 Source Code