Difference between timestamps

 

I have a tool that I am going to attempt to make that obtains the difference between two time stamps. If the difference is greater than x hours, I want to take action on the trade.

A good example of this, is a trade has been open in the market for more than 4 hours, because of this, we are considering it a loss so we want to close the trade. So, we take the open time stamp and obtain the difference of the current timestamp and if it is greater than 4 hours perform the closure.

Concerns:

What if the time happened before midnight? and current time is after midnight?

What if for some reason, the date is 24hours later or a time shift takes place, like setting clocks forward or backward by an hour?

Timestamps are not something I am very good at, and it gets even worse for me when I have to figure in local time vs GMT, I guess you could say I have a poor sense of timing.

Anyway, I would appreciate some input on this if you don't mind taking the time.

Thank you in advance.

 
LEHayes:

[...] Concerns:

What if the time happened before midnight? and current time is after midnight?

What if for some reason, the date is 24hours later or a time shift takes place, like setting clocks forward or backward by an hour?

You're presumably talking about comparing TimeCurrent() against the OrderOpenTime() of a position. Shifts to and from daylight saving should be a fairly minor concern because, AFAIK, in every timezone they happen over a weekend. The clock will not suddenly jump while an EA is trading. However, life does become interesting if you want to do something like 4 trading hours, rather than just "4 hours". That potentially becomes complex over weekends because, if you have a time before market close on the Friday and a time after market re-open on the Sunday/Monday, the exact number of trading hours between them depends on the hours when the broker is open. And also requires occasional adjustment for daylight savings.

 

I think I found what I was looking for. Your statement above does point out another concern, although, I think it could be worked around by closing all trades on Friday and working with new trades on Sunday.

Here is what I found:

extern int ExpirationOfOpenOrderInMinutes = 5;


if((ExpirationOfOpenOrderInMinutes>0) && (TimeCurrent() > OrderOpenTime()+ExpirationOfOpenOrderInMinutes*60)) {

  OrderClose(....

}
 
The only thing I think I need to figure out here is how to deal with hours instead of minutes.
 
LEHayes:
The only thing I think I need to figure out here is how to deal with hours instead of minutes.

TimeCurrent() and OrderOpenTime() give you UNIX-style timestamps, i.e. expressed as the number of seconds from 1/1/1970. The difference between TimeCurrent() and OrderOpenTime() is therefore a number of seconds. The code you've posted above is doing a comparison in minutes simply by multiplying the parameter in minutes by 60 (seconds). In your case you'd be wanting to take action on trades if the difference between TimeCurrent() and OrderOpenTime() exceeded 4 [hours] * 60 [minutes] * 60 [seconds] = 14,400.

 
I think I have it now. The 60 refers to the number of seconds in a minute. In the example, we have 5 * 60 to get the number of seconds in 5 minutes. So, all I have to do is figure out how many seconds are in a hour and multiply that by the number of hours. 60*60. Or, I could just use #hours*(60*60)
Reason: