mql4mt4

How to find day's first candle's HIGH, LOW in current Time frame


I am new to MT4, know little basic programming for MQL4. I am trading in UTC+5:30 in Indian Stocks. I just want a small piece of code to get today's First candles HIGH and LOW in current TimeFrame. Our trading starts at 9:15 AM IST and ends at 3:30 PM IST.

e.g. if I select PERIOD_M15 (15 min chart) then I need to have day's first candle (i.e. from 9:15AM to 9:30AM) HIGH and LOW.

thanks in advance.


Solution

  • welcome to SOF!

    you need input parameters (time of the day start):

    input int InpTimeStartHour=9; input int InpTimeStartMinute=15; this can be as a one string but for simplicity such fields

    bool getHighLowFistCandle(double &high,double &low){
       //check if first candle (zero-current) is after 9:15
       datetime lastCandle=iTime(Symbol(),0,1);
       if(TimeHour(lastCandle)<InpTimeStartHour || 
          (TimeHour(lastCandle)==InpTimeStartHour && TimeMinute(lastCandle)<InpTimeStartMinute){
          return(false);
       }
       //looking for that time candle starting from day start
       datetime todayStart=iTime(Symbol(),PERIOD_D1,0);
       int shift=iBarShift(Symbol(),0,todayStart);
       for(int i=shift;i>0;i--){
          datetime iCandleTime=iTime(Symbol(),0,i);
          if(TimeHour(iCandleTime)==InpTimeStartHour &&
             TimeMinute(iCandleTime)==InpTimeStartMinute){
              high=iHigh(Symbol(),0,i);
              low=iLow(Symbol(),0,i);
              return(true);
          }
       }
      return(false);
    }