Download MetaTrader 5
To add comments, please log in or register
Sign up to publish an article and receive 200 USD
jmb1
86
jmb1 2014.12.11 17:56 

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
954
honest_knave 2014.12.11 18:18  
   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
13602
whroeder1 2014.12.11 18:18  
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 2014.12.11 21:40  
honest_knave and WHRoeder - my grateful thanks to you both.
/
To add comments, please log in or register