[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 493

 
hoz:

I am currently learning how to write one EA in terms of understanding the source code, as the EA is very well written.

But one thing is strange there.

Why is the number of all orders assigned toOrdersTotal() - 1, and not justOrdersTotal()?

Because if we have 0 orders in total, then the value of total will be -1 instead of 0.

After that, check where the total is used. Most likely, we then loop through the orders for (i=0; i<=total; i++).
 
paladin80:
Look where total is used afterwards. Most likely, then the loop goes through the orders for (i=0; i<=total; i++).

By the way, yes... I've already added the full code of this function above:

void FindOrders()
{
// - 1 - == Инициализация переменных перед поиском ======================================
   int total = OrdersTotal() - 1;
   g_type = -1;                                    // На текущий момент у нас нет позиций
// - 1 - == Окончание блока =============================================================
 
// - 2 - == Непосредственно поиск =======================================================
   for (int i = total; i >= 0; i--)                // Используется весь список ордеров
      if (OrderSelect(i, SELECT_BY_POS))           // Убедимся, что ордер выбран
         if (MathFloor(OrderMagicNumber()) == i_magicNumber &&// Ордер открыт
             OrderSymbol() == Symbol())            // ..экспертом, который прикреплен к..
         {                                         // ..текущей паре
            g_ticket = OrderTicket();              // Запишем данные ордера
            g_type = OrderType();
         } 
// - 2 - == Окончание блока =============================================================
}

I figured it would be more logical not to write it this way:

int total = OrdersTotal() - 1;

and then set the loop like this:

for(i=total; i>=1; i--)

Right? Just somehow it's not perceived very well when the counter of number of positions from zero... it's not logical and, therefore, why confuse yourself...

 
hoz:

By the way, yes... I've already added the full code of this function above:

I figured it would be more logical not to write it this way:

and then set the loop like this:

Right? It's just that the counter of the number of positions from zero is not perceived as such... It's not logical and, therefore, there is no reason to confuse ourselves...

You have to go to zero, not one, to search for orders.

for(i=total; i>=0; i--)

A pro once explained to me that we are searching an array of orders, and it's better to start the search with a higher number. In the array, the first element has index 0 (zero), so we shouldn't reach 1 and this is also the reason why we should go to OrdersTotal() - 1, instead of simple OrdersTotal().

I have the order search done this way:

for (int i=OrdersTotal()-1; i>=0; i--)
 
paladin80:

You have to go all the way down to zero, not one, in order to search for orders.

A pro once explained to me that it's an array of orders and then yes, it's better to start with a bigger digit. In the array, the first element has an index of 0 (zero), so not to 1 and also for this reason we need OrdersTotal() - 1, not just OrdersTotal().

It is very interesting. And the first thing I did was to open the textbook and try to find the answer there. And thenhttps://book.mql4.com/ru/trading/ordermodify saw how the tutorial made the overflow:

 for(int i=1; i<=OrdersTotal(); i++)          // Цикл перебора ордер

This is the factor that misled me...

 
hoz:

Very interesting. And the first thing I did was to open the textbook and try to find the answer there. And thenhttps://book.mql4.com/ru/trading/ordermodify I saw how the textbook was overdone:

That's the factor that misled me...


Didn't you notice the next line?

   for(int i=1; i<=OrdersTotal(); i++)          // Цикл перебора ордер     
   {      
     if (OrderSelect(i-1,SELECT_BY_POS)==true) // Если есть следующий
//
//Тут компенсируется отсутствие нуля с i-1
 
borilunad:


Didn't you notice the next line?

No. But somehow it is crooked to write it. I don't want to criticise the textbook, but... it's much more adequate to count from 0 rather than -1. Otherwise you could have already started from -30...

Aspaladin80 pointed out above, from 0 the value of arrays would be more adequate than from -N.

 
hoz:

No. But somehow it is crooked to write it. I don't want to criticise the textbook, but... it's much more adequate to count from 0 rather than -1. Otherwise we could have already started from -30...

Aspaladin80 pointed out above, from 0 the value of arrays would be the most adequate, if from -N.


Now think logically!

These variants are identical, because if i = 0, we need to look from 1 to OrderTotal(), i.e. from 0+1 to OrderTotal() - 1+1 (+1, because i++ is at the end of the for statement) The same in the tutorial, only written from 1 to OrderTotal() and, in order not to count from 2 to OrderTotal()+1, inthe OrderSelect function the textbook author added -1 to i. Got it?

By the way, as many programmers as there are, there are almost as many variants. Everyone has his own vision, his own handwriting!

 
hoz:

No. But somehow it is crooked to write it. I don't want to criticise the textbook, but... it's much more adequate to count from 0 rather than -1. Otherwise you could have already started from -30...

Aspaladin80 pointed out above, 0 would be more adequate for arrays than -N.

If you are interested in the search of orders, I can suggest the following scheme:

if (OrdersTotal()>0)
{  for (int i=OrdersTotal()-1; i>=0; i--)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) // ордер выбирается среди открытых
                                                    // и отложенных ордеров
      {  if (OrderSymbol()!=Symbol()) continue;     // если не наш символ, то уходим
         if (OrderMagicNumber()!=555) continue;     // если не наш магик номер, то уходим
                                                    // можно поставить любые другие фильтры
         // ... ваши вычисления
      }
   }
}
 
hoz:

No. But somehow it is crooked to write it. I don't want to criticise the textbook, but... it's much more adequate to count from 0 rather than -1. Otherwise you could have already started from -30...

Aspaladin80 pointed out above, 0 would be the most appropriate way to read arrays, as opposed to -N.

I have the same point of view, the tutorial was written by a specific person, this is his view of things (organization of order loop), and there are ALWAYS many correct solutions. There are right views, and there are right and elegant at the same time... :)
 
borilunad:


And now think, including logic!

These variants are identical, because if i = 0, we try from 1 to OrderTotal(), i.e. from 0+1 to OrderTotal() - 1+1 (+1, because i++ is at the end of the for statement) And the same is in the textbook, only from 1 to OrderTotal() and in order not to count from 2 to OrderTotal()+1, we add -1 to i in OrderSelect function. Got it?

Of course, I understand. But I haven't seen orders analyzed in such a way before:

if (OrderSelect(i-1,SELECT_BY_POS)==true)

usually just without ==true... I even liked this point. Although, it is interesting, I had never encountered such a method in other EAs. I understand the logic, but still.