Closing position in an array

 
Hi All, I am attempting to write a function that limits the number of open positions held by closing the least profitable trades, I have written the code as below but am having trouble with the PositionClose() function, is there something I am missing here?
CSymbolInfo    m_symbol;                     
CPositionInfo  m_position;                   
CTrade         m_trade;                      
CArray         m_array;


 int MaxPosCheck()
  {
  while(openpositions>=maxpospara)
   {
   openpositions = PositionsTotal();

   for(int i=openpositions-1; i>=0; i--)
    {
    if(m_position.SelectByIndex(i))
     {
     m_array() = m_position.Profit();
     }
    }
   m_array.Sort(0);
   m_trade.PositionClose(m_array(openpositions-1));
   }
   return 0;
  }
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Reason: