Indicator takes ~50% CPU usage.

 

I want to draw few lines each day at 6:45, 10:00, 14:30 and 16:00 so my code looks this way:

datetime setTime(string godzina){
        int dzien, miesiac, rok;
        string szablon;
        datetime czas;
        dzien   = TimeDay(TimeCurrent());
        miesiac = TimeMonth(TimeCurrent());
        rok     = TimeYear(TimeCurrent());

        szablon = rok + "." + miesiac + "." + dzien + " ";

        czas = StrToTime(szablon+godzina);
        return (czas);
}

int start()
  {
   for (int j = 0; j < iBars(Symbol(),PERIOD_D1); j++){
      int k = iBars(Symbol(),PERIOD_D1) - j;
      ObjectCreate("start1"+k, OBJ_VLINE, 0, setTime("06:45")-j*86400, 0);
      ObjectCreate("end1"+k, OBJ_VLINE, 0, setTime("10:00")-j*86400, 0);
      ObjectCreate("start2"+k, OBJ_VLINE, 0, setTime("14:30")-j*86400, 0);
      ObjectCreate("end2"+k, OBJ_VLINE, 0, setTime("16:00")-j*86400, 0);
   }

Some variables are not in English but I think you'll get the point. I thought maybe I should place it in the init instead of start, but it wouldn't work the next day.
Is there any way I could do it in the init?
 
Biertago:

I want to draw few lines each day at 6:45, 10:00, 14:30 and 16:00 so my code looks this way:

Do it in start() but not on every single tick . . . search the forum for "once per bar" then adapt the code/ideas you find to suit your needs.
 
That's a good idea. Thanks :)
 

Don't even need once per bar, just code it like a normal indicator

Not compiled Not tested

#define HR2400 86400       // 24 * 3600
int      TimeOfDay(datetime when){  return( when % HR2400          );         }
datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );         }
//datetime Today(){                   return(DateOfDay( TimeCurrent() ));       }
//datetime Tomorrow(){                return(Today() + HR2400);                 }
//datetime Yesterday(){               return( iTime(NULL, PERIOD_D1, 1) );      }

#define HR0645 24300
#define HR1000 36000
#define HR1430 52200
#define HR1600 57600
int start()
  {
   static datetime lastDrawn;
   for (int j = iBarShift(NULL,0, lastDrawn) - 1; j >= 0; j--){
      lastDrawn = Time[k];
      int tod = TimeOfDay(lastDrawn);
      if(tod == HR0625) ObjectCreate("start1"+tod, OBJ_VLINE, 0, lastDrawn, 0);
      if(tod == HR1000) ObjectCreate(  "end1"+tod, OBJ_VLINE, 0, lastDrawn, 0);
      if(tod == HR1430) ObjectCreate("start2"+tod, OBJ_VLINE, 0, lastDrawn, 0);
      if(tod == HR1600) ObjectCreate(  "end2"+tod, OBJ_VLINE, 0, lastDrawn, 0);
   }
}
Not compiled Not tested