Use Orderprofit () to check the profitability of the last order then you can use it as a criteria for an OrderDelete()
If the 2 orders are OP_BUYSTOP and OP_SELLSTOP.
When they are triggered, do they change to OP_BUY and OP_SELL or do they remain as stops?
If it changes to OP_BUY, then I can simply delete any remaining OP_SELLSTOP orders and vice versa.
I am trying the following but getting order failed to close errors, possibly because it is trying to delete open orders?
////////////////////////////////////////////////////////// //IT IS NOW after 0700GMT but before 2100GMT, monitor order count ////////////////////////////////////////////////////////// if (Hour()>=7 && Hour() <21) { total = OrdersTotal(); for(i=total-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) //one of the orders must have been triggered so delete the remaining pending order { int totaldel = OrdersTotal(); for(i=total-1;i>=0;i--) //if no orders then it won't run through this again { OrderSelect(i, SELECT_BY_POS); int typedel = OrderType(); bool resultdel = false; if(type>1)resultdel = OrderDelete( OrderTicket() ); if(resultdel == false) { Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() ); Sleep(3000); } } } if (type==OP_SELL) //one of the orders must have been triggered so delete the remaining pending order { totaldel = OrdersTotal(); for(i=total-1;i>=0;i--) //if no orders then it won't run through this again { OrderSelect(i, SELECT_BY_POS); typedel = OrderType(); resultdel = false; if(type>1)resultdel = OrderDelete( OrderTicket() ); if(resultdel == false) { Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() ); Sleep(3000); } } } } }
If the 2 orders are OP_BUYSTOP and OP_SELLSTOP.
When they are triggered, do they change to OP_BUY and OP_SELL or do they remain as stops?
If it changes to OP_BUY, then I can simply delete any remaining OP_SELLSTOP orders and vice versa.
a pending order becomes a market order if it is triggered so yes...they become OP_BUY and OP_SELL
a pending order becomes a market order if it is triggered so yes...they become OP_BUY and OP_SELL
This doesn't delete the existing pending order. Can't see why not:
////////////////////////////////////////////////////////// //IT IS NOW after 0700GMT but before 2100GMT, monitor order count ////////////////////////////////////////////////////////// if (Hour()>=7 && Hour() <21) { total = OrdersTotal(); for(i=total-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 && actionedflag == 0) //the buy order has been triggered so close the pending sell { int totaltodel = OrdersTotal(); for(int itodel=totaltodel-1;i>=0;i--) //if no orders then it won't run through this again { OrderSelect(itodel, SELECT_BY_POS); int typetodel = OrderType(); bool resulttodel = false; if(typetodel>1)resulttodel = OrderDelete( OrderTicket() ); } actionedflag = 1; } if (type==OP_SELL && actionedflag == 0) //the buy order has been triggered so close the pending sell { totaltodel = OrdersTotal(); for(itodel=totaltodel-1;i>=0;i--) //if no orders then it won't run through this again { OrderSelect(itodel, SELECT_BY_POS); typetodel = OrderType(); resulttodel = false; if(typetodel>1)resulttodel = OrderDelete( OrderTicket() ); } actionedflag = 1; } } }
my suggest: https://forum.mql4.com/38013
- Check your return codes and find out why.
What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- Add print statements before your if statements (include variable values) and find out why.
- 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 have 2 pending orders 1 BUY, 1 SELL.
If 1 of these 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.