Trade when market is open

 

Hi i am trying to check whether market is open before placing a trade. 

At the moment i have the below code but i still get a market closed error when running the strategy tester


         TimeToStruct(TimeCurrent(),DT);
         day = (ENUM_DAY_OF_WEEK) DT.day_of_week;
         
         SymbolInfoSessionTrade(sym,day,0,starttime,endtime);
         
         TimeToStruct(starttime,SS);
         
         DT.year = SS.year;   DT.mon = SS.mon;  DT.day = SS.day;
         
         tim = StructToTime(DT);

         if(starttime < tim && endtime > tim)
         {
            Trader.PositionOpen(sym,OT,vol,BDAK,0,0,"");
         }

Could the be with trades that open on an overnight session. Then my "endtime" variable would be earlier than my "starttime". But how would I make my code take into account both overnight and non overnight sessions?? 

 
AnalyticInsight:

Hi i am trying to check whether market is open before placing a trade. 

At the moment i have the below code but i still get a market closed error when running the strategy tester


Could the be with trades that open on an overnight session. Then my "endtime" variable would be earlier than my "starttime". But how would I make my code take into account both overnight and non overnight sessions?? 

As you are using datetime, it is unlikely that your endtime will be earlier than starttime (unless you manipulate it to be so) as these are stored as the number of seconds since 1st Jan 1970 so inherently have day, date, month incorporated (have a look at the documentation). 

Your code seems to work as intended - if you add some print statements you can get a clearer view of what's going on (code attached):

void timeTrade()
  {
   MqlDateTime SS, DT;
   string sym = _Symbol;
   datetime starttime,endtime, tim;

   TimeToStruct(TimeCurrent(),DT);
   ENUM_DAY_OF_WEEK day = (ENUM_DAY_OF_WEEK) DT.day_of_week;

   SymbolInfoSessionTrade(sym,day,0,starttime,endtime);

   TimeToStruct(starttime,SS);

   DT.year = SS.year;
   DT.mon  = SS.mon;
   DT.day  = SS.day;

   tim = StructToTime(DT);

   PrintFormat("\nstarttime=%s endtime=%s", TimeToString(starttime), TimeToString(endtime));
   PrintFormat("SS.year=%d SS.mon=%02d SS.day=%d", SS.year, SS.mon, SS.day);
   PrintFormat("DT.year=%d DT.mon=%02d DT.day=%d", DT.year, DT.mon, DT.day);
   PrintFormat("if(starttime[%s] < tim[%s] && endtime[%s] > tim[%s])",
               TimeToString(starttime),
               TimeToString(tim),
               TimeToString(endtime),
               TimeToString(tim)
              );

   if(starttime < tim && endtime > tim)
     {
      PrintFormat("TRUE: Trader.PositionOpen(sym,OT,vol,BDAK,0,0,\"\")");
     }
    else
      {
      PrintFormat("FALSE: Trader.PositionOpen(sym,OT,vol,BDAK,0,0,\"\")");
      } 

  }

The output is like so:


  • 2022.07.08 13:10:19.953 starttime=1970.01.01 00:00 endtime=1970.01.02 00:00
  • 2022.07.08 13:10:19.953 SS.year=1970 SS.mon=01 SS.day=1
  • 2022.07.08 13:10:19.953 DT.year=1970 DT.mon=01 DT.day=1
  • 2022.07.08 13:10:19.953 if(starttime[1970.01.01 00:00] < tim[1970.01.01 10:40] && endtime[1970.01.02 00:00] > tim[1970.01.01 10:40])
  • 2022.07.08 13:10:19.953 TRUE: Trader.PositionOpen(sym,OT,vol,BDAK,0,0,"")

But it is easy to add another check to protect against endtime being earlier than start time, if this happens you just have to re-compute the desired times to the correct values
Reason: