Market closed - page 12

Vinicius de Oliveira
3647
Sergey Gritsay #:

A little more tinkering with the code resulted in this.

//+------------------------------------------------------------------+
//|                                                       test06.mq5 |
//|                                                   Sergey Gritsay |
//|                         https://www.mql5.com/ru/users/sergey1294 |
//+------------------------------------------------------------------+
#property copyright "Sergey Gritsay"
#property link      "https://www.mql5.com/ru/users/sergey1294"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetMillisecondTimer(1);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   string text=NULL;
   int symbol_total=SymbolsTotal(true);

   for(int i=0; i<symbol_total; i++)
     {
      string symbol=SymbolName(i,true);
      if(CheckExchTime(symbol,TimeCurrent()))text+="\n"+symbol+": Торговля разрешена";
      else text+="\n"+symbol+": Торговля запрещена";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
bool CheckExchTime(string symbol,datetime times)
  {
   MqlDateTime last_time;
   MqlDateTime start_time;
   MqlDateTime end_time;
   datetime trade_time_start=0;
   datetime trade_time_end=0;
   datetime start=0;
   datetime end=0;

   ResetLastError();
   datetime expiration=(datetime)SymbolInfoInteger(symbol,SYMBOL_EXPIRATION_TIME);
   if(expiration!=0 && times>=expiration)return(false);
   TimeToStruct(times,last_time);
   if(SymbolInfoSessionTrade(symbol,(ENUM_DAY_OF_WEEK)last_time.day_of_week,0,trade_time_start,trade_time_end))
     {
      TimeToStruct(trade_time_start,start_time);
      TimeToStruct(trade_time_end,end_time);
      start=StringToTime((string)last_time.year+"."+(string)last_time.mon+"."+(string)last_time.day+" "+(string)start_time.hour+":"+(string)start_time.min+":00");
      end=StringToTime((string)last_time.year+"."+(string)last_time.mon+"."+(string)last_time.day+" "+(string)end_time.hour+":"+(string)end_time.min+":00");
      if(start==end)return(true);
      if(times>start && times<end)return(true);
     }
//else Print("SymbolInfoSessionTrade(0) failed, error = ",GetLastError());
   if(SymbolInfoSessionTrade(symbol,(ENUM_DAY_OF_WEEK)last_time.day_of_week,1,trade_time_start,trade_time_end))
     {
      TimeToStruct(trade_time_start,start_time);
      TimeToStruct(trade_time_end,end_time);
      start=StringToTime((string)last_time.year+"."+(string)last_time.mon+"."+(string)last_time.day+" "+(string)start_time.hour+":"+(string)start_time.min+":00");
      end=StringToTime((string)last_time.year+"."+(string)last_time.mon+"."+(string)last_time.day+" "+(string)end_time.hour+":"+(string)end_time.min+":00");
      if(start==end)return(true);
      if(times>start && times<end)return(true);
     }
//else Print("SymbolInfoSessionTrade(1) failed, error = ",GetLastError());
   return(false);
  }
//+------------------------------------------------------------------+

Result




Your function solved it for me here.


Thank you!!!


//+--------------------------------------------------------------------------------------------------------------------+
//| This function checks if the symbol is trading at the current time                                                  |
//+--------------------------------------------------------------------------------------------------------------------+
bool CheckTradingTime(string symbol)
  {
//--- Local variables
   datetime    times = TimeCurrent(), trade_time_start = 0, trade_time_end = 0, start = 0, end = 0;
   MqlDateTime last_time, start_time, end_time;

//--- Check symbol trade end date
   datetime expiration = (datetime)SymbolInfoInteger(symbol, SYMBOL_EXPIRATION_TIME);
   if(expiration != 0 && times >= expiration)
     {
      return(false);
     }

   TimeToStruct(times, last_time);

//--- Session index: 0
   if(SymbolInfoSessionTrade(symbol, (ENUM_DAY_OF_WEEK)last_time.day_of_week, 0, trade_time_start, trade_time_end))
     {
      TimeToStruct(trade_time_start, start_time);
      TimeToStruct(trade_time_end,   end_time);
      start = StringToTime((string)last_time.year + "." + (string)last_time.mon + "." + (string)last_time.day + " " + (string)start_time.hour + ":" + (string)start_time.min + ":00");
      end   = StringToTime((string)last_time.year + "." + (string)last_time.mon + "." + (string)last_time.day + " " + (string)end_time.hour   + ":" + (string)end_time.min   + ":00");
      if(start == end)
        {
         return(true);
        }
      if(times >= start && times < end)
        {
         return(true);
        }
     }

//--- Session index: 1
   if(SymbolInfoSessionTrade(symbol, (ENUM_DAY_OF_WEEK)last_time.day_of_week, 1, trade_time_start, trade_time_end))
     {
      TimeToStruct(trade_time_start, start_time);
      TimeToStruct(trade_time_end,   end_time);
      start = StringToTime((string)last_time.year + "." + (string)last_time.mon + "." + (string)last_time.day + " " + (string)start_time.hour + ":" + (string)start_time.min + ":00");
      end   = StringToTime((string)last_time.year + "." + (string)last_time.mon + "." + (string)last_time.day + " " + (string)end_time.hour   + ":" + (string)end_time.min   + ":00");
      if(start == end)
        {
         return(true);
        }
      if(times >= start && times < end)
        {
         return(true);
        }
     }

//--- Check completed
   return(false);
  }