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.
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.
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.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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.
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.
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.
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.
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...
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.
What difference does it make where the orders are located if OrdersTotal is not an array
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
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.
If you don't like "-1", you can
that's not correct... an order with the number 0 will be skipped...
The correct way is as follows