if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_BID), Slippage ) )
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
if(OrderType()==OP_SELL)
{
if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_ASK), Slippage ) )
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
if(OrderType()==OP_SELL)
{
if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_ASK), Slippage ) )
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
MODE_BID
and
MODE_ASK
For use with MarketInfo()
Marco vd Heijden:
if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_BID), Slippage ) )
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
if(OrderType()==OP_SELL)
{
if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_ASK), Slippage ) )
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
if(OrderType()==OP_SELL)
{
if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_ASK), Slippage ) )
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
MODE_BID
and
MODE_ASK
For use with MarketInfo()
Excellent, thanks!
Can't believe I missed that!
There is no need to test the type, just close it.
In the presence of multiple orders (one EA multiple charts, multiple EA's,
manual trading)
- You must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
- and check OrderSelect. 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
- You must RefreshRates after sleep and between multiple server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead.
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
I have written the function below to close my orders. I want to close only the orders for this symbol and magic number, and only either buy or sell. I've placed the order close within a loop that checks if orders are still open on this currency pair and continues to attempt the close until successful. I don't want it to fail and then stop trying. Does this look correct or am I doing anything wrong here?
The price part in the order close function seems to be giving me problems. In strategy tester the OrderClose() will only work if I use OrderClosePrice() for the closing price, otherwise I get error 138. But on a live account if I use OrderClosePrice() I get error 138, and it only works if I use the MODE_BID and MODE_ASK as shown below. Can anyone advise what is going on and what is the correct way to do it?
Thanks!
{
int TotalNumberOfOrders = OrdersTotal();
while(OpenOrdersThisPair(Symbol(),MagicNumber)>0)
{
for(int i=TotalNumberOfOrders; i>=0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if( OrderMagicNumber() == MN
&& OrderSymbol() == pair
&& ( OrderType() == Type ) )
{
if(OrderType()==OP_BUY)
{
if ( ! OrderClose( OrderTicket(), OrderLots(), MODE_BID, Slippage ) )
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
if(OrderType()==OP_SELL)
{
if ( ! OrderClose( OrderTicket(), OrderLots(), MODE_ASK, Slippage ) )
Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
}
}
if(OpenOrdersThisPair(Symbol(),MagicNumber)<=0) break;
}
}
}
int OpenOrdersThisPair(string pair, int MN)
{
int total=0;
for(int i=OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()== pair)
if(OrderMagicNumber()==MN)
total++;
}
return (total);
}