I have a buy order open and closed again.
OrderNummer=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,NormalizeDouble(Bid-Sl*Point,Digits),NormalizeDouble(Bid+Tpf*Point,Digits),"Buy",123,0,Blue);
if(Bars > barsCount){
if(OrdersTotal() > 0){
for(int c = 0; c < OrdersTotal(); c++){
OrderSelect(c, SELECT_BY_POS);
if(OrderType() == OP_BUY){
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);
}else if(OrderType() == OP_SELL){
Order
The crazy is with a BUY order everything works only with SELL order comes the error
Hi, we can read your code better, and help, if you put your code to SRC ^̮^
- When you post code please use the SRC button !
-
OrderNummer=OrderSend(Symbol(),OP_SELL,0.1,Ask,3,0,0,"SELL",123,0,Blue);
You buy at the Ask and sell at the Bid.- Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
- Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
- The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)
-
OrderNummer=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,NormalizeDouble(Bid-Sl*Point,Digits),NormalizeDouble(Bid+Tpf*Point,Digits),"Buy",123,0,Blue);
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong- SL/TP (stops) need to be normalized to tick size (not Point.) (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 and MetaTrader 4 - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 and MetaTrader 4 - MQL4 programming forum
- Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 and MetaTrader 4 - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 and MetaTrader 4 - MQL4 programming forum
- Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
Code breaks
on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and
metals. Compute what a PIP is and use it, not points.
if(Bars > barsCount){
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
if(OrdersTotal() > 0){ for(int c = 0; c < OrdersTotal(); c++){ OrderSelect(c, SELECT_BY_POS); if(OrderType() == OP_BUY){ OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);
The if is unnecessary, no orders the loop does nothing.-
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual
trading,) because while you are waiting for the current operation (closing,
deleting, modifying) to complete, any number of other operations on other orders
could have concurrently happened and changed the position indexing:
- For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
- For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
- and check OrderSelect.
- and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
- Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
- book.mql4.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have a buy order open and closed again.
Everything works
if(Bars > barsCount){
if(OrdersTotal() > 0){
for(int c = 0; c < OrdersTotal(); c++){
OrderSelect(c, SELECT_BY_POS);
if(OrderType() == OP_BUY){
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);
}else if(OrderType() == OP_SELL){
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
}
}
}
barsCount = Bars;
}
When I open a SELL order comes the error 130
OrderNummer=OrderSend(Symbol(),OP_SELL,0.1,Ask,3,0,0,"SELL",123,0,Blue);
Even if I write
The crazy is with a BUY order everything works only with SELL order comes the error