array or loop problem?

 

Senario: 

in the current currency pair, open multiple positions, then manually close them one by one.

The following function recountorders() works fine and I can test with ArrayRange(sl,0) showing order counts of the current currecy pair correctly, and it turns zero when all orders closed.

int sl[][2];
......
void recountorders()
  {
   int poscount=0;
   for(int i=0; i<=OrdersTotal()-1; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderSymbol()!=Symbol())
         continue;
      if(OrderType()!=OP_SELL && OrderType()!=OP_BUY)
         continue;
      poscount++;
      ArrayResize(sl,poscount);
      sl[poscount-1][0]=OrderTicket();
      sl[poscount-1][1]=OrderStopLoss();
     }
  }

However, after I use this function in an indicator, ArrayRange(sl,0) is normal until the last order closed and ArrayRange(sl,0) keeps 1. Could anybody please help me figure it out?

double sl[][2];
int OnInit()
  {
   .....
   recountorders();

   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   bool found=false;
   int m;

   for(m=0; m<=ArrayRange(sl,0)-1; m++)
     {
      found=false;
      if(found==false && ArrayRange(sl,0)>=1)
        {
         alertbutton.Text("one order closed");
         alertbutton.Show();
         break;
        }
     }

   recountorders();
   Comment(m+"___"+ArrayRange(sl,0));

   return(rates_total);
  }

void recountorders() 
  {
   int poscount=0;
   for(int i=0; i<=OrdersTotal()-1; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderSymbol()!=Symbol())
         continue;
      if(OrderType()!=OP_SELL && OrderType()!=OP_BUY)
         continue;
      poscount++;
      ArrayResize(sl,poscount);
      sl[poscount-1][0]=OrderTicket();
      sl[poscount-1][1]=OrderStopLoss();
     }
  }

I've broken down my code removing as much irrelavent things as possible. Please just consider one order left, then it's closed. 

 
joshatt the last order closed and ArrayRange(sl,0) keeps 1. Could anybody please help me figure it out?

Of course, it does, you never set the array to zero size.

Instead, at the start of OnCalculate compute your count, then just return the actual count and use that.