OrderClose do not close orders
maybe increase the slippage (if it's a pending order use OrderDelete())
Hi,
Thank you for your fast reaplay, I put slippage 100 and it is yet not closing the orders, they are regolar not pending.
any idea?
me.
O.K. I decided to be more aggressive and write this :
void closeIt() { bool result; while (OrdersTotal()>0 && result == true) { if(OrderSelect(TICKET,SELECT_BY_TICKET,MODE_TRADES)) { result = CloseTicket(OrderTicket(), 20); } } if(result==false) { Alert("Ticket number #" +OrderTicket()+ " didn''t close"); } } //############################################################### // // // //############################################################### bool CloseTicket(int ticket, int index) { if(cmd==OP_BUY) price=Bid;else price=Ask; if (index == 0) { result = false; } else if (!OrderClose(ticket,OrderLots(),price,10,CLR_NONE);) { int ErrNum = GetLastError(); if (ErrNum==135 || ErrNum==146) { Sleep(500); RefreshRates(); result = CloseTicket(ticket, index-1) } else { result = false } } else { result = true; } }
but poor me it's yet not closing the order, S.O.S. ANY HELP WILL DO !!!
Thank you all.
me.
what error u get ?
I fix the code to look like this :
void closeIt() { bool result; while (OrdersTotal()>0 && result == true) { if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) { result = CloseTicket(OrderTicket(), 20); } } if(result==false) { Alert("Ticket number #" +OrderTicket()+ " didn''t close"); } } //############################################################### // // // //############################################################### bool CloseTicket(int ticket, int index) { double price=0; int result=0; OrderSelect(ticket, SELECT_BY_TICKET); if(OrderType()==OP_BUY) price=Bid;else price=Ask; if (index == 0) { result = false; } else if (!OrderClose(ticket,OrderLots(),price,10,CLR_NONE)) { int ErrNum = GetLastError(); Alert("ErrNum : "+ErrNum); if (ErrNum==135 || ErrNum==146) { Sleep(500); RefreshRates(); result = CloseTicket(ticket, index-1); } else { result = false; } } else { result = true; } }
but no good so far, the error is ticket #0 didnt close, I dot understand, ticket 0 ???
//+-----------------------------------------------------------------| //| WARNING: Use At Your Own Risk! | //| | //| [Script] Close all open orders | //| Copyright © 2006, Ruby Salton, www.TornadoFX.co.il | //+-----------------------------------------------------------------+ #property copyright "Copyright © 2006, Ruby Salton" #property link "www.TornadoFX.co.il" #define SLIPPAGE 5 #define NO_ERROR 1 #define AT_LEAST_ONE_FAILED 2 int start() { while (CloseAll() == AT_LEAST_ONE_FAILED) { Sleep(1000); Alert("Order close failed - retrying error#: ", GetLastError()); } } int CloseAll() { bool rv = NO_ERROR; int numOfOrders = OrdersTotal(); int FirstOrderType = 0; for (int index = 0; index < OrdersTotal(); index++) { OrderSelect(index, SELECT_BY_POS, MODE_TRADES); { FirstOrderType = OrderType(); break; } } for(index = numOfOrders - 1; index >= 0; index--) { OrderSelect(index, SELECT_BY_POS, MODE_TRADES); switch (OrderType()) { case OP_BUY: if (!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), SLIPPAGE, Red)) rv = AT_LEAST_ONE_FAILED; break; case OP_SELL: if (!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), SLIPPAGE, Red)) rv = AT_LEAST_ONE_FAILED; break; case OP_BUYLIMIT: case OP_SELLLIMIT: case OP_BUYSTOP: case OP_SELLSTOP: if (!OrderDelete(OrderTicket())) rv = AT_LEAST_ONE_FAILED; break; } } return(rv); }
Guys,
Don't make it so complicated :-
while(OrdersTotal()>0){ OrderSelect(0,SELECT_BY_POS); if(OrderType()<2)OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),SLIPPAGE,Red); else OrderDelete(OrderTicket()); }
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ // && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() ){ // and symbol if(OrderType()<2){ if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),SLIPPAGE,Red)) Alert("OrderClose(",OrderTicket(),") failed: ",GetLastError()); } else if (!OrderDelete(OrderTicket())) Alert("OrderDelete(",OrderTicket(),") failed: ",GetLastError()); RefreshRates(); // Required for multiple orders on a real account. }
Thank you for your help your code is working fine.
me.
Hi there,
I need to write a script that will close all the open orders when drop on a churt, I try to use the buildin script that comes with the metatrader but it is not closing the orders, so I wrote this code :
but itdoesnt do the work to, so maybe I loosing something I dot know,
any help will be very appreciated,
Thanks,
me.