This repository is an article-derived reference project based on the original MQL5 article. It does not claim to reproduce the full original source code unless files are explicitly attached.
## Overview
This project documents the first part of a self-healing Expert Advisor architecture for MetaTrader 5. The article focuses on persistence: storing trade-management state in a local SQLite database so the EA can restore its internal state after terminal restarts, VPS interruptions, or crashes.
The implementation described in the article establishes:
- EA lifecycle scaffolding
- runtime trade-state modeling
- SQLite database initialization
- table creation
- saving active trade state
- checking whether saved state exists
- loading saved trade state back into memory
## Original Article
- Article ID: 22532
- Author: Chacha Ian Maroa
- Publication date: 2026.05.29
- Category: Experts
- URL: https://www.mql5.com/en/articles/22532
## Repository Purpose
This repository serves as a reference/reconstruction project for the article’s persistence layer. Its purpose is to preserve the article’s core design and code structure for:
- restart-safe EA architecture
- persistent storage of virtual trade-management state
- SQLite-backed recovery workflows in MQL5
- future extension into live recovery logic in later parts of the series
## Key Concepts
- Volatile runtime memory vs persistent storage
- Recovery-sensitive trade metadata
- Local SQLite database usage in MQL5
- EA operational state machine
- Virtual SL/TP persistence
- Breakeven and trailing-stop recovery tracking
- Timer-based background continuity checks
- Rebuilding runtime state from saved database records
## Algorithm / Architecture Summary
The article defines a basic Expert Advisor named `SelfHealingExpert.mq5` with:
-`ENUM_TEST_TRADE_DIRECTION`
-`ENUM_EA_STATE`
-`STradeState` for persistent operational trade data
- global runtime variables for EA state, active trade state, and database handle
- standard event handlers: `OnInit`, `OnDeinit`, `OnTick`, `OnTimer`
Persistence architecture described in the article:
1. Open or create a local SQLite database using `DatabaseOpen`.
2. Ensure the `trade_states` table exists with `CREATE TABLE IF NOT EXISTS`.
3. On EA startup, initialize the database and timer.
4. Save active trade state with `SaveTradeState()` using `INSERT OR REPLACE`.
5. Check for saved state existence with `TradeStateExists(ticket)`.
6. Restore saved state into `STradeState` with `LoadTradeState(ticket, state)`.
7. Close the database cleanly in `OnDeinit()`.
The database file is described as:
-`MQL5/Files/self_healing_trade_manager.sqlite`
The table stores fields corresponding to the runtime structure, including:
- This repository is based on article content and should be treated as a reference/reconstruction unless the attached source file is actually present in the repository.
- The processed input does not provide the article ID, publication date, or category metadata.
- The article describes only the persistence foundation, not the full self-healing trading system.
- Live trade recovery, managed position detection, virtual SL/TP enforcement, and restart validation are stated as future parts of the series.
- No installation or deployment workflow beyond article-level guidance should be assumed.
- If the attached source file is missing from the repository, the full original source code is not available here.
## Reference
Original article page: https://www.mql5.com/en/articles/22532