Expert Advisor doesn't recognize if chart period is changed

 

Hi guys,

I wrote a small code to demonstrate my problem:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (Time0!=iTime(NULL, _Period, 0)) {
      Time0=iTime(NULL, _Period, 0);
      Alert(_Period);
   }
  }

 When I switch the chart period I expect the EA to pop up the alert window displaying the current chart period. Interestingly, sometimes it works and sometimes not. Here is the journal of my test:

 

I started with the daily and the alert was alright. Then I switched to H4 and the alert was also ok. But after switching to the H1 I waited for almost 2 minutes and there was no alert. The M30 period worked again but M15 and M5 didn't work. The weekly was ok again. Why doesn't the EA recognize all period changes?

Can somebody tell me what's wrong with the code? I can't see any mistake here...?! 

 

When you switch from H4 to H1 for example, you can still have the same time for candle 0.

Anyway if you want to check a timeframe change, compare the timeframe, not the time.  Or even UninitializeReason().

 
Or reset your Time0 variable in OnInit()
 

Can use a switch for example


      switch(Period())
        {
         case 0:// Current

            break;

         case 1:// M1

            break;

         case 5:// M5

            break;

         case 15:// M15

            break;

         case 30:// M30

            break;

         case 60:// H1

            break;

         case 240:// H4

            break;
            
         case 1440:// D1

            break;

         case 10080:// W1

            break;

         case 43200:// MN1

            break;
        }
 
Thanks guys, your help is greatly appreciated!! I absolutely didn't think about the same candle time....!
 
Marcus Riemenschneider:

Hi guys,

I wrote a small code to demonstrate my problem:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (Time0!=iTime(NULL, _Period, 0)) {
      Time0=iTime(NULL, _Period, 0);
      Alert(_Period);
   }
  }

 When I switch the chart period I expect the EA to pop up the alert window displaying the current chart period. Interestingly, sometimes it works and sometimes not. Here is the journal of my test:

 

I started with the daily and the alert was alright. Then I switched to H4 and the alert was also ok. But after switching to the H1 I waited for almost 2 minutes and there was no alert. The M30 period worked again but M15 and M5 didn't work. The weekly was ok again. Why doesn't the EA recognize all period changes?

Can somebody tell me what's wrong with the code? I can't see any mistake here...?! 

Current bars for different time frames can have exactly the same time value. EAs are not resetting variable values the same way as indicators. Do the check the following way :
void OnTick()
{
   static int _prevPeriod=0;
   if (Time0!=iTime(NULL, _Period, 0) || _prevPeriod!=_Period) {
       Time0 =iTime(NULL, _Period, 0);   _prevPeriod =_Period;
            Alert(_Period);
   }
}
 
Problem solved! Thanks again, guys!! :)
Reason: