Protect EA from changing chart period - Is it possible?

 

Hi Coders!

Is it possible to protect the EA to prevent changing the period of the chart?

My first opinion is the following code in the deinit() function:

int deinit(){

int ret=MessageBox("Remove EA?", "Question", MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2);
if(ret==IDYES){
   Print("Deinit - YES");
}
if(ret==IDNO){
   Print("Deinit - NO");
   start();                    // call the start() funciton
}

This doesn't work, because the MessageBox doesn't appear when it is called from the deinit() function. And I can't call the start() function from the deinit() function.

So, can we make the EA to ask the user manual conformation for stop working (remove EA, restart EA, change chart period)?

Thank you.

Relative

 
Relative:

Hi Coders!

Is it possible to protect the EA to prevent changing the period of the chart?

My first opinion is the following code in the deinit() function:

This doesn't work, because the MessageBox doesn't appear when it is called from the deinit() function. And I can't call the start() function from the deinit() function.

So, can we make the EA to ask the user manual conformation for stop working (remove EA, restart EA, change chart period)?

Thank you.

Relative


To prevent i dont know, but i handle like this in the start function ... at least i dont recive wrong signals .. :-)

if (Period()!=PERIOD_M5) //PERIOD_M5 is only example

{

Comment("Not work in this Timeframe "+Period());

return(0);

}

 

Thank you for the reply EADeveloper.

My EA works on all timeframe. But, when you change the period, you restart the EA, and then it losts all datas working with.

 
Relative:

Thank you for the reply EADeveloper.

My EA works on all timeframe. But, when you change the period, you restart the EA, and then it losts all datas working with.


in this case use global variables or store into a file!
 
Yes, I thought about it. But I would like to find a simpler way.
 
Relative:

My EA works on all timeframe. But, when you change the period, you restart the EA, and then it losts all datas working with.

It doesn't lose 'all' data - global and static variables retain their values (as well as extern's and arrays which are both static by default). Your EA should be designed to go through a "deinit() -> init() -> start()" cycle even if you make sure that nobody changes time-frame, since this cycle also happens when u change extern parameters and sometimes by a temporary disconnection from server.

I think using GV's or a file for this is not necessary, they should only be used for a persistence layer (in case Terminal shuts off completely), but for the simple "deinit() -> init() -> start()" cycle global and static variables are sufficient.

 
gordon:

It doesn't lose 'all' data - global and static variables retain their values (as well as extern's and arrays which are both static by default). Your EA should be designed to go through a "deinit() -> init() -> start()" cycle even if you make sure that nobody changes time-frame, since this cycle also happens when u change extern parameters and sometimes by a temporary disconnection from server.

I think using GV's or a file for this is not necessary, they should only be used for a persistence layer (in case Terminal shuts off completely), but for the simple "deinit() -> init() -> start()" cycle global and static variables are sufficient.


Thank you Gordon!

I will check up these static variables...

 
Relative:
Yes, I thought about it. But I would like to find a simpler way.

What about a power spike that reboots the machine or the Terminal crashes? Global variables will not be saved.

Code it to automatically recover. Rebuild info from the chart, open orders, closed orders, or you need persistent storage. There is no simply way

 
Relative:

Thank you for the reply EADeveloper.

My EA works on all timeframe. But, when you change the period, you restart the EA, and then it losts all datas working with.

This is easy to avoid. Store all values that need to be persisted with GlobalVariableSet() immediately whenever they change and restore them all with GlobalVariableGet() in init().


This will also make it more reliable when MT4 or Windows crashes, accidental chart closing, etc. You should design all your EAs like this from the beginning on, make it a necessary requirement and rigorously test it by intentionally closing MT4, removing the EA, closing the chart window, etc. until it works.


GlobalVariableSet() /..Get() will survive an unclean shutdown (crash) of MT4 or Windows. Store them immediately whenever they change and restore them in init().

 

I think a distinction should be made between retaining variable values across a "deinit() -> init() -> start()" cycle vs. retaining them across a complete Terminal restart (a.k.a. a 'persistence layer'). Although many variables fall under both categories, it is not always the case. Regardless, proper EA design requires a good understanding of the behavior of: static/global variables vs. local variables, GlobalVariable's, any kind of persistence layer that relies on files.

Another important consideration is the behavior of extern variables when a "deinit() -> init() -> start()" cycle is triggered -> https://www.mql5.com/en/forum/119716. In MQL5 they solved the problem by making extern variables (which are actually called Input variables in MQL5) const ; they can only be changed via the program properties window. This is not the case in MQL4.

Reason: