Skip to content
All library documents

MQL5 Order Execution with Retries and a Circuit Breaker

Article MQL5 articles

Summary

This article presents a reusable execution layer for MQL5 Expert Advisors. It separates transient submission failures, such as requotes or temporary connection loss, from permanent rejections such as invalid stops or insufficient margin. A retry executor applies capped exponential backoff to retryable return codes, while reporting partial fills as outcomes for the calling strategy to handle. The delay gives a broker time to recover and, because it blocks the EA thread, also prevents another tick from immediately triggering the same submission path.

A circuit breaker tracks failures across orders and moves among closed, open, and half-open states. It blocks submissions after a configured failure threshold, waits through a cooldown, then permits limited probes before restoring normal operation. A gateway combines the retry and circuit-breaker components, and the article describes tests and a demonstration using simulated failures. These examples verify implementation behavior rather than live broker reliability or strategy performance. The author also describes checking existing positions and orders to reduce duplicate submissions; integration and configuration remain important to safe use.

Key ideas

  • Retry policy should distinguish transient server conditions from permanent order rejections.
  • Capped exponential backoff increases the delay between successive retry attempts.
  • A circuit breaker blocks new submissions after repeated failures and uses a cooldown and probes to test recovery.
  • Partial fills are reported to the caller because the execution layer cannot decide how a strategy should handle remaining volume.
  • The included unit tests and demonstration use simulated conditions and do not establish live execution performance.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.