Download MetaTrader 5

Do this once a day

To add comments, please log in or register
jmb1
86
jmb1  

Hello

I'm trying to do something simple, but it is eluding me due a toxic mix of a lack of knowledge and stupidity.

With this piece of code, I'd like to print to the log a simple message, once a day, near the start of the day: 

bool printed=true;

{
if(Hour() > 0)
   printed=false;

if(Hour()== 0 && printed==false)
   Print("Hello");
   printed=true;
}

 I'd be most grateful if someone would enlighten me!

Thank you. 

honest_knave
Moderator
2408
honest_knave  
   static datetime lastday=0;
   datetime today=iTime(NULL,PERIOD_D1,0);
   if(today>lastday)
      {
       Print("hello");
       lastday=today;
      }

  Plenty of different ways to do it. If your broker day is offset from your day, there are other options.

whroeder1
15053
whroeder1  
jmb: once a day, near the start of the day
  1. Printed is not static, therefor it will never be true.
  2. What about the first tick of Sunday? that's no were near midnight?
  3. #define HR2400 (PERIOD_D1 * 60)  // 86400 = 24 * 3600
    int      TimeOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                              return( when % HR2400 );            }
    datetime DateOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                              return( when - TimeOfDay(when) );   }
    datetime Tomorrow( datetime when=0){      if(when == 0)  when = TimeCurrent();
                                              return(DateOfDay(when) + HR2400);   }
    //datetime Yesterday(datetime when=0){      if(when == 0)  when = TimeCurrent();
    //   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
    //                                       return( iTime(NULL, PERIOD_D1, iD1) ); }
    
    static datetime nextDay = 0;
    datetime now = TimeCurrent();
    if(now >= nextDay){ nextDay = Tomorrow(); 
       Print("Hello");
    }

jmb1
86
jmb1  
honest_knave and WHRoeder - my grateful thanks to you both.
To add comments, please log in or register