Determine time in instrument's market

 

I'm working on a system that trades the US30 but I only want it to trade during hours the market is actually open, 09:30 to 16:00, I need something that can do this on current market and historically so I can back test.

Is it possible to return the current time in the market of the instrument?

thanks... 

 
raforrester: current time in the market of the instrument?
RTFM TimeCurrent() or Time[0]
 

Thanks WHRoeder, but TimeCurrent() gives time in seconds since 1st Jan 1970, I need to consider things like daylight saving so the difference will be different depending on whether the US is in daylight saving or not?

(sorry, forgot to put the DST in the OP)

 
raforrester:

... , I need to consider things like daylight saving so the difference will be different depending on whether the US is in daylight saving or not?

(sorry, forgot to put the DST in the OP)

See my post (here) regarding calculating start/end dates for American DST.
 
raforrester:

Thanks WHRoeder, but TimeCurrent() gives time in seconds since 1st Jan 1970, I need to consider things like daylight saving so the difference will be different depending on whether the US is in daylight saving or not?

(sorry, forgot to put the DST in the OP)

 

Every datetime in mql4 is  " time in seconds since 1st Jan 1970"  this is how dates and times are expressed in mql4.
 
@Thirteen, I've just skimmed through the post you linked and it looks like exactly what I'm after - thanks.
 

Thanks to @Thirteen's code I was able to knock up the following functions to use in my EA, thought I'd post them here for others to use / verify.

      // Get time differece between server time and market time
      double GetTimeDiff(string strSymbol)
      {
         double dblResult = 0;
         if (strSymbol == "US30.M" || strSymbol == "_US30")
         {
            if (IsInstrumentInDST(strSymbol))
            {
               dblResult = -25200;
            }
            else
            {
               dblResult = -28800;
            }
         }
         return(dblResult);
      }
      
      // Is the market for the sybol in DST
      bool IsInstrumentInDST(string strSymbol)
      {
         bool bolResult = false;
         int intYear = Year();
         if (strSymbol == "US30.M" || strSymbol == "_US30")
         {
            int intDSTStartDay = 14 - MathMod((1 + 5*intYear/4), 7);
            int intDSTEndDay = 7 - MathMod((1 + 5*intYear/4), 7);
            string strDSTStart = (Year() + ".03." + intDSTStartDay + " 02:00");
            string strDSTEnd = (Year() + ".11." + intDSTEndDay + " 02:00");
            Output("DST starts on " + strDSTStart + " and ends on " + strDSTEnd);
            if (TimeCurrent() > StrToTime(strDSTStart) && TimeCurrent() < StrToTime(strDSTEnd))
            {
               Output("For " + strSymbol + " currently DST applies...");
               bolResult = true;
            }
         }
         return(bolResult);
      }
 
raforrester:

Thanks to @Thirteen's code I was able to knock up the following functions to use in my EA, thought I'd post them here for others to use / verify.

This . . .

double GetTimeDiff

  . . .  should be a datetime not a double . . same for dblResult

 
Actually GetTimeDiff returns an int. So int or double are fine. Not a datetime.
#define HR2400 86400       // 24 * 3600
int      TimeOfDay(datetime when){  return( when % HR2400            );    }
datetime DateOfDay(datetime when){  return( when - TimeOfDay(when)   );    }
Reason: