Download MetaTrader 5

EA restarts when I change Periodicity

To add comments, please log in or register
Jerry Spence
216
Jerry Spence 2010.02.23 12:49 

I have an EA that calculates ADX based on PERIOD_M30. If certain things fall into line, then it Alerts me. Once an alert has been sent, I set a boolean called 'Notify' to true, so that I only get one alert. When things fall out of range again, then I reset Alert to false so it is ready for the next time.

So far so good. However, when I click on a new period button, the EA seems to restart itself from scratch, which resets my Alert booleans and I get an alert again. This didn't happen in MT4 as far as I can remember.

-Jerry

Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
MQL5 programs / Runtime Errors - Documentation on MQL5
Rashid Umarov
Admin
11988
Rashid Umarov 2010.02.23 13:31  

See Program Running:

Loading and Unloading of Expert Advisors

Expert Advisors are loaded in the following cases:

  • when attaching an Expert Advisor to a chart;
  • terminal start (if the Expert Advisor was attached to the chart prior to the shutdown of the terminal);
  • loading of a template (if the Expert Advisor attached to the chart is specified in the template);
  • change of a profile (if the Expert Advisor is attached to the one of the profile charts).
     

Expert Advisors are unloaded in the following cases:

  • when detaching an expert Advisor from a chart;
  • if a new Expert Advisor is attached to a chart, if another Expert Advisor has been attached already, this Expert Advisor is unloaded.
  • terminal shutdown (if the Expert Advisor was attached to a chart);
  • loading of a template, if an Expert Advisor is attached to the chart;
  • close of a chart, to which the Expert Advisor is attached.
  • change of a profile, if the Expert Advisor is attached to one of charts of the changed profile.

In case the symbol or timeframe of a chart, to which the Expert Advisor is attached, changes, Expert Advisors are not loaded or unloaded. In this case client terminal subsequently calls OnDeinit() handlers on the old symbol/timeframe and OnInit() on the new symbol/timeframe (if they are such), values of global variables and static variables are not reset. All events, which have been received for the Expert Advisor before the initialization is completed (OnInit() function) are skipped.

Expert Advisors are not loaded or unloaded when account is changed.


Jerry Spence
216
Jerry Spence 2010.02.23 22:14  
Rosh:

See Program Running:


Thanks Rosh. I've just created it from scratch and it doesn't do what it did before, so I obviously did something wrong.

-Jerry

Jerry Spence
216
Jerry Spence 2010.02.26 21:42  
netconuk:

Thanks Rosh. I've just created it from scratch and it doesn't do what it did before, so I obviously did something wrong.

-Jerry

I'm still confused by this. I have a Static Boolean called FirstTime, initially set to True. At the end of my EA, this is set to False.

In the OnInit() section I have MessageBox(FirstTime)

On the OnClick() section I also have MessageBox(FirstTime)

When I compile it, it is loaded and I Get "True" thrown up. Each tick it comes up with False.

When I click on a new time period, I understand that the OnInit() is called, but I get "False" thrown up, which means that my Static Variable has been reset - which it shouldn't do. In fact all my Statics are reset.

Does it matter where the Statics are defined? I've tried it at the top section under the properties:

#property version   "1.00"

static bool  FirstTime = true;

I've also tried putting it in the main section of my program under OnTick()

Thanks

-Jerry

Jerry Spence
216
Jerry Spence 2010.03.03 02:03  
netconuk:

I'm still confused by this. I have a Static Boolean called FirstTime, initially set to True. At the end of my EA, this is set to False.

In the OnInit() section I have MessageBox(FirstTime)

On the OnClick() section I also have MessageBox(FirstTime)

When I compile it, it is loaded and I Get "True" thrown up. Each tick it comes up with False.

When I click on a new time period, I understand that the OnInit() is called, but I get "False" thrown up, which means that my Static Variable has been reset - which it shouldn't do. In fact all my Statics are reset.

Does it matter where the Statics are defined? I've tried it at the top section under the properties:

#property version   "1.00"

static bool  FirstTime = true;

I've also tried putting it in the main section of my program under OnTick()

Thanks

-Jerry

This is still outstanding and is rather annoying. Can someone else please verify that they get the same problem with Static variables not being static? Or am I doing something wrong?

Thanks

-Jerry

Paul
784
Paul 2010.03.03 03:27  
netconuk posted # :

This is still outstanding and is rather annoying. Can someone else please verify that they get the same problem with Static variables not being static? Or am I doing something wrong?

Thanks

-Jerry

Not quite sure where your problem is because you haven't posted a code sample.  I don't think the static identifier would have any effect on a global variable (ie outside a function call) because globals behave similar to static for given EA load.  Anyway, this is how I would code a once-only flag:

bool FirstTime=true;  // if this behaves like MT4, this assignment will only happen on first load, not when changing timeframes.

OnInit()
{
  FirstTime=true;
// etc
}

OnTick()
{
  // do stuff
  //
  //
  FirstTime=false;
}

 

 

Paul 

 

Jerry Spence
216
Jerry Spence 2010.03.03 15:30  
phampton:
netconuk posted # :

This is still outstanding and is rather annoying. Can someone else please verify that they get the same problem with Static variables not being static? Or am I doing something wrong?

Thanks

-Jerry

Not quite sure where your problem is because you haven't posted a code sample.  I don't think the static identifier would have any effect on a global variable (ie outside a function call) because globals behave similar to static for given EA load.  Anyway, this is how I would code a once-only flag:

 

 

Paul 

 

Thanks for your help Paul

I can see with your example, that FirstTime would get reset to true each time a new period was selected as OnInit() is invoked each time which is not what I want.

However I have got the following:

static bool  FirstTime = true; \\ I believe this should not change when selecting a new time period (It doesn't in MT4)

int OnInit()
  {
//---
MessageBox((FirstTime);

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

//---

FirstTime=false;

  }

//+------------------------------------------------------------------+


Here I am declaring a Static at the start, and each time I click a new period it goes through the OnInit() section and I get the Message that FirstTime has been reset back to True, which I don't think it should.

Your first comment is the crunch - I don't believe it is behaving like MT4.

-Jerry


Paul
784
Paul 2010.03.04 08:35  
netconuk posted # :

Thanks for your help Paul

I can see with your example, that FirstTime would get reset to true each time a new period was selected as OnInit() is invoked each time which is not what I want.

However I have got the following:

static bool  FirstTime = true; \\ I believe this should not change when selecting a new time period (It doesn't in MT4)

int OnInit()
  {
//---
MessageBox((FirstTime);

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

//---

FirstTime=false;

  }

//+------------------------------------------------------------------+


Here I am declaring a Static at the start, and each time I click a new period it goes through the OnInit() section and I get the Message that FirstTime has been reset back to True, which I don't think it should.

Your first comment is the crunch - I don't believe it is behaving like MT4.

-Jerry


 

OK - I think I understand now.  I've always thought that the initialisation (or not) of global variables in MT4 is rather strange behaviour that shouldn't be relied on.

Although not documented as such, the MT5 UnitializeReason() call may work the same as documented for MT4.  ie may be usable in the next OnInit() call like this

int OnInit()
{
  if (UnitializeReason()!=REASON_CHARTCHANGE) FirstTime=true;
// ...
}

 

 

 

 

Jerry Spence
216
Jerry Spence 2010.03.05 01:25  
phampton:
netconuk posted # :

Thanks for your help Paul

I can see with your example, that FirstTime would get reset to true each time a new period was selected as OnInit() is invoked each time which is not what I want.

However I have got the following:

static bool  FirstTime = true; \\ I believe this should not change when selecting a new time period (It doesn't in MT4)

int OnInit()
  {
//---
MessageBox((FirstTime);

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

//---

FirstTime=false;

  }

//+------------------------------------------------------------------+


Here I am declaring a Static at the start, and each time I click a new period it goes through the OnInit() section and I get the Message that FirstTime has been reset back to True, which I don't think it should.

Your first comment is the crunch - I don't believe it is behaving like MT4.

-Jerry


 

OK - I think I understand now.  I've always thought that the initialisation (or not) of global variables in MT4 is rather strange behaviour that shouldn't be relied on.

Although not documented as such, the MT5 UnitializeReason() call may work the same as documented for MT4.  ie may be usable in the next OnInit() call like this

 

 

 

 

I'll try that out, but my argument is that it should work as documented - it's not very usable as it is. There is always the point of course that I am totally misunderstanding it!

-Jerry

Jerry Spence
216
Jerry Spence 2010.03.23 01:17  

There has been several releases since I reported this, but Static variables are still not being saved properly. Can this be fixed?

-Jerry

To add comments, please log in or register