Discussing the article: "Engineering a Self-Healing Expert Advisor in MQL5 (Part 2): Restart-Safe Virtual Trade Protection"

 

Check out the new article: Engineering a Self-Healing Expert Advisor in MQL5 (Part 2): Restart-Safe Virtual Trade Protection.

Build a restart-aware virtual protection layer on top of the SQLite persistence from Part 1. The EA reconstructs hidden stop-loss and take-profit after restart, verifies current price against recovered exits, and closes or continues positions accordingly. The result is a consistent recovery path that detects managed positions and sustains safe runtime management.

In a virtual protection system, the stop-loss and take-profit levels exist only inside the runtime memory of the Expert Advisor. The EA continuously monitors market prices during execution and manually closes positions whenever the virtual protection conditions are reached.

Under normal runtime conditions, this architecture functions correctly. However, the situation changes immediately once the trading terminal shuts down unexpectedly. Consider the following scenario:

  1. EA opens a BUY trade.
  2. Virtual stop-loss stored internally.
  3. Terminal shuts down.
  4. Price crosses hidden stop-loss.
  5. EA restarts. Trade remains open unexpectedly.

This problem occurs because the runtime memory is volatile. Once the terminal closes, the Expert Advisor loses access to its internal protection state and stops monitoring the active position completely.

During this offline period, the market continues moving normally. If price crosses the hidden stop-loss while the Expert Advisor is no longer running, the broker remains unaware because no broker-side stop-loss was ever placed. After a restart, the trade may still remain open even though the intended protection conditions were already violated earlier.

Author: Chacha Ian Maroa