close the only one open order

 

Hy, beginner question,

I have an EA that closes open positions like this:

 for(i=OrdersTotal()-1; i>=0; i--){
        OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
        if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
        if(OrderType()==OP_BUY)OrderCloseZ(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);
        else 
        if(OrderType()==OP_SELL)OrderCloseZ(OrderTicket(),OrderLots(),Ask,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);
        else OrderDelete(OrderTicket());
}

 Please see attached file for OrderCloseZ() function; it is also used for opening orders.

I am unable to simplify the code like this: I want to open a single order and then close it when conditions are "true". How to code this without for() loop ? In fact I do not need it. It is a single order, but I do not know how to code it's opening , then finding  it an code it's close.

Thanks in advance for suggestions! 

Files:
orderz.mqh  2 kb
 
tenlau: How to code this without for() loop ? In fact I do not need it.
  1. You don't? EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush/file) of ticket numbers required.
  2. If it's an open order, you don't need to check the type and use Bid/Ask. Just use OrderClosePrice() instead.
  3. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 
WHRoeder:
tenlau: How to code this without for() loop ? In fact I do not need it.
  1. You don't? EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush/file) of ticket numbers required.
  2. If it's an open order, you don't need to check the type and use Bid/Ask. Just use OrderClosePrice() instead.
  3. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

A - I wanted to avoid loosing time with for loop; since is only one order opened I thought is faster to close it....But I don't know how to find it....

B - About "persistent storage (GV+flush/file)" please give some more details...

 
  1. Faster is irrelevant. The platform completes in microseconds (0.000 0xx.) The network transmits in tens-thousands of milliseconds (0.030 xxx.) The server can take minutes (during news) to close the order (120.xxx xxx.)
  2. You have to find the ticket number. What part of "OrderSelect loop or persistent storage" is unclear "to find it?"
  3. What part of Global Variables of the Terminal - MQL4 Reference including using GlobalVariablesFlush or using File Functions - MQL4 Reference is unclear?
 
WHRoeder:
  1. Faster is irrelevant. The platform completes in microseconds (0.000 0xx.) The network transmits in tens-thousands of milliseconds (0.030 xxx.) The server can take minutes (during news) to close the order (120.xxx xxx.)
  2. You have to find the ticket number. What part of "OrderSelect loop or persistent storage" is unclear "to find it?"
  3. What part of Global Variables of the Terminal - MQL4 Reference including using GlobalVariablesFlush or using File Functions - MQL4 Reference is unclear?

! and 3 is clear.

For 2: I open an order with:

int OrderSend (string symbol, int cmd, double volume, double price, int slippage, double stoploss,
double takeprofit, string comment=NULL, int magic=1234, datetime expiration=0, color arrow_color=CLR_NONE)

 when I look for close it, it's ticket is 1234 ? (magic number). In other words magic=ticket number? If not: a- what are the differences between ticket and magic? b- how to find ticket of the open order without OrderSelect loop?

 

If you open a ticket (OrderSend) you get the ticket number (e.g. 115719094.) A ticket is not a magic number.

In the OrderSelect loop you can filter all orders to find the ticket with your magic number and pair.

Using OrdersTotal directly and/or no filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
If you already have the ticket number, you can just select it, but remember #1 (recovery.)
Reason: