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);
OrderClose (sellticket, 0.01, Bid, 0, Red);
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.
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...
Print("Value of bid without an update is... " , Bid);
Print("Value of bid after forcing an update is... " , Bid);
To add comments, please log in or register