Download MetaTrader 5
To add comments, please log in or register
All MQL5.community events are grouped together. Have a look at the Wall!
Michael
19
Michael 2014.04.16 21:10 

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

whroeder1
13656
whroeder1 2014.04.16 21:28  
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

Keith Watford
Moderator
8017
Keith Watford 2014.04.17 04:48  

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.

qjol
3243
qjol 2014.04.17 11:44  
A lot of discussion over here about this issue
Michael
19
Michael 2014.04.18 16:55  
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

/
To add comments, please log in or register