How to check if market is open for some symbols?

 

SymbolInfoSessionTrade works only for the symbol of the current chart

How can I get info about other symbols?

 
Avi: SymbolInfoSessionTrade works only for the symbol of the current chart. How can I get info about other symbols?

That is incorrect! SymbolInfoSessionTrade works for any symbol. That is why it has a parameter to identify the symbol...

bool  SymbolInfoSessionQuote(
   string            name,                // symbol name
   ENUM_DAY_OF_WEEK  day_of_week,         // day of the week
   uint              session_index,       // session index
   datetime&         from,                // time of the session beginning
   datetime&         to                   // time of the session end
   );
 
Fernando Carreiro #:

That is incorrect! SymbolInfoSessionTrade works for any symbol. That is why it has a parameter to identify the symbol...

I changed the symbol name, but I am getting "false"

It works for me just for symbol in the chart

If I change the symbol in the chart like in parameter, it works

I don't know why

I tried some code I saw here, nothing help

here is example:

bool IsMarketOpen(const string symbol) {
  datetime from, to;
  datetime serverTime = TimeTradeServer();
  MqlDateTime dt;
  TimeToStruct(serverTime,dt);
  const ENUM_DAY_OF_WEEK day_of_week = (ENUM_DAY_OF_WEEK) dt.day_of_week;
  const int time = (int) MathMod(serverTime,PeriodSeconds(PERIOD_D1));
  int session = 0;
   while (SymbolInfoSessionQuote (symbol, day_of_week, session, from, to)) {
    if(time >=from && time <= to ) {
      return true;
    }
    session++;
  }
  return false;
}

and another

 bool  isTradingDay(string symbol)
{
  datetime time=TimeTradeServer();
  MqlDateTime struct_time;         // create MqlDateTime variable
  TimeToStruct(time, struct_time); // convert 'time' variable to a structure 
  datetime fromTime;               // dont need to be initialised
  datetime toTime;                 // dont need to be initialised
  return SymbolInfoSessionQuote(symbol, (ENUM_DAY_OF_WEEK)struct_time.day_of_week, 0, fromTime, toTime);
}      // NOTE: 0 session is the FIRST session for that day (i.e. if 0 = NO TRADING DAY) 

// Checks if market is currently open for specified symbol
 
 
Avi #: I changed the symbol name, but I am getting "false". It works for me just for symbol in the chart. If I change the symbol in the chart like in parameter, it works. I don't know why. I tried some code I saw here, nothing help

Is the other symbol enabled and active in the Market Watch?

EDIT: Is there any error code when it is false?

 
Fernando Carreiro #:

Is the other symbol enabled and active in the Market Watch?

EDIT: Is there any error code when it is false?

yes, the symbol is enabled and active

no error code

I tried eurusd in gbpusd chart and opposite

 
Avi #: yes, the symbol is enabled and active. no error code. I tried eurusd in gbpusd chart and opposite

Here is my test results with the following Script code ... (try running it on your end and report the results please).

string sSymbols[] = {
   "EURUSD",   // Enabled on Market Watch
   "GBPUSD",   // Disabled on Market Watch
   "EURGBP",   // Enabled on Market Watch
   "USDJPY",   // Disabled on Market Watch
   "XAUUSD",   // Enabled on Market Watch
   "NotExist"  // Non-existing symbol
   };

void OnStart( void ) {
   for( int i = 0; i < (int) sSymbols.Size(); i++ ) {
      datetime dtFrom = WRONG_VALUE, dtTo = WRONG_VALUE;
      ResetLastError();
      if( SymbolInfoSessionQuote( sSymbols[i], TUESDAY, 0, dtFrom, dtTo ) )
         Print( i, ") ", sSymbols[i], ": Tuesday from ", dtFrom, " to ", dtTo );
      else
         Print( i, ") ", sSymbols[i], ": Error ", _LastError );
   };
};

2023.11.20 21:24:00.783 Test (EURUSD,H1)        0) EURUSD: Tuesday from 1970.01.01 00:00:00 to 1970.01.01 23:59:00
2023.11.20 21:24:00.783 Test (EURUSD,H1)        1) GBPUSD: Tuesday from 1970.01.01 00:00:00 to 1970.01.01 23:59:00
2023.11.20 21:24:00.783 Test (EURUSD,H1)        2) EURGBP: Tuesday from 1970.01.01 00:00:00 to 1970.01.01 23:59:00
2023.11.20 21:24:00.783 Test (EURUSD,H1)        3) USDJPY: Tuesday from 1970.01.01 00:00:00 to 1970.01.01 23:59:00
2023.11.20 21:24:00.783 Test (EURUSD,H1)        4) XAUUSD: Tuesday from 1970.01.01 01:00:00 to 1970.01.01 23:55:00
2023.11.20 21:24:00.783 Test (EURUSD,H1)        5) NotExist: Error 4301

ERR_MARKET_UNKNOWN_SYMBOL

4301

Unknown symbol



EDIT: Made a correction to include XAUUSD so you could see a different session time.
 
Fernando Carreiro #:

Here is my test results with the following Script code ... (try running it on your end and report the results please).

ERR_MARKET_UNKNOWN_SYMBOL

4301

Unknown symbol



EDIT: Made a correction to include XAUUSD so you could see a different session time.

th

this what I got

 
Avi #: th this what I got

Please show a screenshot of:

  1. your Market Watch
  2. your MetaTrader build and operating system.


 
Avi #: th this what I got

Just noticed now, but your log is NOT from the live Experts tab. It is from the Strategy Tester Journal tab.

Why did you not run it on a Live chart as I did?

 
Fernando Carreiro #:

Just noticed now, but your log is NOT from the live Experts tab. It is from the Strategy Tester Journal tab.

Why did you not run it on a Live chart as I did?

Ok

I did now

But where can I see the resuls?

 
Avi #: Ok. I did now. But where can I see the resuls?
In the Experts tab.