How Can I completely Restart an EA within the codeing

 

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

 
Xlr8er:

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 init() function of an expert advisor or a custom indicator attached to the chart will run just after client terminal has started and history data (this concerns only experts, but not indicators) have been loaded additionally, after the symbol and/or chart period have been changed, after the program has been recompiled in MetaEditor, after inputs have been changed from the window of expert or custom indicator settings. An expert will also be initialized after the account has been changed.

The question is in which of these cases do static, global and extern variables initialize themselves?

  1. "...just after client terminal has started..." -> Yes.
  2. "...after the symbol and/or chart period have been changed..." -> No.
  3. "...after the program has been recompiled in MetaEditor..." -> Yes.
  4. "...after inputs have been changed from the window..." -> No.
  5. "...after the account has been changed..." -> No.
So our only choice is to recompile the expert or restart the Terminal.
  1. For restarting the Terminal, see info here -> https://www.mql5.com/en/forum/123488.
  2. 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 occurred to me that the expert might be using libraries or dll's. Obviously these would be "restarted" in case of restarting the Terminal, but I'm not sure what happens in case of a recompile...?
 

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.

 
fbj, that's exactly what I meant when I said "An expert can be designed to "reset" itself..." in the begining of my post - you gave a good example of how to design an expert to "reset" itself. But that's not Xlr8er's question - he asks how to completely "reload EVERYTHING" - how to "restart" the expert (as if u just attached the expert to the chart). The only practical solution for that is to have the expert restart the Terminal via methods as described in that link.

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...

 
fbj:

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

 
Xlr8er:

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

 
whroeder1:

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

simply change symbol and bring back again.ChartSetSymbolPeriod();
 
Why are you posting to a 7 year old thread with a solution that wasn't available before 2014
 

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.

Reason: