Loading…

Modification Burst Batcher

MQL4 has exactly one trade context for the whole terminal, shared across every EA and every order. A trailing routine that walks the order pool and fires OrderModify() on every eligible order back to back is a common way to trip ERR_TRADE_CONTEXT_BUSY on the later calls in the sweep, especially with several EAs or a large order count on the same account - the calls that fail are exactly the stops that most needed to move. This EA rebuilds a modification queue each pass from the current order pool: any order (subject to an optional magic filter) that has moved far enough into profit to start trailing, and whose computed new stop would be a real improvement over its current one past a minimum-change threshold, gets queued. It then drains only a bounded number of entries from that queue per pass, waiting out IsTradeContextBusy() before each call and pausing a configurable interval between successful modifications. Anything left in the queue simply gets rebuilt and reconsidered next pass - there is no separate "pending" state to lose track of. A transient failure (busy context, requote, timeout) is retried a bounded number of times before being logged as given up for that pass; a permanent failure (e.g. invalid stops) is not retried at all, since retrying it would only waste the burst budget on something that cannot succeed. Notes and limitations The stop is only ever moved in the trade's favour - toward locking in more profit - never loosened, and never placed inside the broker's own stop-level distance for that symbol. Sleep() calls used to pace the burst and to wait out a busy trade context block this EA's own event processing for their duration. At the default settings that is a few hundred milliseconds at most per pass, not a concern in practice, but very large InpBurstIntervalMs or InpMaxRetryAttempts values would extend it. Anything left unmodified after a burst is simply picked up again next pass - there is no guarantee every eligible order is trailed within any specific time bound if the order count consistently exceeds InpMaxModificationsPerBurst per InpPollSeconds. This EA never opens or closes a position - it only modifies the stop-loss of orders that already exist.

Expert advisor · MT4 · v1.0 · Free

About Modification Burst Batcher

Modification Burst Batcher is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 8 settings, such as Trail start pips, Trail distance pips and Min change pips. MQL source code included.

Files in the download: ModificationBurstBatcher.ex4, ModificationBurstBatcher.mq4, ModificationBurstBatcher.txt.

Settings

SettingDefaultWhat it does
Trail start pips15.0Profit, pips, before trailing begins for an order
Trail distance pips10.0Stop distance behind price once trailing is active
Min change pips1.0Minimum SL improvement, pips, before an order is queued
Magic filter0Only manage this magic number, 0 = every order
Max modifications per burst3Cap on OrderModify calls per timer tick
Burst interval ms200Pause between modifications within a burst, milliseconds
Poll seconds1How often the queue is rebuilt and a burst is drained
Max retry attempts3Retries for a modification that hits a retryable error

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: Trailing Stop & Break-even · Alerts · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria