what does the OrderCloseBy function realy do??

 
Am confused with this  OrderCloseBy function, i thought it only closes one order by another but it is closing both orders at once.
 
Byakatonda Denis: Am confused with this  OrderCloseBy function, i thought it only closes one order by another but it is closing both orders at once.

That is correct. It closes two opposite orders (one completely and the other either completely or partially):

Forum on trading, automated trading systems and testing trading strategies

OrderCloseBy Question

Ahmad Mehdiyev, 2014.03.22 12:13

1. You cannot open opposite positions at the same time if your broker is in US. That is true. With other brokers it is possible.

2. The way you get opposite positions (if they are allowed by your broker) is quite simple :). You buy and then sell, or vice versa.

3. The difference in lot size or the time at which each order was opened does not matter as far as OrderCloseBy is concerned.

4. OrderCloseBy is ment to substitute the use of OrderSend, so submitting OrderSend is not required. Simpy use "Close By" or "Multiple Close By" from within the Order Entry window, or use a script if you have one.

5. You do save a spread. But once! Here how it works. When you buy you pay a spread. When you sell you pay a spread. If you close them separately you loose both of those spreads (well they are in your P&L already), but if you do OrderCloseBy you are able to get one of those spreads back since it matches the closing price with opening price of the second trade. Simply put it assumes that when you opened the opposite trade you closed out the first one at the price at the opening price of the matching order.

They way I use OrderCloseBy is when I have a multiple one-sided positions and need to close them all quick I just lock the complete position with one trade, and then OrderCloseBy them with that new matching order. Obviously if you don't have an ability to lock the position you must close the initial multiple positions one-by-one which in a fast market could be quite damaging.

Hope this helps, and I did not simply repeat the things you know already.


Forum on trading, automated trading systems and testing trading strategies

what is your design/strategy for 1 or more order close failures (retries exhausted) on 100% unattended multisym EA ?

JC, 2010.02.12 11:46

The following brought to mind by a discussion on hedging which gordon and I have been participating in...

If you are closing multiple orders then - provided your broker supports it - I'd consider placing an opposing order for the size of your total position, and then repeatedly pairing things off. In other words, if you've got 5 buy orders for 0.2 lots, you place a sell order for 1 lot. And then repeatedly use OrderCloseBy().

This isn't pretty to do, because OrderCloseBy() keeps spawning new ticket numbers. However, as far as I'm concerned, it reduces the execution risk in MT4. If you're closing 5 orders, then there's the potential for the price to move quite strongly while those 5 separate requests are being handled one after the other. And there's 5 times the chance of encountering something like error #128. Of course, the price could move in your favour rather than against you, but personally I don't like the uncertainty. If you go down the pairing-and-offsetting route then your position is locked from the moment the closing order goes in (except for the effect of variations in the spread, but that's less significant than the risk from the price moving).

OrderCloseBy Question
OrderCloseBy Question
  • 2014.03.22
  • www.mql5.com
I've read several threads re: OrderCloseBy and I'm sorry to be thick about this but I still don't get it...
 
Byakatonda Denis:   OrderCloseBy function, i thought it only closes one order by another but it is closing both orders at once.

It closes both, but you pay only one spread.

 

To make a better explanation, the ClosyBy and multiple closeby commands instruct the broker server to reconcile two offsetting trades (or multiple) as one closed trade. Much like options trading where you Buy to Open -> Sell to Close, closeby treats forex orders in the same fashion.

For example, if you have long 1 lot XXXUSD @ 1.00000 (open price) and 0.5 short XXXUSD @ 1.00025 (open price) -- your net-open-position is 0.5 long XXXUSD, and your net closed position is 0.5 XXXUSD 

ticks_gained = 1.00025 - 1.00000
profit = ticks_gained * tick_value

When you use Closeby the broker server treats (reconciles) this transaction a one single trade instead of two separate trades, thus, you won't pay double commissions or multiple spreads.


Example summary: You opened long 1 lot long @ (ask = 1.00000) and you took partial profit by closing 0.5 lot @ (bid = 1.00025). You use closeby to reconcile the the two separate orders as one single transaction. 

Reason: