3

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?

user3666197
  • 1
  • 6
  • 50
  • 92
perjliv
  • 91
  • 3
  • 12

2 Answers2

2

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

Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
  • `ExitBuy_H1= (duration > TmClose) ` stands for "when the position is open from more than TmClose, close it". If I wrote `ExitBuy_H1= ((TimeCurrent() - OrderOpenTime()) > TmClose` works, if I wrote `ExitBuy_H1= duration > TmClose` doesen't works – perjliv Sep 26 '17 at 22:05
  • 1
    maybe you dont assign some value to duration before, it must work, they all are integers in MQL4 – Daniel Kniaz Sep 27 '17 at 08:27
  • i just wrote `datetime duration = (TimeCurrent() - OrderOpenTime()); datetime TmStop = StringToTime ("1970.01.01 16:00"); ExitBuy_H1= (duration> TmStop);` and it doesen't works...can't understand why... – perjliv Sep 27 '17 at 12:53
  • 1
    really no idea what is your goal. TmClose should be in seconds, converting it to datetime looks strange. Make sure your orderOpenTime() returns a value not an error – Daniel Kniaz Sep 27 '17 at 14:00
  • I only want to call a datetime variable datetime `duration = (TimeCurrent() - OrderOpenTime());` that let me close a position hold for more than n hours. If, in the corner info, I write `text[3]= "Duration: " + TimeToStr(duration, TIME_MINUTES);` it also doesen't works, it works with every other entry (i.e. TimeCurrent(), OrderOpenTime(), TimeCurrent() - OrderOpenTime(), TmStop, ecc) except duration (as defined as above)...can't explain why... – perjliv Sep 28 '17 at 14:28
  • because your parameters are both converted into seconds since 1970.01.01, after you compute duration - i think it is some number of seconds, see my example above with bool holdMoreThanXHours, if you want to see how long does your position is open, write `text="Duration: "+(string)(duration/PeriodSeconds(PERIOD_M1));` – Daniel Kniaz Sep 28 '17 at 17:07
1

Fact A) the code, as-is, absolutely out of any question works.

//+------------------------------------------------------------------+
//|                                           Test_StackOverflow.mq4 |
//+------------------------------------------------------------------+
#property strict

void OnStart() {
     datetime duration = ( TimeCurrent() - OrderOpenTime() );
     string        txt = "Duration: " + TimeToStr( duration, TIME_MINUTES );
     }
//+------------------------------------------------------------------+

0 error(s), 0 warning(s), compile time: 2000 msec 1 1


Fact B) the full MCVE-context of the code, as-is, is missing.

StackOverflow requires users to post a complete MCVE-representation of the problem. This requirement was not met in the original post.

While the datetime and int data-types are mutually interchangeable, the problem does not seem to be hidden in this intrinsic "duality" of a value representation, but must be somewhere else.

The main suspects for Why? are:

  • variable definition was masked by another variable having the same name
  • variable scope-of-definition was exceeded ( asking a variable outside of it's scope )
  • db.Pool-operations were not preceded by OrderSelect()
user3666197
  • 1
  • 6
  • 50
  • 92