Advertisement IC Markets

Making my EA survive a restart without forgetting its state

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Fairman
Posts: 3303
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Making my EA survive a restart without forgetting its state

Post by Fairman »

Making my EA survive a restart without forgetting its state

I restarted my platform once while the EA had open trades and it behaved strangely. It had lost track of things like which trades had already had a partial close, and how many trades it had taken that day. It took some clean up to fix.

The lesson was that anything the EA needs to remember across a restart can't live only in variables. Variables disappear when the program is removed or the platform restarts. Now I store the important bits where they persist: in global variables of the terminal, or in a small file.

What I save: the count of trades today, the daily starting balance, flags for partial closes keyed by the position ticket, and the time of the last trade for cooldowns. On startup, the EA reads those values and also scans the open positions to rebuild its picture. If something doesn't match, it logs a warning.

I test by deliberately stopping the platform mid trade on a demo, and restarting. It's easy to forget, but a restart will happen eventually, due to an update, a power cut or a mistake.
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
Fairman
Posts: 3303
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Making my EA survive a restart without forgetting its state

Post by Fairman »

One thing I would add is to save the magic number logic carefully. If you run the same EA on several charts, each instance needs its own magic number so that on restart each one only picks up its own trades. I had a bug where two instances both thought they owned the same position after a restart, and one tried to manage it with the wrong rules. Scanning open positions by symbol and magic number on startup fixed it, and now each instance rebuilds only what belongs to it.
It’s Fairman :geek:
Fairman
Posts: 3303
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Making my EA survive a restart without forgetting its state

Post by Fairman »

For anyone using terminal global variables, be aware they can be cleared if you delete them by hand or if the terminal data is reset, and they only store numbers. I use them for counts and timestamps, but anything more complex goes into a small file in the files folder, written each time the value changes. I also write a version number at the top of the file so that if I change the format later, the EA can tell an old file from a new one and not misread it.
It’s Fairman :geek:
Fairman
Posts: 3303
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Making my EA survive a restart without forgetting its state

Post by Fairman »

Testing restarts deliberately on demo is the best advice in this thread. I go further and test the ugly cases: restart during the news filter, restart just after a partial close, and restart across midnight server time when the daily counters should reset. The midnight one caught a bug where the daily trade count carried over from the previous day because the saved date was not compared on startup. Without forcing those cases I would never have found it until it cost me a trade.
It’s Fairman :geek:
Post Reply