Overview

Many MQL5 programs need to know which instrument they are running on. The built-in Symbol() function returns the current chart’s symbol as a string, allowing you to print it for diagnostics or use it to request prices, trade, or load indicator data.

Ad

What You Will Be Able to Do

  • Retrieve the active chart symbol with Symbol().
  • Print the symbol and related data to the Experts log.
  • Combine Symbol() with market info functions such as SymbolInfoDouble().
  • Apply symbol-aware logic in scripts and Expert Advisors.

Code Example


//+------------------------------------------------------------------+
//|                                                  GetSymbolName.mq5 |
//+------------------------------------------------------------------+
#property version "1.00"
#property strict

void OnStart()
  {
   // Current chart symbol
   string sym = Symbol();
   Print("Active chart symbol: ", sym);

   // Get Bid price and digits for the active symbol
   double bid  = 0.0;
   int    digs = (int)SymbolInfoInteger(sym, SYMBOL_DIGITS);

   if(SymbolInfoDouble(sym, SYMBOL_BID, bid))
     {
      Print("Bid price: ", DoubleToString(bid, digs));
     }
   else
     {
      Print("Warning: failed to read Bid for symbol ", sym);
     }

   // Example: using a different symbol explicitly (if available in Market Watch)
   string other = "EURUSD";
   if(SymbolSelect(other, true))
     {
      double other_bid = SymbolInfoDouble(other, SYMBOL_BID);
      int other_digs   = (int)SymbolInfoInteger(other, SYMBOL_DIGITS);
      Print(other, " Bid: ", DoubleToString(other_bid, other_digs));
     }
   else
     {
      Print("Info: ", other, " not visible in Market Watch.");
     }
  }
Ad

Execution Steps

  1. Open MetaEditorFile → New → Script and name it GetSymbolName.
  2. Paste the code above and press F7 to compile.
  3. In MetaTrader 5, attach the script to any chart.
  4. Open the Experts tab to confirm the symbol name and Bid price are printed.
  5. (Optional) Add other symbols to Market Watch and re-run to see explicit symbol queries.

Key Point

Symbol() is context-sensitive: it returns the symbol of the chart where the program runs. When an EA must handle multiple instruments, pass symbol names explicitly instead of relying on the current chart context.

Ad

Next Section

→ Next: What Are Indicators