OrderClose is not working with some broker

Back to topics list To post a new topic, please log in or register
avatar
4
bmhassene1 2016.09.16 22:17
 

Hi guys,

 

I'm wondering if there is a problem with this function because it is working on some broker and other not. I tried the simpliest code to check and found out that some broker like instaforex it is not working

   sellticket=OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,0,"Sell Order placed",MagicNumber,0,Green);
   OrderSelect(sellticket, SELECT_BY_TICKET);
   Sleep(4000);
   OrderClose (sellticket, 0.01, Bid, 0, Red);

 

avatar
735
honest_knave 2016.09.17 00:19 #
 

Check your return values and trade server return codes.

You are trying to close a SELL at the Bid price, but sell orders are closed at the Ask. Best to use OrderClosePrice() instead. 

avatar
Moderator
3206
angevoyageur 2016.09.17 11:44 #
 
bmhassene1:

Hi guys,

 

I'm wondering if there is a problem with this function because it is working on some broker and other not. I tried the simpliest code to check and found out that some broker like instaforex it is not working

 

A sell is closed at Ask price, not Bid.
avatar
1316
jjc 2016.09.19 09:42 #
 
bmhassene1:

working on some broker and other not 

For completeness... some brokers use the price which you provide for OrderSend() and OrderClose(); other brokers ignore it and simply execute the trade at the current market price regardless of the price you specify. (These are referred to in MT4's UI as "instant execution" and "market execution", though this does not necessarily reflect the broker's onward liquidity with any LP which they use.)

For example, the following OrderSend() and OrderClose() will work with some brokers but not with others:

OrderSend(Symbol(), OP_BUY, 0.01, 123456789 /* Nonsense price */, 0, 0, 0, "");
OrderClose(ticket, 0.01, 123456789 /* Nonsense price */, 0, Red);

What honest_knave is telling you indirectly is that you should, nevertheless, use the correct current price in OrderSend() and OrderClose() so that your EA works with both types of broker. And, for closes of a sell position, this should be the ask price rather than the bid price.

There is also a further problem in your code. The variables Bid and Ask do not update while an EA or script is executing its OnTick()/OnStart(). Therefore, if you do a Sleep (or if other actions such as order placement take a while to execute), the values of Bid and Ask can become out of date during the execution of the EA/script.

You should either force an update of these values using RefreshRates(), or you should something such as MarketInfo(Symbol(), MODE_BID) instead of Bid. For example:

// The variables Bid and Ask do not update while an EA or script is running. Therefore...
Sleep(5000);
Print("Value of bid without an update is... " , Bid);

RefreshRates();
Print("Value of bid after forcing an update is... " , Bid);


Back to topics list  

To add comments, please log in or register