MT4 - Object Create with invalid time creates 'random object'.

 

I'm currently using something like this in my code: 

for (int i = 0; i < 10; i++)

{

// someday is date + 10 am

datetime linePos = someValidDate - 60*60*24*i; // minus i-days

ObjectCreate("SOMEPREFIX_" + i, OBJ_VLINE, 0, linePos, 0);

}

This works fine when the date is a valid date, but as you can imagine some days are sundays or saturdays without market data. But ObjectCreate doesn't return -1 in such a case. It draws a line at 10pm and that is very strange. All lines are drawn correctly, but every week there is a random line at 10pm (there is also a 10am line on that day - the 10pm line is the sunday line...). What can I do about this?

 

PS: When I  print someValidDate - 60*60*24*i it always shows 10am...

 

Something like this

   datetime linePos = someValidDate - 60*60*24*i; // minus i-days
   if(iBarShift(Symbol(),PERIOD_H1,linePos,true)>0)
      ObjectCreate("SOMEPREFIX_" + i, OBJ_VLINE, 0, linePos, 0); 

may work. I used PERIOD_H1, so if you do not have the H1 chart open, you will need to make sure that it is kept updated

 
b2885089: This works fine when the date is a valid date, but as you can imagine some days are sundays or saturdays without market data. But ObjectCreate doesn't return -1 in such a case.
  1. Of course ObjectCreate doesn't return an error, It just creates a line.
  2. Don't use someValidDate - 60*60*24*i use a valid date
    for (int i = 0; i < 10; i++){
      datetime linePos = iTime(NULL, PERIOD_D1, i);
      ObjectCreate("SOMEPREFIX_" + i, OBJ_VLINE, 0, linePos, 0); 

Reason: