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)); |
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...
Ahh I'm an idiot, thanks I figured this out
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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?