how to get the last trading day specific time bar (M1) ? MQL4 MT4 coding

 
if i want to know last trading day 16:45 (M1) that bar. how to do this? i made a code which can know the previous 16:45 bar (M1), but if Current time pass 16:45, let said 16:50, this code only can know the previous bar 16:45 (M1), but i want to know last trading day 16:45, even if today current time pass 16:45. How to solve this problem ? Can anyone give a help?


input int SEndTimebarHour = 16;//
input int SEndTimebarbarMins = 45;//


int SEndTimebar25(int SEndTimebarHour, int SEndTimebarbarMins)
{  
   int i;
   
   #define HR2300 ((SEndTimebarHour*3600)+(SEndTimebarbarMins*60)) // 23*3600(Hour) / 0*60(Mins)
   #define HR24   86400 // 24*3600
   int iLimit = iBars(0, PERIOD_M1);
   
   for(int i=0; i< iLimit; i++)
   {  
      datetime t2300 = iTime(0, PERIOD_M1,i);
      int      TOD   = t2300 % HR24; // Time of day
      if (TOD == HR2300) 
      {
         return i;
      }
   }
   return i;
}

 
To Pui Kuen if Current time pass 16:45, let said 16:50, this code only can know the previous bar 16:45 (M1), but i want to know last trading day 16:45,
  1. "Last trading day 16:45" is non-sense. Last trading day has nothing to do with 16:45, the day starts at first tick at or beyond 00:00.

  2. That makes no sense. If the time is now 1650, then there will be a 1650 M1 bar. 1645 is not an M1 but the previous M5 bar.
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

  3. iBarShift(_Symbol, PERIOD_M5, TimeCurrent() )+1 is the previous M5 bar.

  4. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20

 
William Roeder:
  1. "Last trading day 16:45" is non-sense. Last trading day has nothing to do with 16:45, the day starts at first tick at or beyond 00:00.

  2. That makes no sense. If the time is now 1650, then there will be a 1650 M1 bar. 1645 is not an M1 but the previous M5 bar.
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

  3. iBarShift(_Symbol, PERIOD_M5, TimeCurrent() )+1 is the previous M5 bar.

  4. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20

Hello William,

Maybe my word made mistake.

For example, everyday has a 16:45 M1 bar, i want to know last trading day 16:45 M1 bar Open price.

For my code, i can get the nearest 16:45 bar. if the current time is smaller than 16:45 beacuse the last trading day 16:45 M1 bar is the nearest one.

However, if the current time greater than 16:45, such as 16:50, the nearest 16:45 M1 bar is today instead of last trading day, so i want to get the last trading day 16:45 M1 bar.

Hope this can make you understand more clearly

 
To Pui Kuen: Hope this can make you understand more clearly

You used the same words exactly the same way. Made no sense the first time. Same words, same nonsense.

To Pui Kuen: however, if the current time greater than 16:45, such as 16:50, the nearest 16:45 M1 bar is today instead of last trading day, so i want to get the last trading day 16:45 M1 bar.

The last trading day is always today. Same words, same nonsense.

Perhaps you want the previous trading day 16:45 M1 bar.
          Find bar of the same time one day ago - MQL4 programming forum 2017.10.06

SECONDS HR1645 = 60300; // (16 × 60 + 45) × 60
datetime yesterdayBOD = yesterday();
INDEX iY1645 = iBarShift(_Symbol, PERIOD_M1, yesterdayBOD + HR1645);
 
William Roeder:

You used the same words exactly the same way. Made no sense the first time. Same words, same nonsense.

The last trading day is always today. Same words, same nonsense.

Perhaps you want the previous trading day 16:45 M1 bar.
          Find bar of the same time one day ago - MQL4 programming forum 2017.10.06

i am sorry, you are right. Forgive my English. is "Previous" instead of "Last".

Thanks!

What if yesterday is Sat. ?  

 
To Pui Kuen: What if yesterday is Sat. ?  

If you just used date( date() - 1 ) on Sunday (assuming your broker has Sunday bars,) you would get Saturday, previous calendar day.

If you look at the yesterday function, it finds the last bar not part the day of the passed argument. That can't be Saturday because the market was closed. It can only find a Friday. (or earlier) bar.

 
William Roeder:

If you just used date( date() - 1 ) on Sunday (assuming your broker has Sunday bars,) you would get Saturday, previous calendar day.

If you look at the yesterday function, it finds the last bar not part the day of the passed argument. That can't be Saturday because the market was closed. It can only find a Friday. (or earlier) bar.

is this function only work in MT5 language ?

i cannot make this work in MT4 

 
To Pui Kuen: is this function only work in MT5 language ? i cannot make this work in MT4 
  1. "Doesn't work" is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. 2004
              When asking about code

    Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.

  2. Do you see blue words with underlines? Those are links. They take you to more information. Like the one I provided takes you to my function definitions.

Reason: