Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1686

 
Aleksei Stepanenko #:
is greater than or equal to, because the first element of the array also needs to be taken into account, and its index in the array is zero.
I don't understand what array everyone is talking about here. If OrdersTotal doesn't work like that. It's not an array, it's a function that returns a number.
 
Aleksei Stepanenko #:
No, it's OK. If there are no positions, then i=OrdersTotal()-1 will be equal to -1. And the loop condition i>=0 will not be executed immediately. And the program will not enter the loop.
But this is not correct, the "sum of orders" is 1, i.e. we will always exclude 1 order from the loop.
 
Nerd Trader #:
I don't understand what array everyone is referring to here. If OrdersTotal doesn't work like that.

Orders are essentially in an array in the program, we just don't see it. OrdersTotal is equal to ArraySize, and shows the total number of elements.

In an array, the index of the first element always starts with zero, and the size of the last element minus one.

Пример: всего 5 ордеров, индексы: 0,1,2,3,4.

или массив из 5 элементов, индексы те же.

for(int i=5-1; i>=0; i--)
 
Aleksei Stepanenko #:

Orders are essentially in an array in the program, we just don't see it. OrdersTotal is equal to ArraySize, and shows the total number of elements.

The index of the first element in the array is always zero, while the size of the last one, respectively, is minus one.

What difference does it make where the orders are located, if OrdersTotal isn't an array. It returns the number of orders, not the array index.

 
Nerd Trader #:
But that's not correct, it turns out "sum of orders" is 1, which means 1 order will always be excluded from the loop.

the orders are in a numbered list... The list counts down from 0...

For example

serial number 0, buy type, lot 0.1

serial number 1, sell type, lot 1

serial number 2, sell type, lot 0.5

sequence number 3, buy type, lot 0.16

If you now call OrdersTotal() it will = 4

But if we want to navigate through all rows of the list, we need to go through numbers 0 1 2 3, number 4 is not here... although there are 4 orders.

So a trick is done, when putting together a loop, minus 1 on the number of orders, that's it...


Nerd Trader#:

What difference does it make where the orders are located if OrdersTotal is not an array. It returns the number of orders, not the array index.

Correct, BUT there is an implicit fact, having the number of orders you can get the numbers of all the orders in the list
Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
  • 2021.10.19
  • www.mql5.com
В этой ветке я хочу начать свою помощь тем, кто действительно хочет разобраться и научиться программированию на новом MQL4 и желает легко перейти н...
 
OrdersTotal is the total number, but indexes in almost all programming languages start with 0, not 1. Just get used to it. Move back one unit and that's it, and it will be the same as you think.
 
Nerd Trader #:

What difference does it make where the orders are located if OrdersTotal is not an array

The number of orders and the order numbers in the array are different things. And there are also tickets)
 
Nerd Trader #:
But this is not correct, you get "sum of orders" - 1, i.e. 1 order will always be excluded from the loop.

If you don't like "-1", you can do this

for(int i = OrdersTotal(); i >= 1 ; i --)
 
Aleksei Stepanenko #:

Orders are essentially in an array in the program, we just don't see it. OrdersTotal is equal to ArraySize, and shows the total number of elements.

In an array, the index of the first element always starts with zero, and the size of the last one - minus one.

So total and array index are different things, why do you think it's the same? To create an array with 1 index, type 1, not 0, (int ar[1]), so ArraySize( ar ) also returns 1, not 0.

 
MakarFX #:

If you don't like "-1", you can

for(int i = OrdersTotal(); i > 0 ; i --)

that's not correct... an order with the number 0 will be skipped...

The correct way is as follows

for(int i = OrdersTotal()-1; i >= 0 ; i --)
 {
 }
Reason: