Array out of Range error

 

Hi, I'm trying to write an EA, first step is to compile a list of open orders that match the symbol that the EA is currently open on.

However, whenever I try to check the contents of an array index with

MessageBox(OrderArray[0][0]); 

I get an Array out of Range error.

Here is the relevant code:

int OrderDB = 0;

string OrderArray[0][4];
void populateOrdersDB()
  { 
  
  
   if(orderDB<OrdersTotal()){}

   else
     {

      for(int i=0; i<=OrdersTotal()-1; i++)
        {
         bool res = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if (res)
         {
            {
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY || OrderType()==OP_SELL)
                  {
                  string ticket = (string)OrderTicket();
                  string direction = OrderType();
                  string entryPrice  = OrderOpenPrice();
                  string lots = (string)OrderLots();
                  string entryTime = (string)OrderOpenTime();
                 
                  OrderArray[i,0] = ticket;                 
                  OrderArray[i,1] = direction;
                  OrderArray[i,2] = entryPrice;                   
                  OrderArray[i,3] = lots;
                  OrderArray[i,4] = entryTime;
                 
                  }
            }
         }
         else MessageBox("Could not select order! Error: "+string(GetLastError()));
        }
     }
  }
 
Sorry, I can't get the CODE tag ("SRC" button) working. I apologize in advance for the full code showing up.
 
string OrderArray[0][4];
  1. Don't declare the array a fixed size; that shouldn't even compile. use Array[][4].
  2. You don't resize the array before trying to insert into it, so of course you get out of range.
  3. You need to keep the array size separate from the variable i because you aren't storing all orders.
  4. You are storing different things, don't use a common datatype (strings) use a structure
    struct Data{ int ticket, direction; double entryPrice, lots; datetime entryTime; }
    Data OrderArray[];
    :
       ArrayResize(OrderArray, count+1);
       OrderArray[count].ticket      = OrderTicket();
       OrderArray[count].direction   = OrderType();
       OrderArray[count].entryPrice  = OrderOpenPrice();
       OrderArray[count].lots        = OrderLots();
       OrderArray[count].entryTime   = OrderOpenTime();
       ++count; // Now you have one.
  5. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.
 
WHRoeder:
  1. Don't declare the array a fixed size; that shouldn't even compile. use Array[][4].
  2. You don't resize the array before trying to insert into it, so of course you get out of range.
  3. You need to keep the array size separate from the variable i because you aren't storing all orders.
  4. You are storing different things, don't use a common datatype (strings) use a structure

  5. Play video
    Please edit your post.
    For large amounts of code, attach it.

Thanks.


1. Yeah, I don't know why I had 0 in the first dimension. It's not like that in my code, but was like that in the OP.

2. I'm guessing you mean I *didn't* resize the array before trying to insert into it?

3. Not sure about this one, I guess it will make sense once I work on #2.

4. Don't know anything about structure, I'll look at your code and look into it.

5. As I mentioned, the SRC button doesn't work for me. I tried 3 different browsers and nothing. What happens in your video doesn't happen for me. This will be the 2nd time I apologize.


edit: I decided to try one last time and now the SRC feature is working.

 

Hey, just wanted to say I looked at the code shortly afterwards and it worked out great. I understood everything you meant, WHRoeder. The struct class is very similar to object-oriented programming, which I remember from my Java course in college. This has been a great addition to my arsenal.


Once again, thank you very much!

Reason: