Small question about OrderClose() order.

 

Hello freinds,

I have a tecnical quastion:

If I want to close an open trade, and I already know its: Ticket number, lot size and the correct price. Do

I have to preceed the ORDERCLOSE() with ORDERSELECT()?

Thank you all,

Y.

 

orderclose is enough if you know the ticket number.

 
willgart:

orderclose is enough if you know the ticket number.


Thank you
 
crossy:

Hello freinds,

I have a tecnical quastion:

If I want to close an open trade, and I already know its: Ticket number, lot size and the correct price.


If you use OrderClosePrice() you don't even have to distinguish between Buys and Sells.
 

If you have the ticket number, OrderClose() is enough.

If ALL you have is the ticket number, then you must check to see if the ticket is still open (or handle the error if it has already closed) OrderSelect() and test if OrderCloseTime() is still zero.

If you don't want to distinguish between Buys and Sells you can use OrderClosePrice() but then you must OrderSelect() before using it

 
crossy:

Hello freinds,

I have a tecnical quastion:

If I want to close an open trade, and I already know its: Ticket number, lot size and the correct price. Do

I have to preceed the ORDERCLOSE() with ORDERSELECT()?

Thank you all,

Y.

Use the following code:

// in case if position is BUY - Bid, if SELL - Ask
int ticket=12345678; // ticket's number
OrderClose(ticket, OrderLots(), Bid, 3, CLR_NONE);
 
paladin80:

Use the following code:



And don't forget the OrderSelect() if you want to use OrderLots() ;-)
 
RaptorUK:

And don't forget the OrderSelect() if you want to use OrderLots() ;-)

Hmm, in my EA I close positions always with OrderSelect()... and then OrderLots() inside of OrderClose(). I thought that OrderLots() works correctly without OrderSelect(). So, the correct code is:

// in case if position is BUY - Bid, if SELL - Ask
int ticket=12345678; // ticket's number
double my_lot=0,1:
OrderClose(ticket, my_lot, Bid, 3, CLR_NONE);
 
OrderLots() " Note: The order must be previously selected by the OrderSelect() function. "
 
paladin80:

Hmm, in my EA I close positions always with OrderSelect()... and then OrderLots() inside of OrderClose(). I thought that OrderLots() works correctly without OrderSelect(). So, the correct code is:



You are not handle a 5 digit situation if you simply use 3 for Slippage

Don't forget freezelevel if the price is almost at OrderStopLoss() or OrderTakeProfit() it can fail

Instead of Bid or Ask to close a trade you can simply use OrderClosePrice() see Comment of RaptorUK

in your example is also not checked if the trade is still open see comment WHRoeder also