MarketClosed() Method - how to determine day to trade on.

Chris Stols
56
bool MarketClosed()
  {
   bool closed=false;
   MqlDateTime STime;
   datetime time=TimeCurrent();
   TimeToStruct(time,STime);

   datetime date_from, date_to;

   bool session_exist=true;
   uint session_index=0;
//--- get over all sessions of the current day
   while(session_exist)
     {
      //--- check if there is a trade session with the number session_index
      session_exist=SymbolInfoSessionTrade(m_PositionForInstrument.ProductName, (ENUM_DAY_OF_WEEK) STime.day_of_week, session_index, date_from, date_to);

      //--- if such session exists
      if(session_exist)
        {
         MqlDateTime STime_date_from;
         TimeToStruct(date_from, STime_date_from);

         MqlDateTime STime_date_to;
         TimeToStruct(date_to, STime_date_to);

         if(
            (STime.day >= STime_date_from.day || STime.day <= STime_date_to.day)        
            || ((STime.hour >= STime_date_from.hour && STime.hour <= STime_date_to.hour)
               && ((STime.min >= STime_date_from.min && STime.min <= STime_date_to.min))))
            return false;
        }
      //--- increase the counter of sessions
      session_index++;
     }

   return true;
  }


Hi There; 

Anyone got a suggestion in code snippet i can use to calculate if a symbol can work within a date range properly?

I have the above mentioned code but it fails miserably on the following:


(STime.day >= STime_date_from.day || STime.day <= STime_date_to.day)
amrali
5489
amrali  
Chris Stols:


Hi There; 

Anyone got a suggestion in code snippet i can use to calculate if a symbol can work within a date range properly?

I have the above mentioned code but it fails miserably on the following:


Try this code:

//+------------------------------------------------------------------+
//| Checks if market is currently open for specified symbol          |
//+------------------------------------------------------------------+
//bool IsMarketOpen(const string symbol)
bool CheckSessionTrade(const string symbol)
  {
   MqlDateTime STime;
   datetime time = TimeTradeServer(STime);
   datetime time_sec = time % PeriodSeconds(PERIOD_D1);
//---
   datetime from, to;
   uint session_index=0;
   while(SymbolInfoSessionTrade(symbol, (ENUM_DAY_OF_WEEK)STime.day_of_week, session_index++, from, to))
      if(time_sec >=from && time_sec <= to)
         return true;
//---
   return false;
  }


//--- check trading session time
   if(!CheckSessionTrade(m_PositionForInstrument.ProductName))
     {
      // if failed the check
      Print("Error: Trading is not allowed [Market closed].");
      PlaySound("timeout.wav");
     }