Skip to content
All library documents

Persistent Trade State Recovery for MQL5 Expert Advisors

Article MQL5 articles

Summary

The article explains why an Expert Advisor can lose critical management context after a terminal or VPS restart. Virtual stop-loss and take-profit levels, breakeven status, and trailing-stop progress may exist only in runtime memory, even while the broker-held position remains open. Without restoring that information, virtual protection and management can fail during recovery.

It presents a persistence foundation using a local SQLite database and a structured trade-state record. The described components initialize the database, save and check for trade records, and reload stored values into the EA’s runtime state. The article frames these mechanisms as groundwork for later recovery features, including position detection and post-restart protection checks. It gives an illustrative failure scenario but no measured reliability or trading-performance results. The approach is limited to the persistence layer in this installment; successful recovery also depends on later logic and correct handling of database and broker state.

Key ideas

  • Runtime-only variables are lost when an Expert Advisor restarts, even if its broker position remains open.
  • Virtual stops and trailing or breakeven state need durable storage to preserve management continuity.
  • A structured trade-state record can hold the operational details needed to rebuild the EA’s in-memory context.
  • SQLite routines provide a local mechanism for creating, saving, checking, and loading trade state.
  • This installment establishes persistence foundations rather than demonstrating a complete live recovery system.

Tags

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