datetime addition

 

hi

I wrote an indicator and I need to add some days to my indicator i used this approch:

datetime End=D'2012.03.18';
datetime dt=End+102*PeriodSeconds();
ObjectCreate(0,names[i],OBJ_VLINE,0,dt,0);

in   PERIOD_D1   it must draw a line at 2012.09.04

but it draws the line at  2012.06.28

I dont understand what is wrong with my code ?

 
pumper:

hi

I wrote an indicator and I need to add some days to my indicator i used this approch:

in   PERIOD_D1   it must draw a line at 2012.09.04

but it draws the line at  2012.06.28

I dont understand what is wrong with my code ?

If you add 102 days from March 18th 2012 then the calculation and the line is drawn correctly.

Here's some of my explanation before https://www.mql5.com/en/forum/9067#comment_379567 , I'm not using PeriodSecond() though. And please re-read again the definition of datetime and PeriodSeconds.

Please ask any question again, if you still don't understand.

 

thanks phi.nuts

please take a look at this :

may be its a bug

Files:
test.mq5  3 kb
 
pumper:

thanks phi.nuts

please take a look at this :

may be its a bug

03.18+102 days is 06.28!

How can you calculate the 09.04?

 

Its my fault.

I expected to draw a line 102 candles after that day.

now how can i do that?

 
pumper:

Its my fault.

I expected to draw a line 102 candles after that day.

now how can i do that?

I made it for you!
Files:
test.mq5  3 kb
 
pumper:

Its my fault.

I expected to draw a line 102 candles after that day.

now how can i do that?

If you want to "draw a line 102 candles after that day" in PERIOD_D1 chart, then it's equal with 102 days and you are doing it correctly.

 
phi.nuts:

If you want to "draw a line 102 candles after that day" in PERIOD_D1 chart, then it's equal with 102 days and you are doing it correctly.

No, because weekend has no candle.
 
VincentX:
No, because weekend has no candle.

Well, actually, I haven't finished my comment there and have to restart my PC, so I posted it anyway - my comment become misleading indeed :(. Thank VincentX for correcting it.


pumper, if we calculate time to the future we won't missing any future time, but if we calculate candles to the future, we will miss candle on weekend - like VincentX said. Just check last week, does chart have candle on weekend ?.

Since you working this on PERIOD_D1 chart, you probably think that you only missing 2 candles on weekend. However, if your broker is using GMT based time, then you only missing 1 candle on weekend.

It's a bad idea to count on number of candle in the future since there's missing candle on weekend.

 

so what i can do ?

in my indicator i must calculate some event in the future for example some event must happen 100 candles later and i want to show it on the chart.

how can i do that ? the VincentX way is very cool but only work for the candles that happend.

what about candles that will come in the future ?

 
pumper:

so what i can do ?

in my indicator i must calculate some event in the future for example some event must happen 100 candles later and i want to show it on the chart.

how can i do that ? the VincentX way is very cool but only work for the candles that happend.

what about candles that will come in the future ?

If you really must have to do some 100 candles projection and not 100 days to the future, then project it like your first code, the projection result will fall in on wrong time but on correct candles. You may have to correct the calculation / re-calculate at least once every week or if there's holiday (time gap in daily candle). 

Here's the logic : if today is Friday, then we will see a candles of Saturday and Sunday on chart, just pretend that Saturday is a Monday, and that Sunday is a Tuesday. Of course, on real Monday, the number of candle may change, that's why you have to re-calculate at least once every week.  

Reason: