Abnormal termination

 

Hello guys!

I have an issue regarding an EA I recently coded. I detected that sometimes it freezes the mt4 platform and I have to restart it or wait until it responses.

I manage to identify that the function that was causing the issue is this one:


void checkForMods()
  {
   uint last=OrdersHistoryTotal();
   double profit=0;
   ulong ticket;
   if(last>0)
     {
      for(uint i=last; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
           {
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
              {
               profit=OrderProfit();
               ticket=OrderTicket();
               break;
              }
           }
        }
     }

   if(totalOrders!=OrdersHistoryTotal())
     {
      //do something
      totalOrders=OrdersHistoryTotal();
     }
  }


The idea is to do something only if the last result is positive or negative and do not count the last result twice (thats why totalOrders). How can I manage this in a better way? And why does this causes an infinite loop and how do I avoid it/fix it?

I am assuming two things here. The first one is that this is the function that causes the problem (mainly because I commented the line in charge of executing the function and the problem dissapeard). The second one is that this gets into an infinite loop (why?!?).

The problem only occurs when I change an input in the EA while attached to a chart.


Any help?


Thank you in advance!!

:D

 
   uint last=OrdersHistoryTotal();
   ⋮
   if(last>0)
   ⋮
      for(uint i=last; i>=0; i--)
  1. No need for the if as the for loop will do nothing when the count is zero.

  2. If there are n items in history their positions are [0 … n-1]. Your first OrderSelect always fails.

  3. An unsigned int can never be less than zero. Your test is always true by definition. Either use a signed int, or compare count, not index:
    for(uint i=last; i>0;){ --i; // Count in the for, index inside the braces.

  4. Do not assume history has only closed orders.
    Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum
 
William Roeder:
  1. No need for the if as the for loop will do nothing when the count is zero.

  2. If there are n items in history their positions are [0 … n-1]. Your first OrderSelect always fails.

  3. An unsigned int can never be less than zero. Your test is always true by definition. Either use a signed int, or compare count, not index:

  4. Do not assume history has only closed orders.
    Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum

Hi William, thank you for your answers :)


1.- Sorry my ignorance, but why the the for loop does nothing when "last" is zero?

2.- Oh I guess in that case the for loop should start from last-1, right?

3. I do not understant the difference between 

for(uint i=last; i>0;){ --i; // Count in the for, index inside the braces.

and

for(uint i=last; i>=0; i--)

(besides the highlighted parts). I guess that the difference is that in the second one i starts from "last" and at the beginning of the loop it goes -1?

Is that equivalent to this?

for(uint i=last-1; i>0; i--)

4. How can I check if the order is closed or not? How can I be sure that the last order retrieved is the last order closed/deleted/modified at that given time?


Thank you William!