Self-learning the MQL5 language from scratch - page 76

 
MrBrooklin: .
if(Symbol_Main==_Symbol)
Why dance when there is a direct request for the current chart symbol.
 
Konstantin Nikitin:
MrBrooklin: . Why dance when there is a direct request for the current chart symbol.

Hello Konstantin, I needed to compare the symbol that was specified in the input parameters with the symbol that the EA is attempting to set to.

The code you suggested does not work either.

Regards, Vladimir.

 
MrBrooklin:

Hello Konstantin, I needed to compare the symbol that was specified in the input parameters with the symbol that the EA is attempting to set to.

The code you suggested does not work either.

Regards, Vladimir.

In your settings you have specified

input string Symbol_Main="GBPUSDrfd";  //Валютная пара, на которую ставим советник

_Symbol

The _Symbol variable stores the name of the current chart symbol.

So, the check for the symbol in the current chart should work fine.

if(Symbol_Main==_Symbol)
 
Konstantin Nikitin:

In your settings you have specified

_Symbol

The _Symbol variable stores the name of the current chart symbol.

So, the symbol check by the current chart should be successful.

Thank you, Konstantin! It all worked! I made a mistake when correcting the code. I fixed it now and it worked. God grant you health and prosperity!

I wish you all a Happy New Year!

Regards, Vladimir.

 

The code should look like this:

input string Symbol_Main="GBPUSDrfd";  //Валютная пара, на которую ставим советник

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   /* Определим график валютной пары, на который будем устанавливать советник*/
   if(Symbol_Main==_Symbol) //если график валютной пары совпадает со входным параметром
     {
      /* выводим окно сообщений на торговом терминале и продолжаем работу советника */
      MessageBox("Работа советника на данной валютной паре разрешена!");
      return(INIT_SUCCEEDED); //возвращаем для функции OnInit значение означающее "удачная инициализация"
     }
   else //в противном случае, если график валютной пары не совпадает со входным параметром
     {
      /* выводим окно сообщений на торговом терминале и закрываем советник */
      MessageBox("Работа советника на данной валютной паре запрещена! Выходим!");
      return(INIT_FAILED); //возвращаем для функции OnInit значение означающее "неудачная инициализация"
     }
  }

Note: In the Symbol_Main input parameter, you should specify the name of the currency pair as prescribed by your forex dealer. In my case GBPUSD symbol has the ending rfd.

Be careful!

Regards, Vladimir.

 
MrBrooklin:

The code should look like this:

Note: In the Symbol_Main input parameter, you need to specify the name of the currency pair as prescribed by your forex dealer. In my case GBPUSD symbol has the ending rfd.

Be careful!

Sincerely, Vladimir.

void OnStart()
  {
     Print(CheckSymbol("eurus"));
  }
//+------------------------------------------------------------------+
string CheckSymbol(const string _symboll)
  {
   string symbol = _symboll;
   if(!StringToUpper(symbol))
      return NULL;
//---
   for(int i=0; i<SymbolsTotal(false); i++)
     {
      string s = SymbolName(i, false);
      if(!StringToUpper(s))
         continue;
      if(s == symbol || StringFind(symbol, s, 0) >= 0 || StringFind(s, symbol, 0) >= 0)
         if(SymbolSelect(symbol, true))
            return SymbolName(i, false);
     }
//---
   return NULL;
  }

And enjoy your life )))) Pay attention that I am asking even not completely written symbol and case is not important.

 
Konstantin Nikitin:

And enjoy your life )))) Note that I request even not completely written symbol and case is not important.

Thanks a lot, Konstantin, for the great tip! I will certainly use it in my EA. I think this function will also be useful for beginners in learning the MQL5 programming language.

I am still as close as crawling to Peking!

Regards, Vladimir.

 
Konstantin Nikitin:

And enjoy your life )))) Note that I am even asking for a character that is not completely written and is not case sensitive.

Why should you make fun of an immature mind? Your code does not meet the requirement at all. If you enter only "eu" or "eur" in the input parameters, that part of the condition

|| StringFind(s, symbol, 0) >= 0)

will be fulfilled no matter what currency pair contains "EU" in its name and it does not matter EUR as base currency or quoted currency. And why go through SymbolName MarketWatch searching for the right symbol?

 
Alexey Viktorov:

Why make such a mockery of an immature mind? Your code does not meet the requirement at all. If only "eu" or "eur" is entered in the input parameters, this part of the condition

will be fulfilled no matter what currency pair contains "EU" in its name and it does not matter EUR as base currency or quoted currency. And why should I go through the SymbolName of MarketWatch searching for the right symbol?

Hello Alexey! To be honest, I'm not good at programming, so I take a lot on faith. Does it mean that the code provided by Konstantin should be reworked?

Sincerely, Vladimir.

 
MrBrooklin:

The code should look like this:

Note: In the Symbol_Main input parameter, you should specify the name of the currency pair as prescribed by your forex dealer. In my case GBPUSD symbol has the ending rfd.

Be careful!

Sincerely Vladimir.

I would do it like this

input string Symbol_Main="GBPUSD";  //Валютная пара, на которую ставим советник

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   /* Определим график валютной пары, на который будем устанавливать советник*/
   if(StringFind(_Symbol, Symbol_Main, 0) == 0) //если график валютной пары содержит входной параметр
     {
      /* выводим окно сообщений на торговом терминале и продолжаем работу советника */
      MessageBox("Работа советника на данной валютной паре разрешена!");
      return(INIT_SUCCEEDED); //возвращаем для функции OnInit значение означающее "удачная инициализация"
     }
   else //в противном случае, если график валютной пары не совпадает со входным параметром
     {
      /* выводим окно сообщений на торговом терминале и закрываем советник */
      MessageBox("Работа советника на данной валютной паре запрещена! Выходим!");
      return(INIT_FAILED); //возвращаем для функции OnInit значение означающее "неудачная инициализация"
     }
  }