Skip to content
All library documents

Reconciling EA Trade State and Entering Safe Mode on Recovery Failures

Article MQL5 articles

Summary

The article describes defensive recovery for an MQL5 Expert Advisor that manages trades using runtime state and SQLite records. Its validation function checks whether a managed broker position, in-memory trade state, and database record agree. It handles two mismatches: an open position without a saved record, and a saved active record without a corresponding broker position. On either failure, the EA enters Safe Mode and suspends automated trade management rather than acting on unverified protection settings.

The method is integrated into both startup recovery and periodic timer processing. The article reports two demonstrations: deleting a recovery record while leaving a position open, and manually closing a position while its record remains active. Both reportedly triggered the controlled response. This is an implementation and reliability lesson, not evidence of trading returns. Safe Mode reduces the risk of applying incorrect virtual stops or trailing logic, but it also leaves the trade without those automated management actions until recovery integrity can be restored.

Key ideas

  • Recovery should be trusted only when broker positions, runtime state, and persisted records agree.
  • A position without its recovery record may lack essential virtual protection and management details.
  • Startup recovery and periodic validation can detect different points of state divergence.
  • Safe Mode suspends automated management when the EA cannot verify its stored trade state.
  • The demonstrations cover two mismatch cases but do not measure trading performance.

Tags

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