Replace
switch(type) { //Close pending orders case OP_BUYLIMIT : case OP_BUYSTOP : result = OrderDelete( OrderTicket() ); case OP_SELLLIMIT : case OP_SELLSTOP : result = OrderDelete( OrderTicket() ); }
to
if(type>1)result = OrderDelete( OrderTicket() );
Replace [...] to [...]
Fleshing out Roger's answer with a reason, for SanMiguel's benefit, in the code Roger is replacing OrderDelete() gets called twice if the order type is a buy-stop or a buy-limit. Processing "falls through" from one part of a switch statement to another unless there's a "break". For an example of a similar problem involving switch statements without breaks, see my comment timestamped 2009.03.26 21:56 in 'Evaluating Account Balance'
Fleshing out Roger's answer with a reason, for SanMiguel's benefit, in the code Roger is replacing OrderDelete() gets called twice if the order type is a buy-stop or a buy-limit. Processing "falls through" from one part of a switch statement to another unless there's a "break". For an example of a similar problem involving switch statements without breaks, see my comment timestamped 2009.03.26 21:56 in 'Evaluating Account Balance'
what about the sell-stop and sell-limit ? is that 1 as well?
If 1 of these pending order trades is triggered and hits its take profit, I want to cancel the other order.
If it instead, hits its stop loss, I want to keep the other order.
Would I use a flag for this in this code?
Does a pending order change from op_buylimit to op-Buy when triggered?
////////////////////////////////////////////////////////// //IT IS NOW after 0700GMT but before 2100GMT, monitor order count ////////////////////////////////////////////////////////// if (Hour()>=7 && Hour() <21) { int totalOrdrs = OrdersTotal(); if (totalOrdrs > 0) { for(i=totalOrdrs-1;i>=0;i--) //if no orders then it won't run through this again { OrderSelect(i, SELECT_BY_POS); type = OrderType(); result = false; if(type=OP_BUY || type==OP_SELL) // ie one of the orders has been triggered, now we delete the other one. { for(i=totalOrdrs-1;i>=0;i--) //if no orders then it won't run through this again { OrderSelect(i, SELECT_BY_POS); type = OrderType(); result = false; if(type>1)result = OrderDelete( OrderTicket() ); if(result == false) { Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() ); Sleep(3000); } } } } } } }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I am getting a lot of these errors in my code:
Alert: Order 153 failed to close. Error:4108
Any ideas why?