void CloseAll(int t) { int cnt=OrdersTotal(); int ticket=0; double closeprice=0; double closelot=0; int ordertip=-1; for(int i=cnt-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) ticket=OrderTicket(); closeprice=OrderClosePrice(); closelot=OrderLots(); ordertip=OrderType(); if(OrderSymbol()==Symbol()&& (t==OrderMagicNumber()||ticket==t)) { if(ordertip==OP_BUY||ordertip==OP_SELL) close=OrderClose(ticket,closelot,closeprice,50,clrGreen); if(ordertip==OP_BUYSTOP||ordertip==OP_SELLSTOP) move=OrderDelete(ticket,clrRed); } if(OrderSymbol()==Symbol()) { if(ordertip==OP_BUYSTOP||ordertip==OP_SELLSTOP) move=OrderDelete(ticket,clrRed); } } }
You try to delete it twice.
William Roeder #:
You try to delete it twice.
You try to delete it twice.
I was using the code below but it doesn't close pending orders
this is why I added it
//======== void CloseAll(int t) { int cnt=OrdersTotal(); int ticket=0; double closeprice=0; double closelot=0; int ordertip=-1; for(int i=cnt-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) ticket=OrderTicket(); closeprice=OrderClosePrice(); closelot=OrderLots(); ordertip=OrderType(); if(OrderSymbol()==Symbol()&& (t==OrderMagicNumber()||ticket==t)) { if(ordertip==OP_BUY||ordertip==OP_SELL) close=OrderClose(ticket,closelot,closeprice,50,clrGreen); if(ordertip==OP_BUYSTOP||ordertip==OP_SELLSTOP) move=OrderDelete(ticket,clrRed); } } } //*********
This is untested, uncompiled code, to serve only as an example:
void CloseAll( string sSymbol, int nMagicNumber, int nTicket, int nSlippage ) { for( int i = OrdersTotal() - 1; i >= 0; i-- ) { if( OrderSelect( i, SELECT_BY_POS ) ) { int nSelected = OrderTicket(); if( ( nSelected == nTicket ) || ( OrderMagicNumber() == nMagicNumber && OrderSymbol() == sSymbol ) ) { ResetLastError(); switch( OrderType() ) { case OP_BUY: case OP_SELL: if( !OrderClose( nSelected, OrderLots(), OrderClosePrice(), nSlippage, clrGold ) ) PrintFormat( "Error %d closing position %d", _LastError, nSelected ); break; default: if( !OrderDelete( nSelected, clrYellow ) ) PrintFormat( "Error %d deleting order %d", _LastError, nSelected ); break; }; }; }; }; };
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello
I got " unknown ticket orderdelete funtion " error to close pending orders