Questions from Beginners MQL5 MT5 MetaTrader 5 - page 624

 
Leo59:
Can you tell me why this might happen?

double A;
int x, y;

x=100; y=3; A=x/y=33,000
Целый тип int x=100 / Целый тип int y=3 => Целое число 33.
Так как "А" - это вещественное число, то Целое число "33" преобразовывается к вещественному и получаем 33.0
 
Karputov Vladimir:
Thanks Vladimir!

This is an interesting feature. You go and search in the search engine but you don't find anything. When you ask a question in the forum and wait for an answer, you find it by yourself ))


https://www.mql5.com/ru/articles/1561

Peculiarities of working with double numbers in MQL4

Особенности работы с числами типа double в MQL4
Особенности работы с числами типа double в MQL4
  • 2009.11.02
  • MetaQuotes Software Corp.
  • www.mql5.com
В данной заметке собраны советы по решению наиболее часто возникающих ошибок при работе с числами типа double в программах на MQL4.
 
Leo59:
Please tell me, what can cause this?

double A;
int x, y;

x=100; y=3; A=x/y=33.000

as a rule, I guess:

Variables that are used in mathematical calculations, it is best to declare them as double

If these calculations use an incremental loop variable, we can use double y=y+1 or double y=y-1;

Распределенные вычисления в сети MQL5 Cloud Network
Распределенные вычисления в сети MQL5 Cloud Network
  • cloud.mql5.com
Заработать деньги, продавая мощности своего компьютера для сети распределенных вычислений MQL5 Cloud Network
 
new-rena:

as a rule, I guess:

Variables that are used in mathematical calculations are best declared as double.

or double y=y+1 or double y=y-1;

There is also an explicit type conversion:

double A;
int x, y;

x=100; y=3; 
A=(double)x/y; // в операциях умножения и деления достаточно привести к double только одно
 

Hello.

Please tell me how to find a position with a certain lot among the open positions.

The symbol and the magic number are unknown. I need the following condition: if ( position's lot== 0.1) then {some action;}

 
mila.com:

Hello.

Can you please tell me how to find among the open positions a position with a certain lot.

The symbol and the magic number are unknown. I need about the following condition: if ( position's lot== 0.1) then {some action;}

Here is an example of a script that goes through all open positions and compares their volume to a given one (variable"lot"):

//+------------------------------------------------------------------+
//|                                               PositionsTotal.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property  script_show_inputs
//--- input parameter
input double lot=0.01; // искомый лот
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int count_positions=PositionsTotal(); // количество открытых позиций
   for(int i=0;i<count_positions;i++)
     {
      ResetLastError();
      //--- возвращает символ соответствующей открытой позиции и автоматически выбирает позицию для дальнейшей работы с ней 
      string name=PositionGetSymbol(i);
      if(name==NULL)
        {
         Print("PositionGetSymbol Error# ",GetLastError());
         return;
        }

      double position_lot=PositionGetDouble(POSITION_VOLUME); // получаем объем позиции
      if(position_lot!=0)
        {
         if(position_lot==lot)
           {
            // найдена позиция с заданным объёмом 
           }
        }
     }
  }
//+------------------------------------------------------------------+
Files:
 
Karputov Vladimir:

Here's an example of a script that goes through all open positions and compares their volume to a given one (variable"lot"):

Thank you, I'm sorry, I didn't specify, is there something similar for MQL4?

 
mila.com:

Thank you, I'm sorry, I didn't specify, is there something similar for MQL4?

No, no. And why, when there is MetaTrader 5 with netting and hedging and you can trade on real exchanges?

 
Karputov Vladimir:

Here is an example of a script that goes through all open positions and compares their volume to a given one (variable"lot"):

Vladimir, can I ask how you envisage a position with a volume of 0.0 in this code?

      double position_lot=PositionGetDouble(POSITION_VOLUME); // получаем объем позиции
      if(position_lot!=0)
 
Alexey Viktorov:

Vladimir, can I ask how you envisage the 0.0 position in this code?

This is a check to return the PositionGetDouble function. If it fails, it returns 0 - i.e. it is an error.
Reason: