Array out of range

 
Hi All, I have written the code below as an attempt to create a function that limits the number of open positions by closing either the oldest or least profitable depending on input settings, the code compiles however when I run it on backtest I get an error message saying array out of range on line 309, however I don't understand why that is, could someone please explain what might be causing this?
int MaxPosCheck()
  {
   double profitarray[][2];
  
   if(FIFO == true)
   {
    for(int i=0;i<openpositions-1;i++)
      {
      if(m_position.SelectByIndex(i))
        {         
         profitarray[i][0]=m_position.Ticket();
         profitarray[i][1]=m_position.Profit();
        }
      }
      
    ArraySort(profitarray);
    int sortedarraysize = ArraySize(profitarray);
    while(openpositions>maxpospara)
    {
    for(int i=0;i<sortedarraysize-1;i++)
        {
         int sortedarraysize = ArraySize(profitarray);
             m_trade.PositionClose(profitarray[0][0]);
             sortedarraysize = sortedarraysize-1;
             ArrayResize(profitarray,sortedarraysize,0);        
        }
     }
    }
    else
     {
     for(int i=0;i<openpositions-1;i++)
      {
      if(m_position.SelectByIndex(i))
        {         
         profitarray[i][1]=m_position.Ticket();
         profitarray[i][0]=m_position.Profit();
        }
      }
      
    ArraySort(profitarray);
    int sortedarraysize = ArraySize(profitarray);
     while(openpositions>maxpospara)
    {  
    for(int i=0;i<sortedarraysize-1;i++)
      {      
         int sortedarraysize = ArraySize(profitarray);
             m_trade.PositionClose(profitarray[0][1]);
             sortedarraysize = sortedarraysize-1;
             ArrayResize(profitarray,sortedarraysize,0);         
      }
     }  
    } 
   
   return 0;
  }
 

You have declared an dynamic array, which will be empty (size = 0).

 double profitarray[][2];

Yet you immediately begin to try assigning values to its elements ...

profitarray[i][0]=m_position.Ticket();
profitarray[i][1]=m_position.Profit();

So obviously it will give an array out of range error.

You will need to first set the size of the dynamic array accordingly, before populating it ...

ArrayResize

Sets the new size in the first dimension of the array

Documentation on MQL5: Array Functions / ArrayResize
Documentation on MQL5: Array Functions / ArrayResize
  • www.mql5.com
ArrayResize - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Many thanks Fernando, I have implemented this and cleared the array resize issue, now I am getting the following error during the running of the EA:

VirtualAlloc failed in large allocator size=2684354592

I am fairly new to MQL/C++ and haven't quite got to grips with the memory allocation side of things, is this due to array sizes or variable sizes?

 

The memory allocation problem has been solved, however the EA now just freezes when this function is called, does anyone know what could be causing this?

I get the following warning on compilation;

implicit conversion from 'number' to 'string'

I think this issue has something to do with the PositionClose function but I do not know what or how to fix this

 

Then show your new code and the the log output and identify the affected line numbers.

We cannot read your mind nor see your computer.

Reason: