Download MetaTrader 5

Expert Advisor doesn't recognize if chart period is changed

To add comments, please log in or register
Marcus Riemenschneider
232
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...?! 

Alain Verleyen
Moderator
35119
Alain Verleyen  

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

whroeder1
17093
whroeder1  
Or reset your Time0 variable in OnInit()
Marco vd Heijden
Moderator
6152
Marco vd Heijden  

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;
        }
Marcus Riemenschneider
232
Marcus Riemenschneider  
Thanks guys, your help is greatly appreciated!! I absolutely didn't think about the same candle time....!
Mladen Rakic
72273
Mladen Rakic  
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);
   }
}
Marcus Riemenschneider
232
Marcus Riemenschneider  
Problem solved! Thanks again, guys!! :)
To add comments, please log in or register