Source code
Remember EA state after restart with terminal global variables
A value stored only in an EA variable disappears when the program is unloaded. Terminal global variables provide another storage mechanism, but they are shared terminal resources rather than private variables belonging automatically to one EA.
Understand the lifetime
The MQL5 reference distinguishes terminal global variables from program-scope globals. Terminal globals remain for four weeks after their last access, and other MQL5 programs in the same terminal can access them. They are not a permanent database.
Name the state explicitly
For a hypothetical once-per-session flag, include a strategy identifier, symbol, account context and session date in the key. Use a schema version when the meaning changes. Avoid generic names such as done that another chart could reuse.
Reconcile on startup
A saved flag can disagree with reality after a rejected request, manual closure or interrupted shutdown. Read current positions and trade history before deciding what the flag means. Repeating initialization should lead to the same state, not another trade.
Test the recovery path
In a demo environment, stop the EA before and after the important event, then restart it. Also test a missing key and an older-format key. Ask AI to document the fallback and preserve unrelated terminal variables instead of deleting everything to reset one strategy.
Back to top












