(solved)array out of range? not price related, maybe about OrdersTotal

 

Please close this dumb question..... the last two lines, I should use poscount instead of i.

I'm working on this code, using a CButton, my purpose: when new position (not including pending order) added to the current symbol, or stoploss changed in any of open position of the current symbol, this CButton pops up showing some alert text. 

The following code works fine when I test with only one symbol. But then, after I open a position in another symbol, then open one more position in the current symbol, the CButton pops up showing new position added, and error occur: array out of range.

#property strict
#property indicator_chart_window
#include <Controls\Button.mqh>
CButton alertbutton;
double sl[][2];   //------- storing each open position of the current symbol's: ticketnumber and stoploss.
......
int OnInit()
  {
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true);

   alertbutton.Create(.....);
   .....
     
   getarray();

//---
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   alertbutton.Destroy(reason);
  }

int OnCalculate(....)
{
   bool found=false;

   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;

      found=false;
      for(int k=0; k<=ArrayRange(sl,0)-1; k++)
        {
         if(OrderTicket()==sl[k][0])
           {
            found=true;
            // stoploss changed
            if(OrderStopLoss() != sl[k][1])
              {
               alertbutton.Text(stoplosschange);
               alertbutton.Show();
               break;
              }
            // not changed
            else
               break;
           }
        }
      if(found==false)  //---- new position added
        {
         alertbutton.Text(newpos);
         alertbutton.Show();
        }
     }

   getarray();
......
   return(rates_total);
 }

void getarray()   //------- refresh the array of open position of the current symbol's: ticketnumber and stoploss
  {
   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[i][0]=OrderTicket();
      sl[i][1]=OrderStopLoss();
     }
  }

I thought I got it, now I'm totally lost. Could anybody please help?

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.