Well this means that your code "features" another bug somewhere..
Because it has to be OrdersTotal()-1.
Every working code does it like that and also the fact that your program did not work correctly is proof of that very fact.
Think of it: OrdersTotal() returns the amount of orders, so, if, say, you have 3 orders then OrdersTotal() will return 3;
however, the position of orders starts at 0 meaning:
order 1 will have the position 0, thus, order 3 will reside at position 2 when accessed with OrderSelect() by position.
Now, if you excluded your calculation from the loop then the logical error might not affect the calculation indeed.
Because it has to be OrdersTotal()-1.
Every working code does it like that and also the fact that your program did not work correctly is proof of that very fact.
Think of it: OrdersTotal() returns the amount of orders, so, if, say, you have 3 orders then OrdersTotal() will return 3;
however, the position of orders starts at 0 meaning:
order 1 will have the position 0, thus, order 3 will reside at position 2 when accessed with OrderSelect() by position.
Now, if you excluded your calculation from the loop then the logical error might not affect the calculation indeed.