Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1195

 
I'm on an android.
 
baxromsaid:
I dont know how to open a demo account.


baxromsaid:
I'm on android.


MetaQuotes is not a broker - therefore you can only open a demo account on MetaQuotes-Demo servers.

A real trading account must be opened with a broker.

 

Hi connoisseurs !


Can you please tell me why my code doesn't set vol and ask? The value zero is always displayed in Print. Spread is "clamping" normally, it shows the correct value.

I attach EA to LTCUSD chart.

What is the error?

#include <Trade\SymbolInfo.mqh>
CSymbolInfo m_symbol;
int a=0,b=0,c=0,spread=0,i=0,j=0;
double x=0,y=0,z=0,m=0,ask=0;
input int vyb=7;
bool bool_1=true,bool_2=true,bool_3=true;
string str_1="",str_2="",str_3="",name="",Text[101];
ulong vol=0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  name=_Symbol;
  Print("текущий символ: ",name); 
  spread=m_symbol.Spread();
  Print("спред по символу: ",spread);
  vol=m_symbol.Volume();
  Print("обьєм по символу ",name," - ",vol);
  ask=m_symbol.Ask();
  Print("Цена Аск равно ",ask);
 
odyn:

Hi connoisseurs !


Can you please tell me why my code doesn't set vol and ask? The value zero is always displayed in Print. Spread is "clamping" normally, it shows the correct value.

I attach EA to LTCUSD chart.

What is the error?

You should apply either CSymbolInfo::Refresh or CSymbolInfo::RefreshRates method before getting it - depending on what you need.

Here is the code of these functions:

//+------------------------------------------------------------------+
//| Refresh cached data                                              |
//+------------------------------------------------------------------+
bool CSymbolInfo::Refresh(void)
  {
   long tmp=0;
//---
   if(!SymbolInfoDouble(m_name,SYMBOL_POINT,m_point))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_TRADE_TICK_VALUE,m_tick_value))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_TRADE_TICK_VALUE_PROFIT,m_tick_value_profit))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_TRADE_TICK_VALUE_LOSS,m_tick_value_loss))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_TRADE_TICK_SIZE,m_tick_size))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_TRADE_CONTRACT_SIZE,m_contract_size))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_VOLUME_MIN,m_lots_min))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_VOLUME_MAX,m_lots_max))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_VOLUME_STEP,m_lots_step))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_VOLUME_LIMIT,m_lots_limit))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_SWAP_LONG,m_swap_long))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_SWAP_SHORT,m_swap_short))
      return(false);
   if(!SymbolInfoInteger(m_name,SYMBOL_DIGITS,tmp))
      return(false);
   m_digits=(int)tmp;
   if(!SymbolInfoInteger(m_name,SYMBOL_ORDER_MODE,tmp))
      return(false);
   m_order_mode=(int)tmp;
   if(!SymbolInfoInteger(m_name,SYMBOL_TRADE_EXEMODE,tmp))
      return(false);
   m_trade_execution=(ENUM_SYMBOL_TRADE_EXECUTION)tmp;
   if(!SymbolInfoInteger(m_name,SYMBOL_TRADE_CALC_MODE,tmp))
      return(false);
   m_trade_calcmode=(ENUM_SYMBOL_CALC_MODE)tmp;
   if(!SymbolInfoInteger(m_name,SYMBOL_TRADE_MODE,tmp))
      return(false);
   m_trade_mode=(ENUM_SYMBOL_TRADE_MODE)tmp;
   if(!SymbolInfoInteger(m_name,SYMBOL_SWAP_MODE,tmp))
      return(false);
   m_swap_mode=(ENUM_SYMBOL_SWAP_MODE)tmp;
   if(!SymbolInfoInteger(m_name,SYMBOL_SWAP_ROLLOVER3DAYS,tmp))
      return(false);
   m_swap3=(ENUM_DAY_OF_WEEK)tmp;
   if(!SymbolInfoDouble(m_name,SYMBOL_MARGIN_INITIAL,m_margin_initial))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_MARGIN_MAINTENANCE,m_margin_maintenance))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_MARGIN_LONG,m_margin_long))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_MARGIN_SHORT,m_margin_short))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_MARGIN_LIMIT,m_margin_limit))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_MARGIN_STOP,m_margin_stop))
      return(false);
   if(!SymbolInfoDouble(m_name,SYMBOL_MARGIN_STOPLIMIT,m_margin_stoplimit))
      return(false);
   if(!SymbolInfoInteger(m_name,SYMBOL_EXPIRATION_MODE,tmp))
      return(false);
   m_trade_time_flags=(int)tmp;
   if(!SymbolInfoInteger(m_name,SYMBOL_FILLING_MODE,tmp))
      return(false);
   m_trade_fill_flags=(int)tmp;
//--- succeed
   return(true);
  }
//+------------------------------------------------------------------+
//| Refresh cached data                                              |
//+------------------------------------------------------------------+
bool CSymbolInfo::RefreshRates(void)
  {
   return(SymbolInfoTick(m_name,m_tick));
  }
Документация по MQL5: Стандартная библиотека / Торговые классы / CSymbolInfo / Refresh
Документация по MQL5: Стандартная библиотека / Торговые классы / CSymbolInfo / Refresh
  • www.mql5.com
Стандартная библиотека / Торговые классы / CSymbolInfo / Refresh - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Vladimir Karputov:

You must apply either CSymbolInfo::Refresh or CSymbolInfo::RefreshRates method before getting - depending on what you need

Thank you very much, dear Vladimir!

Maybe you can also tell me how to get the symbol name with theCSymbolInfo Name method? When I try to do so

#include <Trade\SymbolInfo.mqh>
CSymbolInfo m_symbol;
int a=0,b=0,c=0,spread=0,i=0,j=0;
double x=0,y=0,z=0,m=0,ask=0;
input int vyb=7;
bool bool_1=true,bool_2=true,bool_3=true;
string str_1="",str_2="",str_3="",name="",Text[101];
ulong vol=0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  name=m_symbol.Name(Symbol())
  Print("текущий символ: ",name); 

I get a boolean value true in the name variable.

 
odyn:

Thank you very much, dear Vladimir!

Maybe you can also suggest: how to get a symbol name usingCSymbolInfo Name method? When I try to do it this way

I get a boolean value true in the name variable.

I do so - I initialize variable of CSymbolInfo class.

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
***
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
   RefreshRates();
***
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
      return(false);
     }
//---
   return(true);
  }


If you want to get a name (for example when comparing position symbol with current symbol), you need to use CSymbolInfo::Name() method.

Example usage:

//+------------------------------------------------------------------+
//| Is position exists                                               |
//+------------------------------------------------------------------+
bool IsPositionExists(void)
  {
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
            return(true);
//---
   return(false);
  }


The help shows two forms of calling the Name method

Name

Gets the name of financial instrument.

string  Name() const

Returned value

Name of financial instrument.


Name

Sets the name of the financial instrument for further work with it.

bool  Name(string name)

Returned value

No.

Документация по MQL5: Стандартная библиотека / Торговые классы / CSymbolInfo / Name
Документация по MQL5: Стандартная библиотека / Торговые классы / CSymbolInfo / Name
  • www.mql5.com
Стандартная библиотека / Торговые классы / CSymbolInfo / Name - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Vladimir Karputov:


If you want to get a name (e.g. when comparing a position symbol to the current symbol), you need to use the CSymbolInfo::Name() method.

Example usage:


The help shows two forms of calling the Name method

Name

Gets the name of financial instrument.

Returned value

Name of financial instrument.

I have a question on this form of method Name.

HOW do I get instrument's NAME in the variable? Because doing it this way:

#include <Trade\SymbolInfo.mqh>
CSymbolInfo m_symbol;
string str_1="",str_2="",str_3="",name="";
name=m_symbol.Name();
Print("текущий символ: ",name);

I get boolean true inname. How to get a string with name of the instrument in it, usingmethod CSymbolInfo::Name() ?

Документация по MQL5: Стандартная библиотека / Торговые классы / CSymbolInfo / Name
Документация по MQL5: Стандартная библиотека / Торговые классы / CSymbolInfo / Name
  • www.mql5.com
Стандартная библиотека / Торговые классы / CSymbolInfo / Name - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
odyn:

I have a question about this particular form of the Name method.

HOW do I get tool's NAME into a variable? Because doing this:

I get a boolean true inname. How do I get a string with tool name in it, usingCSymbolInfo::Name()method?

First we INITIALIZE it. Then we use method Name().

We run the code on the '' character.

//+------------------------------------------------------------------+
//|                                                            1.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property version   "1.00"
#include <Trade\SymbolInfo.mqh>
//---
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return;
     }
   string name=m_symbol.Name();
   Print("текущий символ: ",name);
//---
  }
//+------------------------------------------------------------------+

Result:

1 (EURUSD,H1)   текущий символ: EURUSD
 
Vladimir Karputov:

First we INITIALIZE. Then we use the Name() method.

Run the code on the '' character.

Result:

Got it. Thanks for your patience )).


PS: my compiler is blaming return in your code. When I removed it, everything is fine.

 
odyn:

Got it. Thanks for your patience )).


PS: my compiler is blaming return in your code. When I removed it, it's fine.

This is a script (I gave an example of a script), not an EA. If you copied my code into EA, there will be an error.

Reason: