int OnInit() { datetime time=iTime(_Symbol, PERIOD_M1, 2); Print(CandleStartTime(time, PERIOD_H1)); Print(CandleStartTime(time, PERIOD_M15)); Print(CandleStartTime(time, PERIOD_M30)); Print(CandleStartTime(time, PERIOD_M5)); return(INIT_SUCCEEDED); } datetime CandleStartTime(datetime time, ENUM_TIMEFRAMES period) { datetime candle_start_time= (int)(time/(PeriodSeconds(period)))*PeriodSeconds(period); return candle_start_time; }
Thanks Yashar. I found your solution is a little bit complicated for a beginner like me, anyway, highly appreciated.
After studying your codes, instead, I just created my own simple way. I know that the basic requirement of iTime must have the "shift" input. Therefore in order to get other TF's iTime, I must use iBarShift to get other TF's "shift" first. My solution / illustration would be as follows :-
datetime iTm=iTime(sym,5,526); datetime M30Tm=iTime(sym,30,iBarShift(sym,30,iTm,true)); // if I want to get M30's iTime datetime H1Tm= iTime(sym,60,iBarShift(sym,60,iTm,true)); // if I want to get H1's iTime datetime H4Tm= iTime(sym,240,iBarShift(sym,240,iTm,true)); // if I want to get H4's iTime datetime D1Tm= iTime(sym,1440,iBarShift(sym,1440,iTm,true)); // if I want to get D1's iTime Print("M30Tm "+TimeToStr(M30Tm)+" /H1Tm "+TimeToStr(H1Tm)+" /H4Tm "+TimeToStr(H4Tm)+" /D1Tm "+TimeToStr(D1Tm));
Kindly correct me if I'm wrong, thanks
- The code I posted above works for MT5. Sorry I wasn't aware this is MT4. A small change and it works in MT4 as well:
int OnInit() { datetime time=iTime(_Symbol, PERIOD_M1, 2); Print(TimeToStr(CandleStartTime(time, PERIOD_H1))); Print(TimeToStr(CandleStartTime(time, PERIOD_M15))); Print(TimeToStr(CandleStartTime(time, PERIOD_M30))); Print(TimeToStr(CandleStartTime(time, PERIOD_M5))); return(INIT_SUCCEEDED); } datetime CandleStartTime(datetime time, ENUM_TIMEFRAMES period) { datetime candle_start_time= (int)(time/(PeriodSeconds(period)))*PeriodSeconds(period); return candle_start_time; }
- Your solution works most of the times. However I do not suggest using iBarshift because:
iBarShiftReturned value
Index of the bar which covers the specified time. If there is no bar for the specified time (history "gap"), the function will return -1 or the nearest bar index (depending on exact parameter).
exact=false
[in] Return mode when the bar is not found (false - iBarShift returns the nearest, true - iBarShift returns -1).
- Also applying iTime to other time frame than current chart is risky and you will need to check errors 4066 and 4073:
4066
ERR_HISTORY_WILL_UPDATED
Requested history data is in updating state
4067
ERR_TRADE_ERROR
Internal trade error
4068
ERR_RESOURCE_NOT_FOUND
Resource not found
4069
ERR_RESOURCE_NOT_SUPPORTED
Resource not supported
4070
ERR_RESOURCE_DUPLICATED
Duplicate resource
4071
ERR_INDICATOR_CANNOT_INIT
Custom indicator cannot initialize
4072
ERR_INDICATOR_CANNOT_LOAD
Cannot load custom indicator
4073
ERR_NO_HISTORY_DATA
No history data
- The code I posted above works for MT5. Sorry I wasn't aware this is MT4. A small change and it works in MT4 as well:
- Your solution works most of the times. However I do not suggest using iBarshift because:
- Also applying iTime to other time frame than current chart is risky and you will need to check errors 4066 and 4073:
4066
ERR_HISTORY_WILL_UPDATED
Requested history data is in updating state
4067
ERR_TRADE_ERROR
Internal trade error
4068
ERR_RESOURCE_NOT_FOUND
Resource not found
4069
ERR_RESOURCE_NOT_SUPPORTED
Resource not supported
4070
ERR_RESOURCE_DUPLICATED
Duplicate resource
4071
ERR_INDICATOR_CANNOT_INIT
Custom indicator cannot initialize
4072
ERR_INDICATOR_CANNOT_LOAD
Cannot load custom indicator
4073
ERR_NO_HISTORY_DATA
No history data
Hi Yashar, thanks. What you pointed out is absolutely correct because iBarShift may give -1 value in certain time, such as BTCUSD may start at xx.xx.xx xx.05.xx ...
How about below codes to filter out the -1 value, finally to get a correct iBarShift number as follows :-
datetime iTm=iTime(sym,5,526); int tTF=Period(); // Chart's TF int StrSf=iBarShift(sym,tTF,iTm,true); if(StrSf==-1) StrSf=iBarShift(sym,tTF,iTm,false)-1; datetime StrTm=iTime(sym,tTF,StrSf); Print("StartSf "+IntegerToString(StrSf)+" /StartTm "+TimeToStr(StrTm));
Again, kindly correct me if I'm wrong. Please advise, thanks
This may not do what you really expect... The closest existing bar could be anywhere. It all depends on the rest of the code you write and how they interact with this part.

- 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 coders, please advise how to get the iTime automatically for other respective timeframe's datetime ?
Note that the above example of "PERIOD_M1" is not fixed, I may use other period for my purposes.If the Print output is "2025.02.14 18:57", I should expect a set of codes to get me the following answers automatically for respective timeframes such as :-
M5 : 2025.02.14 18:55
M15 : 2025.02.14 18:45
M30 : 2025.02.14 18:30
H1 : 2025.02.14 18:00
H4 : 2025.02.14 16:00
D1 : 2025.02.14 00:00
W1 : 2025.02.09 00:00
MN : 2025.02.01 00:00
Thanks