Error Closing 4107

 
void CloseAllOrders(int slippage, int magicNumber) {
int x;
  double trades[][4];
  int total=OrdersTotal();
  if(total>0)
  {
  ArrayResize(trades,total);
  int count=0;
  for(x=total-1;x>=0;x--)
     {
     if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
     if(magicNumber == OrderMagicNumber())
        {
        trades[count][0]=OrderTicket();
        trades[count][1]=OrderProfit()+OrderCommission()+OrderSwap();
        trades[count][2]=OrderLots();
        trades[count][3]=OrderType();
        count++;
        }
     }
     
  ArrayResize(trades,count);
  ArraySort(trades,WHOLE_ARRAY,0,MODE_ASCEND);
  x=0;
  while(x<count)
     {
        RefreshRates();
        double close_price=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS));
        if(trades[x][3]==OP_BUY)
           close_price=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS));
           
           //NormalizeDouble(close_price,Digits);
        if(!OrderClose((int)trades[x][0],trades[x][2],close_price,slippage,clrNONE))
           Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());
        x++;
     }
     }
}

I originally just had close_price as just Ask or Bid, but after a bit of research from this forum I changed them to what they are now. However I still get issues when closing 3 digit JPY pairs with the 4107 invalid price error. What am I doing wrong here?
 
You have no order selected, therefor you can not use any Trade Functions - MQL4 Reference
  while(x<count)
     {
        RefreshRates();
        double close_price=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS));
 
There is an OrderSelect in there, and I am using OrderSymbol because I need it so if I run the EA on GBPUSD, that it can close trades from other pairs if needed.
 
No there isn't. The OrderSelect is in the for loop, none in the while loop. You are trying to close ticket trades[x][0] which can be any pair.
 
void CloseAllOrders(int slippage, int magicNumber) {
int x;
  double trades[][5];
  int total=OrdersTotal();
  if(total>0)
  {
  ArrayResize(trades,total);
  int count=0;
  for(x=total-1;x>=0;x--)
     {
     if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
     if(magicNumber == OrderMagicNumber())
        {
        trades[count][0]=OrderTicket();
        trades[count][1]=OrderProfit()+OrderCommission()+OrderSwap();
        trades[count][2]=OrderLots();
        trades[count][3]=OrderType();
        trades[count][4]=OrderSymbol();
        count++;
        }
     }
     
  ArrayResize(trades,count);
  ArraySort(trades,WHOLE_ARRAY,0,MODE_ASCEND);
  x=0;
  while(x<count)
     {
        RefreshRates();
        double close_price=NormalizeDouble(MarketInfo(trades[x][4],MODE_ASK),MarketInfo(trades[x][4],MODE_DIGITS));
        if(trades[x][3]==OP_BUY)
           close_price=NormalizeDouble(MarketInfo(trades[x][4],MODE_BID),MarketInfo(trades[x][4],MODE_DIGITS));
           
           //NormalizeDouble(close_price,Digits);
        if(!OrderClose((int)trades[x][0],trades[x][2],close_price,slippage,clrNONE))
           Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());
        x++;
     }
     }
}

Ok I added a spot in the trades array for the OrderSymbol, and used it in the close_price but did not help...
 

Try to check the values in your array.

Do you think the name of a symbol can be stored in a double value?

 
Ahh I'm an idiot, thanks I figured this out