Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1480

 
MrBrooklin #:

For the sake of interest, I removed the d variable. I left only the function. The result of printing has not changed. To be honest, I don't understand about the three values at all.

Regards, Vladimir.

It has not changed because this value was not printed.

Just add printing and you will see.

 
Alexey Viktorov #:

Not changed because this value was not printed.

Just add print and you will see.

Printed it. It stayed the same.

2023.11.22 19:48:24.110 10 (EURUSDrfd,H1)       a и b перед вызовом:14 8
2023.11.22 19:48:24.110 10 (EURUSDrfd,H1)       a и b после вызова:28 4

Maybe I misunderstood something again? Or did you need to print thedvariable? Is this what we were talking about?

Regards, Vladimir.

 
MrBrooklin #:

Printed it out. As it was, everything remains the same.

Regards, Vladimir.

Printed where?

//+------------------------------------------------------------------+
//| передача параметров по ссылке                                    |
//+------------------------------------------------------------------+
double SecondMethod(int &i,int &j)
  {
   double res;
//---
   i*=2;
   j/=2;
   res=i+j;
//---
   return(res);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---.
   int a=14,b=8;
   Print("a и b перед вызовом:",a," ",b);
   double d=SecondMethod(a,b);
   Print("Результат выполнения функции ", d);
   Print("a и b после вызова:",a," ",b, " ",d);// или здесь. А можно и там и тут…
  }
//+------------------------------------------------------------------+
 
Alexey Viktorov #:

Printed it where?

Now I've got it all figured out. )) Thank you all!

Regards, Vladimir.

 
Good morning and good mood everyone! I continue to study the MQL5 programming language. I wrote a small script to calculate the total number of open positions and print out some of its parameters, including error codes, if any:
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   ResetLastError(); // установим значение предопределенной переменной _LastError в ноль
   int pos_total = PositionsTotal(); // объявим переменную для хранения количества открытых позиций
   if(pos_total > 0) // если есть открытые позиции
     {
      for(int i=0; i<pos_total; i++) // запустим цикл и переберём все открытые позиции
        {
         if(PositionGetSymbol(i) == _Symbol) // выберем все открытые позиции по текущему символу
           {
            ulong  pos_id = PositionGetInteger(POSITION_IDENTIFIER);
            double pos_price = PositionGetDouble(POSITION_PRICE_OPEN);
            double pos_tp = PositionGetDouble(POSITION_TP);
            double pos_sl = PositionGetDouble(POSITION_SL);
            PrintFormat("Позиция #%d цена = %G тейк-профит = %G стоп-лосс = %G", pos_id, pos_price, pos_tp, pos_sl);
           }
         else // если выбрать позицию не удалось
           {
            PrintFormat("Не удалось выбрать позицию по символу %s. Ошибка = ", _Symbol, GetLastError());
           }
        }
     }
  }
//+------------------------------------------------------------------+

I ran it on a symbol, where there are open positions. It works properly, the information is printed correctly. Next. Started on the symbol where there are no open positions. The information about the fact that there are no open positions is displayed correctly, but the code of the cause of the error, for some reason no. Can you please help me to understand why the error code is not displayed?

Regards, Vladimir.

 
MrBrooklin #:

Started it on a symbol with open positions. It works properly, the information is displayed correctly. Next. I ran it on a symbol with no open positions. The information about the fact that there are no open positions is displayed correctly, but the code of the cause of the error, for some reason no. Can you please help me to understand why the error code is not displayed?

Where does the error code come from if there is no error? It is just the wrong symbol for the position.

 
JRandomTrader #:

Why would there be an error code if there's no error? It's just the wrong symbol for the position.

Thanks for the response! I expected that at least this error code would be displayed:

ERR_TRADE_POSITION_NOT_FOUND

4753

Position not found


I disagree about the symbol. The symbol is exactly the one on whose chart this script was thrown. I have no open positions on the symbol GBPUSDrfd now, but there is no error either. It is not clear!!!

2023.11.23 13:27:26.385 7 (GBPUSDrfd,H1)        Не удалось выбрать позицию по символу GBPUSDrfd. Ошибка = 
2023.11.23 13:27:26.385 7 (GBPUSDrfd,H1)        Не удалось выбрать позицию по символу GBPUSDrfd. Ошибка = 

Regards, Vladimir.

 
MrBrooklin #:

Thanks for the response! I expected at least this error code to be displayed:

ERR_TRADE_POSITION_NOT_FOUND

4753

Position not found


I disagree about the symbol. The symbol is exactly the one, on the chart of which this script was thrown. I don't have any open positions on the symbol GBPUSDrfd, but I don't have an error either. I don't understand!!!

Regards, Vladimir.

All positions have been found! But none of the found positions has the symbol we need.

 
JRandomTrader #:

All positions found! But none of the found positions has the symbol we need.

I get it now. Thank you!!! It turns out that if there is at least one open position, the error code 4753 will never appear.

Regards, Vladimir.

 
MrBrooklin #:

Got it now. Thank you!!! It turns out that if there is at least one open position, the error code 4753 will never appear.

Regards, Vladimir.

It will appear if you write the following

PositionSelect("мой символ");