EA gets restarted when timeframe changes - page 2

 
WindmillMQL:

I have coded my first EA and am now running it on a demo account. While doing this I noticed an effect which is undesired:

I have a chart opened (e.g. EURUSD, H1) and have started the EA on this chart. Autotrading is enabled and the EA seems to be doing what it is supposed to be doing.

In order to see the trades in more detail I change the chart's timeframe from H1 to M15. To my surprise do I see that EA runs its OnDeinit() on the H1 chart, exits, restarts and runs OnInit() on the M10 chart.

This is not what I wanted. I wanted my EA to continue running uninterrupted. My EA does not depend on the timeframe of the chart, so the functionality of the EA does not change when I change any of the chart settings.


My question: what setting, or code, should I use to have the EA uninterrupted when I change  the chart's timeframe?

I think sometimes we have to accept what MT4 can and can't do. Others may have ASKED for EAs to DeInit on a change in TF ;)

I try to code "in the moment", so I don't rely on things like terminal global variables, files or even arrays for EA critical processes. That way, if something is amiss, the EA will just continue on when the TF changes or the VPS restarts, say.

 
WindmillMQL:

This sounds interesting. So you store certain data upon OnDeinit(). I assume that you load that data on the next OnInit()?

I had not heard of this possibility. How do I know which parameters to store, where to store them (database?), and how to reload the data?

Sorry for the many questions. I'm new to programming an EA and am still learning the ropes.

When OnDeinit() is executed it deletes or not the GV depending of the reason for shutting down the EA.

The graphical interface I create to control the EA from the chart directly takes priority over the EA settings. Therefore, all options that can be adjusted from the chart are stored as GlobalVariables. When OnInit() is executed it checks for a GV is present, if true it then loads the settings from GV

 
Fernando Morales:

When OnDeinit() is executed it deletes or not the GV depending of the reason for shutting down the EA.

The graphical interface I create to control the EA from the chart directly takes priority over the EA settings. Therefore, all options that can be adjusted from the chart are stored as GlobalVariables. When OnInit() is executed it checks for a GV is present, if true it then loads the settings from GV

Thank you Fernando for your explanation on how you use global variables upon OnDeinit(). I have meanwhile read some of the MQL5 Reference manual and found the section where these functions are explained. I will study some more and see whether I can use this approach in my EA.
 
andrew:

I think sometimes we have to accept what MT4 can and can't do. Others may have ASKED for EAs to DeInit on a change in TF ;)

Yes, you could be correct that others asked to have this functionality.

It would have been more versatile if a parameter could be set where one chooses whether the EA restarts or not after changing the time frame. Many people would prefer a default setting of "yes, restart EA", whereas I would have set that switch to "no, don't touch my EA". Anyway, it is what it is and I will look for a way around it.

 
Roberto Jacobs:

Set in your EA code the timeframe that the EA will use.

Maybe you should make strict timeframes with the ChartSetSymbolPeriod function.

Using ChartSetSymbolPeriod() will re-initialize the EA, so it won't solve the OP's problem

 
Keith Watford:

Using ChartSetSymbolPeriod() will re-initialize the EA, so it won't solve the OP's problem

Not so, if you try to change timeframes, then timeframes will not be replaced at all.

I think that's what was asked by WindmillMQL. example:

if(!ChartSetSymbolPeriod(ChartID(),Symbol(),PERIOD_M5))
   ChartSetSymbolPeriod(ChartID(),Symbol(),PERIOD_M5);
 
Roberto Jacobs:

Not so, if you try to change timeframes, then timeframes will not be replaced at all.

I think that's what was asked by WindmillMQL. example:

Sorry, but I have no idea what you are saying.

 
Keith Watford:

Sorry, but I have no idea what you are saying.

Yeah. I'm sorry too.

My response only relates to the question:  == My question: what settings, or code, should I use to have the EA uninterrupted when I change the chart's timeframe? ==

It only protects users from being able to change timeframes.

 
Roberto Jacobs:

Yeah. I'm sorry too.

My response only relates to the question:  == My question: what settings, or code, should I use to have the EA uninterrupted when I change the chart's timeframe? ==

It only protects users from being able to change timeframes.

Ah, I fear that you misunderstand my original question.

My purpose was to be able to zoom out and zoom in on the chart. To see a more detailed picture, or a more broader overview of how prices were developing. And how the EA is responding to that by placing trades.

By zooming in&out I discovered that the EA gets restarted each time I change the time frame. I had not expected this and was surprised by this behaviour of MetaTrader.

So I want to have the exact opposite of what you are suggesting: I want to be able to change the time frame of the chart. But it should not affect the running EA.

From other replies in this thread I understand that this is not possible.