- Array Out of Range Error
- Error 4108
- uninit reason 0;
for(int x=OrdersTotal();x>=0;x--) { if(OrderSelect(x,SELECT_BY_POS)==true) { if(OrderType()==OP_BUY){ cprice=MarketInfo(OrderSymbol(),MODE_BID);} else if(OrderType()==OP_SELL){ cprice=MarketInfo(OrderSymbol(),MODE_ASK);} if(!OrderClose(OrderTicket(),OrderLots(),cprice,clrNone)) { PrintFormat("Error closing order #%d. %s",OrderTicket(),ErrorDescription(GetLastError())); } } }
Rather than figuring out the close price, you can just use OrderClosePrice()
Hello All, I am trying to close all open orders one after the other. When I run the above I inevitably end up getting some or the other error. Can someone point out the potential fallacies?
//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void ClosePos() { //------------------------------------------------------------------------------------------------- int CloseLong=0,CloseShort=0; //------------------------------------------------------------------------------------------------- //Long for(int i=OrdersTotal()-1;i>=0;i--) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==EA_MagicNumber) if(OrderComment()==EA_Comment) if(OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) { Print("-------------------------------------------------------------------------------------------------"); CloseLong=OrderClose(OrderTicket(),OrderLots(),Bid,EA_Slippage,EA_ArrowColor); //If a error has occurred - Print the error description if(CloseLong==0) { Print("Long Order was not Closed..Error : "+ErrorDescription(GetLastError())); } //If a error has not occurred - Print the trade information if(CloseLong>0) { if(Bid<OrderOpenPrice()) { Print("Loss...",DoubleToStr(OrderOpenPrice()-Bid,Digits),".pips"); } if(Bid>OrderOpenPrice()) { Print("Profit..",DoubleToStr(Bid-OrderOpenPrice(),Digits),".pips"); } Print("Close Order Price...",DoubleToStr(Bid,Digits)); Print("Order Symbol...",Symbol()); Print("Long Order was closed successfully"); Print("-------------------------------------------------------------------------------------------------"); OrderCloseLine(); } } if(OrderType()==OP_SELL) { Print("-------------------------------------------------------------------------------------------------"); CloseShort=OrderClose(OrderTicket(),OrderLots(),Ask,EA_Slippage,EA_ArrowColor); if(CloseShort==0)//If a error has occurred - Print the error description { Print("Short Order was not Closed..Error : "+ErrorDescription(GetLastError())); } if(CloseShort>0)//If a error has not occurred - Print the trade information { if(Ask>OrderOpenPrice()) { Print("Loss...",DoubleToStr(Ask-OrderOpenPrice(),Digits),".pips"); } if(Ask<OrderOpenPrice()) { Print("Profit..",DoubleToStr(OrderOpenPrice()-Ask,Digits),".pips"); } Print("Close Order Price...",DoubleToStr(Ask,Digits)); Print("Order Symbol...",Symbol()); Print("Short Order was closed successfully"); Print("-------------------------------------------------------------------------------------------------"); OrderCloseLine(); } } } }
for( int count=OrdersTotal()-1;count>=0;count--) { OrderSelect(count,SELECT_BY_POS,MODE_TRADES); if (OrderType()==1) { vask=MarketInfo(OrderSymbol(),MODE_ASK); vol=OrderLots(); OrderClose(OrderTicket(),vol,vask,clrAliceBlue); Print("OrderClose error", GetLastError()); } else if(OrderType()==0) { vbid=MarketInfo(OrderSymbol(),MODE_BID); vol=OrderLots(); OrderClose(OrderTicket(),vol,vbid,clrAliceBlue); Print("OrderClose error", GetLastError()); } }
If this is part of an EA, you should - please - take care that you only close the orders your EA opened and not all. Parts like these produce hassle to other EAs when they´re not properly coded.
Same for ObjectsDeleteAll() ... an absolute no-go-function. Never use.
Doerk
You're passing a color for the slippage parameter. The 5th parameter should be color.
Also, you're adding extra code that you don't need (vask and vbid). Just use OrderClosePrice()
cryptex:
Thank you for the valuable comments above. I have implemented them. My new piece looks something like this. However I am getting OrderCLose error 3. Which pertains to invalid trade parameters. Not sure how to resolve it.
for( int count=OrdersTotal()-1;count>=0;count--) { OrderSelect(count,SELECT_BY_POS,MODE_TRADES); if (OrderType()==1) { vask=MarketInfo(OrderSymbol(),MODE_ASK); vol=OrderLots(); OrderClose(OrderTicket(),vol,vask,clrAliceBlue); Print("OrderClose error", GetLastError()); } else if(OrderType()==0) { vbid=MarketInfo(OrderSymbol(),MODE_BID); vol=OrderLots(); OrderClose(OrderTicket(),vol,vbid,clrAliceBlue); Print("OrderClose error", GetLastError()); } }
Same for ObjectsDeleteAll() ... an absolute no-go-function. Never use.
In the past, yes it was troublesome. But now you can finally specify a tag (prefix) which is great. One less UDF in most of my code.
int ObjectsDeleteAll( long chart_id, // chart ID const string prefix, // prefix in object name int sub_window=EMPTY, // window index int object_type=EMPTY // object type );
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use