How to avoid the first alert when attach to a chart

 

Hi,

I have this code below and it gives me an alert every minute. That works fine, but it also gives me an alert when i attach it to a chart and the first tick come in and i don't want that. How can i avoid that first alert?

#include <stdlib.mqh>
#include <WinUser32.mqh>

int init()
{
    ObjectsDeleteAll();      // clear the chart
    Comment("");    // clear the chart
}

// Expert start
int start()
{
if (isNewHour())
   Alert("The time is now: ", Minute());
}

bool isNewHour() 
{
   static datetime BarTime;
   //----
   bool res=false;
   if (BarTime!=iTime(NULL,PERIOD_M1,0)) {
   BarTime=iTime(NULL,PERIOD_M1,0);
   res=true;
   }
   return(res);
}
 

Perhaps something like this.

bool isNewHour() 
{
   static datetime BarTime;
   if (BarTime==0) {BarTime=iTime(NULL,PERIOD_M1,0);}
   //----
   bool res=false;
   if (BarTime!=iTime(NULL,PERIOD_M1,0)) {
   BarTime=iTime(NULL,PERIOD_M1,0);
   res=true;
   }
   return(res);
}
 
ubzen:

Perhaps something like this.

ubzen! This is not fair :)

I have been using hours to try to find out how to solve this.

10 mins after i have asked the answer is there. Now i have to study the code and learn :)

Thanks, it works perfect!

 
You're welcome :)
 
ubzen:
You're welcome :)

ubzen,

Now when there's not so many ticks during the night, it's not giving me the alert every minute. The alert comes when there's a tick and there can go up to 10, 20 or 30 seconds before that happens.

Maybe i cannot get an alert if that requeres a tick, but what if i want to write e.g. price or anything else to a file, could that work without a tick?

 

but what if i want to write e.g. price or anything else to a file, could that work without a tick? No.

You could use Loops and Sleep. That way the expert/script don't have to wait for a tick and operates like Clock-work. Example:

void start(){
    while(true){
        Do_Something();
        Sleep(60000);/*60_Seconds*/ 
        RefreshRates();
}   }
 
ubzen:

but what if i want to write e.g. price or anything else to a file, could that work without a tick? No.

You could use Loops and Sleep. That way the expert/script don't have to wait for a tick and operates like Clock-work. Example:


Yes i see. So my code looks like this now and seems to work. Thanks again :)

bool isNewHour() 
{
   static datetime BarTime;
   if (BarTime==0) {BarTime=iTime(NULL,PERIOD_M1,0);}
   bool res=false;
   if (BarTime!=iTime(NULL,PERIOD_M1,0)) {
   BarTime=iTime(NULL,PERIOD_M1,0);
   res=true;
   }
   return(res);
}

// Expert start
void start()
{
    if (isNewHour()){
      while(true)
      {
        Alert("The time is now: ", Minute());
        //Do_Something();
        Sleep(60000);/*60_Seconds*/ 
        RefreshRates();
      }
    }
}