What code can I put in my EA that will completely Restart it.. Like reload EVERYTHING...
Reload the externs, init()and start() functions .... Aswell as reload the chart...
An expert can be designed to "reset" itself without actually going through an actual "restart". But for the sake of argument (and because it's an interesting question), let's say we actually want a "restart"... From https://docs.mql4.com/runtime/start we know that init() would be called in the following cases:
The question is in which of these cases do static, global and extern variables initialize themselves?
- "...just after client terminal has started..." -> Yes.
- "...after the symbol and/or chart period have been changed..." -> No.
- "...after the program has been
recompiled in MetaEditor..." -> Yes.
- "...after inputs have been changed from the window..." -> No.
- "...after the account has been changed..." -> No.
- For restarting the Terminal, see info here -> https://www.mql5.com/en/forum/123488.
- Recompiling seems to work only if done from MetaEditor... When compiling with MetaLang.exe via command-line, the expert does not seem to "know" that it has been recompiled (it just continues normally). I doubt there is any way to press the compile button in MetaEditor via an expert (?).
Here's a small experiment proving #2:
#import "shell32.dll" int ShellExecuteA( int hwnd, string Operation, string File, string Parameters, string Directory, int ShowCmd ); #import int start() { static int cnt = 0; Print("cnt=",cnt); cnt++; // command line options for MetaLang.exe launched via ShellExecuteA() string options = " \"" + TerminalPath() + "\\experts\\" + WindowExpertName() + ".mq4\""; if(cnt==5) int ret = ShellExecuteA(0,"open","metalang.exe",options,TerminalPath(),7); return(0); }Running this, the expert is indeed recompiled after 5 ticks, but the expert does not reinitialize itself... Counting continues normally.
Just a thot...
Is not init() called for each of 1..5 ?
If so, [and this is maybe messy idea] each function [to incl. start()] which has need of static memory [to incl. any type resized []'s], could be called from init()
The called code would have to detect an init() set global scope flag which says "reset to compile time state" and once done the function would immediately return without doing any main body code.
Once init() has called all such functions and also reset any program global memory needing a known compile time value, init() would return to terminal.
start() on entry would detect this global scope flag which says "reset to compile time state" and having done any such work itself, would set this flag false and continue into main body code.
Alternately, start() could be seen as just another function needing to reset when called by init() and detects this reset flag. Having reset, start(), like the other functions, would immediately return.
Assuming start() is the last called function, init() would then set global flag false and return to terminal.
only overhead would be the need to check a bool flag in all functions which participate in this reset scheme...
As said, is a messy? idea. But is not that bad to ensure total EA reset.
This of course means that init() would either detect via UninitializeReason() or by default, always do the reset. Depends on EA design I'd say.
p.s. Now that I re-read Xlr8er's post, he is actually also asking to "reload the chart"... So for that the only solution is restarting the Terminal.
ok, is true. The post's semantics and hence my interpretation were guilty of 'lost in the translation' as " .... Aswell as reload the chart... " was, in my reading not there.
So yes, I am guilty of not reading each word and also coupled with the subject matter, which has been of great interest at one time and having not ever read anywhere of a psbl design... I as it were, went off half cocked.
Age has it's faults :-)
Nevertheless, was interesting to ramble on about a psbl design. Anyway, your using the words "good example" were heartening...
ok, is true. The post's semantics and hence my interpretation were guilty of 'lost in the translation' as " .... Aswell as reload the chart... " was, in my reading not there.
So yes, I am guilty of not reading each word and also coupled with the subject matter, which has been of great interest at one time and having not ever read anywhere of a psbl design... I as it were, went off half cocked.
Age has it's faults :-)
Nevertheless, was interesting to ramble on about a psbl design. Anyway, your using the words "good example" were heartening...
@Gordon/FBJ
I have an EA that i will like to reset its external variables to the default setting within the EA after a trade is taken, can you advise on how to achieve that?
Thanks
Iddy
What code can I put in my EA that will completely Restart it.. Like reload EVERYTHING...
Reload the externs, init()and start() functions .... Aswell as reload the chart...
The chart is already current.
Don't modify externals, use copies. https://www.mql5.com/en/forum/135417
Alternative is to restart the terminal but that will effect all EAs and if they're not properly coded to recover you'll have big problems
The chart is already current.
Don't modify externals, use copies. https://www.mql5.com/en/forum/135417
Alternative is to restart the terminal but that will effect all EAs and if they're not properly coded to recover you'll have big problems
Seriously? Probably to help coders who are starting today, rather than pre-2014. I for one appreciate the up-to-date advice. That being said, your comment about the results of restarting the terminal still needs to be considered and/or handled.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
What code can I put in my EA that will completely Restart it.. Like reload EVERYTHING...
Reload the externs, init()and start() functions .... Aswell as reload the chart...
Thank you very much