time | - | Datetime as number of seconds elapsed since midnight (00:00:00), January 1, 1970. |
So g_starttime needs to be in seconds.
Get a Time[i] you know about then add subtract in seconds
Hello Ickyrus,
I'm sorry to be so "thick", but I'm afraid that I don't understand you.
Basically, I'm trying to find out the closing price of the 1 hour bar that started at 1am server time today.
G-fer
DayStart_time=iTime[symbol(),PERIOD_D1,0) ;
DayStart_1AM=DayStart_time+1*60*60 ; //add 1 hour in seconds
DayStart_1PM=DayStar_time+13*60*60 ;
datetime g_starttime = "13:00" int shift=iBarShift(NULL,0,g_starttime);
iBarShift requires a datetime but your starttime is being initialized by a string, not a date. Bogus.
- either StrToTime
datetime g_starttime = StrToTime("13:00");
- or
#define HR2400 86400 // 24 * 3600 #define HR1300 46800 datetime TimeOfDay(datetime when){ return( when % HR2400 ); } datetime DateOfDay(datetime when){ return( when - TimeOfDay(when) ); } : datetime g_starttime = DateOfDay(Time[0]) + HR1300;
Thanks for the help, gentlemen ... I think I understand a little better now.
Does this mean that:
g_midnight=iTime(NULL,PERIOD_D1,0);
g_starttime=g_midnight+13*60*60;
int shift=iBarShift(NULL,0,g_starttime);
and
datetime g_starttime = StrToTime("13:00"); int shift=iBarShift(NULL,0,g_starttime);
would both return the number of bars since 1pm server time?
G-fer
edited to correct variable name
Ahh ... just answered my own question by trying a tiny EA.
Yup ... both work. Thanks for the help. I'll be sure to be aware of the error that I might cause if I try to find a time that hasn't actually happened yet.
G-fer.
be sure to be aware of the error that I might cause if I try to find a time that hasn't actually happened yet.
G-fer.
int iBarShift( | string symbol, int timeframe, datetime time, bool exact=false) |
Search for bar by open time. The function returns bar shift with the open time specified. If the bar having the specified open time is missing, the function will return -1 or the nearest bar shift depending on the exact.
You should also be aware that you are using the default find nearest bar to date time with the parameter exact set to false if its set to true then the finding a time that has not happened can be checked for with the returned error of -1.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi All,
Having a little (ok, a lot) of trouble with iTime functions.
I'm trying to determine the closing price of a bar that opened at a particular server time today. I assume I need to get the shift to the bar, and than query the closing price.
My sticking point is getting the time shift. For example, the code below tells me how many bars since 13:00 on 1/1/1970.
I need to know how may bars since 13:00 today, without having to manually type in today's date. Can this be done?
Regards ....