- Experts: Profit Counter
- Experts: FMOneEA
- Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6.
Hello, Im starting with MQL4 language and I'm trying to put togerther my first simple breakout trading system. It works on breakout of range principle, so BUY stop on top of the price range and SELL stop on the bottom of the price range. But, I dont found effective solution how to delete the opposite STOP order when some order is activated. Can you help me? :-)
- www.mql5.com
Go through your list of orders (your chart/MN). Remember if you find an open order and the ticket number of any pending. Delete the pending if you found an open.
Yes that sound simple! I tried to put together code below. But it seems my logic is bad, it doesnt work :/.
for (p=OrdersTotal(); p>=0;p--) // go through all orders {if(OrderSelect(q, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY) // if we have active BUY order which belongs to our EA {for (m=OrdersTotal(); m>=0;m--) // start another counter, go through all orders {if(OrderSelect(q, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber()==MagicNumber && OrderType() == OP_SELLSTOP) //if we have SELLSTOP, which belongs to our EA {delete = OrderDelete(OrderTicket(), Yellow);}}}} // delete it.
Also Im curious, when ppl use operator For, why someone uses OrdersTotal()-1 and other group of people using it without -1?
I am very grateful for your advices, I don't programmed anything before I started with MQL :)
I find code made by Fernando Carreiro on this forum, which solves this problem. https://www.mql5.com/en/forum/159380
void CheckMarketDeletePending() { for( int i = OrdersTotal() - 1; i >= 0; i-- ) { if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ) { if( ( OrderMagicNumber() == MagicNumber ) && ( OrderSymbol() == _Symbol ) ) { if( ( OrderType() == OP_BUY ) || ( OrderType() == OP_SELL ) ) { DeletePendingOrders(); break; } } } } } void DeletePendingOrders() { for( int i = OrdersTotal() - 1; i >= 0; i-- ) { if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ) { if( ( OrderMagicNumber() == MagicNumber ) && ( OrderSymbol() == _Symbol ) ) { if( ( OrderType() != OP_BUY ) && ( OrderType() != OP_SELL ) ) { if( !OrderDelete( OrderTicket() ) ) { // Delete failed (need to deal with situation) // Check Error Codes } } } } } }
It seems my logic was right, but Im not exiting the For loop with "break;". Im not sure why its needed, I must study the MQL4 little bit more :-). Also, he is starting his For loop from OrdersTotal() -1 and not just OrdersTotal(), can someone please explain me, why?
If there are 4 orders, their positions are 0 … 3. OrderSelect(OrdersTotal()… will always fail.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use