OrderCloseBy

 

  if(iRSI(NULL,0,14,PRICE_CLOSE,0)>75)
    {
     OrderCloseBy(order_id,opposite_id);
     return(0);
    }

From the book "If two market orders are opened for one symbol simultaneously, one of them being Buy and another one being Sell, they can be closed in one of two ways: you can close them consecutively, one by one, using OrderClose(); or you can close one of them by the other one using OrderCloseBy(). In terms of saving money, the second way is more preferable, because you will save one spread in closing orders by each other."

 

Ordercloseby() have been hunting me for a while now. I haven't had any practical examples to use it in but I'm thinking it's a nice way to save some pips. Would you need to close both orders (the buy and sell) to save the one pip? Can someone who is familiar with this function comment on it? Also, if anyone aware of a similar code or technique to save some $$ please provide as well. Thanks.

 

 

ubzen:

Ordercloseby() have been hunting me for a while now. [...] Can someone who is familiar with this function comment on it?

Perhaps the most important point is that it doesn't work with all brokers. And, AFAIK, there's no way of detecting in advance from MQ4 whether the broker allows use of OrderCloseBy()
 

Thanks JJC, I didn't know that. Well here's where the good ole telephone comes in handy :) 

 
ubzen:

Thanks JJC, I didn't know that. Well here's where the good ole telephone comes in handy :)

If the broker does support it, then the user interface will provide "close by" and "multiple close by" options (i.e. if you have open orders in opposite directions, and you click on Close, the "Type" dropdown will include these options). But there's no way that I know of for an EA to detect by itself whether OrderCloseBy() will work.
 
Cool, I've got MultipleCloseBy and CloseBy. I just tested it out and it closed both orders. Appears like it stole the profit for the other and call it a hedge lol. I guess what I'm asking is what is it doing? How does it work?
 
ubzen:
[...] I guess what I'm asking is what is it doing?

It closes both orders while only one spread is 'payed' (one of the orders is closed with zero spread...).

How does it work?

It's a type of operation the platform supports... It's actually common to many other trading platforms.

 

Doesn't sound so attractive anymore. If I wanna close order A and save 1-spread. Opening order B which costs 1-spread. Then closing both orders simultaneously at a cost of only 1-spread instead of 2 doesn't sound like saving.

 

Now that I think about it, seem this technique was designed for someone who's hedging A & B from the very start. If this person is playing for interest rates, order A and B would be on different instruments.

 

Is there any good reason for someone to hedge the same instrument, starting at the same time? 

 
ubzen:

Doesn't sound so attractive anymore. If I wanna close order A and save 1-spread. Opening order B which costs 1-spread. Then closing both orders simultaneously at a cost of only 1-spread instead of 2 doesn't sound like saving.

Ya, it's only if u already have 2 opposite orders. There is one exception - in case u have many orders of same type and u would like to close them all - it makes sense to open one large opposite order with a lot size equal to the total lot size of the already opened orders and then use CloseBy() to close all orders in a loop. This will protect you against fast price movements while closing all those orders (which might take seconds to minutes).


Is there any good reason for someone to hedge the same instrument, starting at the same time?

You are opening a hornet's nest there. There are a few heated debates on the subject of hedging... See this thread for example -> https://www.mql5.com/en/forum/117708.
 
ubzen:

Is there any good reason for someone to hedge the same instrument, starting at the same time?

FXCM have posted information about this (http://www.fxcm.co.uk/hedging.jsp), including a video (http://forex.acrobat.com/p82929619/).

However, you don't need the platform to support hedging in order to be able to trade this kind of strategy. It's just easier to manage (and easier to code) if the platform lets you have separate positions in opposite directions, rather than making you do the translation into a single order for your net position across the two sub-strategies. As gordon's said, it's a hornet nest, because lots of people don't seem to get the fact that it is still possible - but just more difficult and irritating - to run this kind of strategy if you can't place two separate opposing orders.

There is another reason for hedging, though it's not the good reason you asked for. If each order is treated as a separate position, instead of you simply having a single net position in an instrument at any given point in time, you can carry uncrystallised losses on your account - i.e. you can close winning trades, and leave losers running. Your account equity is unaffected by this, and remains the same in hedging and non-hedging scenarios, but your account balance is artificially inflated. Some people don't like no-hedging/MT5 because they're deluding themselves by concentrating on balance rather than equity, and no-hedging/MT5 thus suddenly makes their results appear a lot worse.

 
Thanks for your responses JJC and Gordon!
 

i tried it but its telling rong function


\