Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1581

 
Vitaly Muzichenko fill an array up to 100 elements nicely?

Tell me, what is the += operator for? After all, there is zero in the array... Therefore, a simple assignment operator = is enough here.

 
Alexey Viktorov #:

Tell me, what is the += operator for? After all, there is zero in the array... Therefore, a simple assignment operator = is enough here

I typed it on the site, and then I couldn't edit it. You need to sleep at night, not to suffer with codes :)

 
Yuriy Bykov #:

Since the indices on which the value of an element changes do not have a simple dependence (like every next element is 7 greater than the previous one), it is necessary to have them in the code. The multiplier by which R is multiplied is an arithmetic progression with a step of 1. Therefore, we can implement that code approximately like this:

Note that for i < 10 and i < 18 the multiplier does not change, so the number 10 is not included in the bounds array of index bounds.

If the multipliers can change in different ways, they can also be organised as an array:

One more thing like this. The array initialisation string is of the following form:

is guaranteed to initialise only the first element of the array with zero. Try executing this code:

and you will see in the output:

For full initialisation, you must either list all 100 values for an array of 100 elements in parentheses, or use the ArrayInitialize() function, for example.

But in your task, you don't need to initialise the array because you are not modifying the existing initial values, but assigning new ones each time.

Thank you!

I will take the second option.

 
Vitaly Muzichenko fill an array up to 100 elements nicely?
Add one more loop with 7 and check .for R write indexing.even without it the code is disgusting simply
 
Рaра Нoth #:
Add one more loop with 7 and a check for R. Write indexing for R. Even without it, the code is disgusting.

I don't understand anything.

 
Vitaly Muzichenko #:

I don't get it.

Look how a matrix is drawn with two loops, but set values at a separate index in one loop
 

Good day and good mood everyone!

The article contains the code of the function of checking the sufficiency of funds for a trade operation:

bool CheckMoneyForTrade(string symb,double lots,ENUM_ORDER_TYPE type)
  {
//--- получим цену открытия
   MqlTick mqltick;
   SymbolInfoTick(symb,mqltick);
   double price=mqltick.ask;
   if(type==ORDER_TYPE_SELL)
      price=mqltick.bid;
//--- значения необходимой и свободной маржи
   double margin,free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   //--- вызовем функцию проверки
   if(!OrderCalcMargin(type,symb,lots,price,margin))
     {
      //--- что-то пошло не так, сообщим и вернем false
      Print("Error in ",__FUNCTION__," code=",GetLastError());
      return(false);
     }
   //--- если не хватает средств на проведение операции
   if(margin>free_margin)
     {
      //--- сообщим об ошибке и вернем false
      Print("Not enough money for ",EnumToString(type)," ",lots," ",symb," Error code=",GetLastError());
      return(false);
     }
//--- проверка прошла успешно
   return(true);
  }

What is highlighted in yellow is that we first declared the price variable and assigned the Ask price value to it, and literally after the line where the order type is checked, the Bid value is assigned accordingly.

Question - why can't Bid be assigned at the stage of declaring the price variable at once, or is there some sacred meaning here?

Regards, Vladimir.

 
MrBrooklin #:

Good day and good cheer everyone!

The article contains the code of the function to check if there are enough funds to perform a trade operation:

What is highlighted in yellow is that we first declared the price variable and assigned the Ask price value to it, and literally after the line where the order type is checked, the Bid value is assigned accordingly.

Question - why can't Bid be assigned immediately at the stage of declaring the price variable or is there some sacred meaning here?

Regards, Vladimir.

Vladimir, why do you need such perversions? It is easier to use the OrderCheck() function. Moreover, the MqlTradeRequest structure can be kept declared at the level of global variables and after a successful check can be used without filling it again before OrderSend().

When you get the margin size, you have to perform arithmetic operations... And here everything is already calculated in the structure.

struct MqlTradeCheckResult 
  { 
   uint         retcode;             // Код ответа 
   double       balance;             // Баланс после совершения сделки 
   double       equity;              // Эквити после совершения сделки 
   double       profit;              // Плавающая прибыль 
   double       margin;              // Маржевые требования 
   double       margin_free;         // Свободная маржа 
   double       margin_level;        // Уровень маржи 
   string       comment;             // Комментарий к коду ответа (описание ошибки) 
  };
 
Alexey Viktorov #:

Vladimir, why such perversions??? It is easier to use the OrderCheck() function. Moreover, the MqlTradeRequest structure can be kept declared at the level of global variables and after a successful check can be used without re-filling before OrderSend().

When you get the margin size, you have to perform arithmetic operations... And here everything is already calculated in the structure.

What if EA is set on more than one pair?

OrderCheck() is somehow more reliable.
 
Alexey Viktorov #:

Vladimir, why such perversions??? It is easier to use the OrderCheck() function. Moreover, the MqlTradeRequest structure can be kept declared at the level of global variables and after a successful check can be used without re-filling before OrderSend().

When you get the margin size, you have to perform arithmetic operations... And here everything is already calculated in the structure.

Hi Alexey, I understand your variant, Vitaly's hint is reasonable too.

But still! This code is taken from an article written by MetaQuotes specialists and I have no reason not to trust them. I just had a question I would like to get an answer to. ))

Regards, Vladimir.