How to use the select symbol "by Name method" in order to use CSymbolInfo functions?

 

I've read the documentation and I understand you need to select a symbol using the Name method in order to use any of the functions of CSymbolInfo. What I don't understand is how to do that. Every function in the documentation says:


When I click on that all I get is:

Sorry if this is an obvious answer. A simple example would be great. Thank you for any help

 
Please show your code attempt.
 
// Simple example
#include <Trade\SymbolInfo.mqh>
void OnStart() {
   CSymbolInfo oSymbol;
   if( oSymbol.Name( "EURUSD" ) ) {
      oSymbol.Refresh();
      // do something
   };
};
 
Alain Verleyen #:
Please show your code attempt.
#include <Trade\SymbolInfo.mqh>
CSymbolInfo ChartInfo;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   ChartInfo.Name(Symbol());
   ChartInfo.Refresh();
   
   double ask = ChartInfo.Ask();
   double bid = ChartInfo.Bid();
   
   Print("Ask: ",ask," Bid: ",bid);
  }

It always returns zero. I haven't tried other functions yet

 
Kurren Kidd #:It always returns zero. I haven't tried other functions yet

You are not checking the return value from the Name method to check if it succeeded or not ...

Also, you need to use RefreshRates for the Ask and Bid.
// Sample code
#include <Trade\SymbolInfo.mqh>
void OnStart() {
   CSymbolInfo oSymbol;
   if( oSymbol.Name( _Symbol ) ) {
      oSymbol.RefreshRates();
      double dbAsk = oSymbol.Ask(),
             dbBid = oSymbol.Bid();
      Print( "Ask: ", dbAsk, " Bid: ", dbBid );
   };
};
2023.05.15 20:43:32.700 TestTicks (EURUSD,H1)   Ask: 1.08763 Bid: 1.08762
 
Kurren Kidd #:

It always returns zero. I haven't tried other functions yet

You need to use RefreshRates() method, not Refresh().
 
Alain Verleyen #:
You need to use RefreshRates() method, not Refresh().

Thank you! This worked

 
Kurren Kidd #: I'm still getting zeros printed. Is there anything else you can think of? Thank you for the help so far
I updated my post #4. Please read it again.
 
Fernando Carreiro #:

You are not checking the return value from the Name method to check if it succeeded or not ...

Also, you need to use RefreshRates for the Ask and Bid.

Thank you too! This was driving me nuts

 
Kurren Kidd #:Thank you too! This was driving me nuts
Refresh and RefreshRates, updates different data for the symbol. Have a look at the source code of CSymbolInfo so that you can understand it better.
 
Fernando Carreiro #:
Refresh and RefreshRates, updates different data for the symbol. Have a look at the source code of CSymbolInfo so that you can understand it better.

I will, reading up now. I finally understand how it works. Thank you thank you

Reason: