Handling partial closes of orders and finding out when a trade has been fully closed?

 

Hello,

My broker (OANDA) does not allow hedging. Given this fact, consider this code fragment:

int ticket;

OnInit() {
ticket = OrderSend(Symbol(),OP_BUY,0.02,Ask,3,0,0,"the long trade",0,0,Blue);
}

OnTick() {
OrderSelect(ticket,SELECT_BY_TICKET);
OrderPrint();
}

This will continuously print the trade, for example: 0.02 lots long in EURUSD, OrderCloseTime=0

Now go and place a sell-stop manually in the MetaTrader client. Say, sell-stop 0.01 lots at a price just below the order entry price of the current trade.

If this sell-stop order is triggered, the OrderPrint() will reflect this and begin printing: 0.01 lots long in EURUSD, OrderCloseTime=123098420384

But look. The OrderCloseTime value is suddenly not 0 even though the trade is only partially closed. Worse, if I repeat the above and place a sell-stop for the remaining 0.01 lots, the trade is effectively closed but there seems to be no way to determine this from the OrderPrint() output: The OrderLots() value continues to return 0.01 even though that final 0.01 lot have also been sold, and the OrderCloseTime() outputs the same value as before.

How can I determine programmatically if a long trade has been "fully closed" by one or two or perhaps even a dozen stop-trades in the opposite direction?

-Michael

 
mberg2007: How can I determine programmatically if a long trade has been "fully closed" by one or two or perhaps even a dozen stop-trades in the opposite direction?

Question makes no sense. If the OrderCloseTime is zero it's open else it's closed. There are no stop-trades. You do NOT close a trade by opening one in the opposite direction, that's a NYSE model not the mq4 model.
Don't double post

 

I don't know if it works for all brokers.

When part of the position is closed, you are still checking the original ticket number and that has closed

The broker will close the first ticket and then open a new ticket with the remainder

Say you have 1 lot trade, ticket number 1.

you close 0.2 lots.

Ticket number 1 is now closed and a new ticket is opened for the remaining 0.8 lots.

With my broker, I will find in the closed order's comment something like "to #7"

So get the ticket number from the comment and then use that in your OrderSelect.

 
A lot of discussion over here about this issue
 
GumRai:

I don't know if it works for all brokers.

When part of the position is closed, you are still checking the original ticket number and that has closed

The broker will close the first ticket and then open a new ticket with the remainder

Say you have 1 lot trade, ticket number 1.

you close 0.2 lots.

Ticket number 1 is now closed and a new ticket is opened for the remaining 0.8 lots.

With my broker, I will find in the closed order's comment something like "to #7"

So get the ticket number from the comment and then use that in your OrderSelect.


Thanks a lot! This is brilliant, I did actually see those comments "to #x" and I wondered about what that meant. Your explanation makes perfect sense - this was precisely what I needed! :-)

-Michael

Reason: