Simple to spot a shifted date range?

 

The following example is using iBarShift for parsing datetimes  🙂


input int     Hour_shift=-5;  //Hour shift
input int     Hour_start=1;   //Start hr; 0-23
input int     Minute_start=0; //Start min; 0-59
input int     Second_start=0; //Start sec; 0-59
input int     Hour_end=9;     //Stop hr; 0-23
input int     Minute_end=59;  //Stop min ; 0-59
input int     Second_end=59;  //Stop sec; 0-59

int OnInit()
{
 int iGMT=(int)NormalizeDouble(((double)TimeCurrent()-(double)TimeGMT())/3600,0)+Hour_shift; 
 Print("GMT Hour Shift: "+ (string)iGMT); 

 int iStart=iBarShift(NULL,60,iTime(NULL,1440,0),false)+(iGMT<0?MathAbs(iGMT):(iGMT>0?-iGMT:iGMT));

 datetime dtTmp=Time[iStart];
 
 int iStartYear=TimeYear(dtTmp);
 int iStartMonth=TimeMonth(dtTmp);
 int iStartDay=TimeDay(dtTmp);
 int iStartHour=TimeHour(dtTmp)+Hour_start;
 int iStartMinute=TimeMinute(dtTmp)+Minute_start;
 int iStartSecond=TimeSeconds(dtTmp)+Second_start;
 datetime dtStart=
  (datetime)((string)iStartYear+"."+
             (string)iStartMonth+"."+
             (string)iStartDay+" "+
             (string)iStartHour+":"+
             (string)iStartMinute+":"+
             (string)iStartSecond);
 Print("Day Start: "+ (string)dtStart);
 
 int iEndYear=TimeYear(dtTmp);
 int iEndMonth=TimeMonth(dtTmp);
 int iEndDay=TimeDay(dtTmp);
 int iEndHour=TimeHour(dtTmp)+(Hour_end-Hour_start) ;
 int iEndMinute=TimeMinute(dtTmp)+Minute_end;
 int iEndSecond=TimeSeconds(dtTmp)+Second_end;
 datetime dtEnd=
  (datetime)((string)iEndYear+"."+
             (string)iEndMonth+"."+
             (string)iEndDay+" "+
             (string)iEndHour+":"+
             (string)iEndMinute+":"+
             (string)iEndSecond);
 Print("Day End: "+ (string)dtEnd); 
 
 return(INIT_SUCCEEDED);
}

The GMT shift is +3, so the offset between +3 and -5 is -2 hours ~ which must be applied to both sides

2021.09.20 03:20:46.189 EA5 USDJPY,H1: Day End: 2021.09.20 07:59:59

2021.09.20 03:20:46.189 EA5 USDJPY,H1: Day Start: 2021.09.17 22:00:00

2021.09.20 03:20:46.188 EA5 USDJPY,H1: GMT Hour Shift: -2


How else? 

 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2.  int iGMT=(int)NormalizeDouble(((double)TimeCurrent()-(double)TimeGMT())/3600,0)

    You are truncating instead of rounding.

  3.  int iStart=iBarShift(NULL,60,iTime(NULL,1440,0),false)+(iGMT<0?MathAbs(iGMT):(iGMT>0?-iGMT:iGMT));
    
    

    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)

  4.  datetime dtTmp=Time[iStart];
     
     int iStartYear=TimeYear(dtTmp);
     int iStartMonth=TimeMonth(dtTmp);
     int iStartDay=TimeDay(dtTmp);
     int iStartHour=TimeHour(dtTmp)+Hour_start;
     int iStartMinute=TimeMinute(dtTmp)+Minute_start;
     int iStartSecond=TimeSeconds(dtTmp)+Second_start;
     datetime dtStart=
      (datetime)((string)iStartYear+"."+
                 (string)iStartMonth+"."+
                 (string)iStartDay+" "+
                 (string)iStartHour+":"+
                 (string)iStartMinute+":"+
                 (string)iStartSecond);
    You have a datetime (dtTmp). No need for all that, just add Hour_start*3600 + Minute_start*60 + Second_start.