datetimedurationmql4

A datetime arithmetic in MQL4


I would like to define a datetime type variable that is a result of a simple arithmetic operation between datetime type variables.

I've defined:

datetime duration = ( TimeCurrent() - OrderOpenTime() );
datetime   TmStop = StringToTime( "1970.01.01 16:00" );

but when I call it in some other arithmetic operation or generally in code like this

ExitBuy_H1 = ( duration > TmClose && ...

or this

text[3]= "Duration: " + TimeToStr( duration, TIME_MINUTES );

it doesn't work.

TmStop instead works fine.

Does anyone know why?


Solution

  • datetime is a simple integer, number of seconds passed since 1970.01.01 00:00. duration in your example is also in seconds, even though it is datetime formated, when you need it in minutes, divide by 60. TmClose from your example means 16*60*60 seconds and you can compare that integer with any other int of course, but what might be the reason for that? if you hold you position more then 16 hours, then duration > TmClose is true. if you want to convert difference in seconds (duration) into time, then you will have time converted from 1970.01.01 00:00 + duration seconds.

    Anyway it is not clear what is your goal in doing this calculations? if you want to make sure that you hold that particular position more then x hours, then simple bool holdMoreThanXHours = TimeCurrent()-OrderOpenTime()>x*PeriodSeconds(PERIOD_H1), and do not forget to reselect each ticket if you have several ones in open