How to make an EA not lose data after MT4 shut down?

 

I have an idea for coding an EA, but at very first stage I found out that it would start making losses if by any reason MT4 would be switched off and on again. I need my EA to remember a set of data (an array to be specific) no matter what. I couldn't find any solution anywhere on the internet. My best idea is to store data in/as labels on the chart. They are not deleted nor lost when MT4 is turned off. Anyways I do believe there is a better way to do that, some kind of comand or function perhaps... I'm not that experienced programmer so I turn to more skilled fellows. I will really appreciate any ideas.

Max

 

The terminology you are seeking for is "persistence".

We refer to "it" as the persistence protocol.

People differ in their method of implementing a persistence protocol, inasmuch as we differ in our very coding styles.

My style is to log the requisite persisting information into a csv file. This csv file is opened and its contents are read/loaded during the init(). The log file is updated in start() as needed, as well as updated in deinit().

In other industries we refer to this file as the "checkpoint" file.

 
i save info to file (csv,txt), read in init() and rewrite file when status has changed, both in start() and deinit()
 
Updating in deinit() shouldn't be necessary if done right in start(). Furthermore if must be done in start as a OS crash/power failure etc won't call deinit.
 
WHRoeder:
[...] as a OS crash/power failure etc won't call deinit.
Which is exactly why deinit() could be used in a persistence layer to distinguish between an unexpected failure (OS/Terminal crash) to an expected failure ('graceful' closure of Terminal by the user).
 
gordon:
Which is exactly why deinit() could be used in a persistence layer to distinguish between an unexpected failure (OS/Terminal crash) to an expected failure ('graceful' closure of Terminal by the user).

lol, I was going to post the EXACT same thing but you beat me to it :)
 

what if a bad configured server installs some updates and autorestarts?

from my point of view that is a unplanned unexpected failure but i think deinit would be called...


would be cool if we can read the reason why deinit is being called.

but, my failsave routine is called at every change of the internal state.

 

zzuegg:

[...] would be cool if we can read the reason why deinit is being called.

https://docs.mql4.com/check/UninitializeReason.
 
Note that there are some minor issues with using UninitializeReason()... Search for ukt's discussions on the subject -> https://forum.mql4.com/search/uninitializereason?author=ukt.
 

ya. i knew that. but it would be interesting if we can differ between

a)MetaTrader is beeing shot down by user

b)windows shutdown

c)windows restart.


especially the last two. because i would close all open orders on shutdown, but not on a restart.

 
zzuegg:

[...]

b)windows shutdown

c)windows restart.

Shutdown is the same as Restart only with longer delay between the time the EA was stopped and the time it continues... This time is what matters, not what happened. You can save a time stamp every call to start() as part of the persistence layer to know this time (roughly).
Reason: