Loading…

Circuit Breaker And Backoff Policies

Most retry code in published experts is a while loop with a fixed Sleep. That is fine for one transient requote. It is how accounts get their IP banned by a broker's API gateway when the endpoint is actually down for ten minutes and the loop keeps hitting it every second regardless. This header implements the circuit breaker pattern used in server backends, adapted to the single-threaded, tick-driven model both MetaTrader platforms share. A breaker starts CLOSED and lets every request through while counting consecutive failures. Once the failure threshold is reached it trips OPEN and refuses everything until a backoff window elapses. After the window, exactly one probe is allowed through as HALF_OPEN: enough consecutive successes closes the breaker again, a single failure re-trips it with a longer backoff. The backoff itself uses "equal jitter" - the delay is randomised inside the top half of the exponential window, never the whole window. Full jitter (0 to the window) is common in AWS-style retry guidance but has a real weakness here: it can hand back a near-zero delay immediately after a trip, which means a struggling endpoint gets hit again within milliseconds. Equal jitter guarantees a genuine minimum rest while still avoiding every instance retrying in lockstep. Timing is measured with GetTickCount(), not TimeCurrent(). TimeCurrent() only advances once a second, which makes it useless for backoff windows under a second and produces the classic bug where a breaker configured for a 300 ms retry silently waits a full second (or longer) instead. GetTickCount() is millisecond resolution, and the unsigned subtraction used throughout stays correct across its ~49.7 day wraparound without any special-case code. The exponential backoff calculator, BackoffDelayMs, is exposed standalone. Use it directly in any retry loop that does not need the full breaker state machine - a single WebRequest retry, a reconnect attempt, anything where you just need "wait a bit longer each time, with jitter" and nothing more. One breaker instance protects one resource. Create one per symbol, per endpoint, per anything that can fail independently - a single global breaker would trip every symbol's trading because one broker API call timed out. Notes and limitations - Single-threaded by design, matching both platforms' single-threaded, tick-driven execution model: AllowRequest, RecordSuccess and RecordFailure are meant to be called from one place (one OnTick path), not from overlapping calls. There is no internal locking. - The half-open state allows every call through until a success or failure is recorded, not just one physical request. If your code can issue several requests per tick, gate them so only one probe happens per half-open episode. - GetTickCount() wraps around after roughly 49.7 days of terminal uptime. The unsigned arithmetic used here handles that correctly, but if you read m_openedAtMs directly (there is no accessor - use the provided methods instead) do not compare it with signed subtraction. - This header does not retry anything itself. It only tells you whether to attempt the call and lets you report the outcome - the call and its retry loop remain the caller's responsibility.

Script · MT5 · v1.0 · Free

About Circuit Breaker And Backoff Policies

Circuit Breaker And Backoff Policies is a script for MetaTrader 5. It has 3 settings, such as Failure threshold, Success threshold and Base delay ms. MQL source code included.

Files in the download: CircuitBreaker.mq5.mqh, CircuitBreaker.txt, CircuitBreakerDemo.ex5, CircuitBreakerDemo.mq5.

Settings

SettingDefaultWhat it does
Failure threshold3Consecutive failures before the breaker trips
Success threshold2Consecutive half-open successes needed to close
Base delay ms300Base backoff window in milliseconds

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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria