Do OrderSend() and OrderClose() automatically select the current ticket?

 

If I make a trade using OrderSend() or OrderClose() then want to look at the number of lots or execution price on the ticket, do I need to make a further call to OrderSelect to get this ticket, or does this happen automatically?

Eg if I call OrderSend() then OrderMagicNumber(), is the magic number returned necessarily that of the order I have just sent (assuming successful)? Or, do I need to go OrderSend(), OrderSelect() then OrderMagicNumber()?


Also, a question on threading. If I call OrderSend() or OrderClose() for a market order, does it block until the execution at the broker is complete, or do I need to go back and look at it later?

 
stewart:

If I make a trade using OrderSend() or OrderClose() then want to look at the number of lots or execution price on the ticket, do I need to make a further call to OrderSelect to get this ticket, or does this happen automatically?

Also, a question on threading. If I call OrderSend() or OrderClose() for a market order, does it block until the execution at the broker is complete, or do I need to go back and look at it later?

  1. You need to call OrderSelect ALWAYS before using OrderTicket(), OrderOpenPrice(), etc.
  2. OrderSend returns the ticket number, it has to block to return it to you. OrderClose returns a bool, it has to block to return it to you. What are Function return values ? How do I use them ? - MQL4 forum
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){                                             OptInitialization();
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    if (ticket < 0)
       Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET))
       Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
       Alert("OrderModify failed: ", GetLastError());
     */
 

You should always use OrderSelect() before examining any 'current' ticket values.

I believe MT4 is essentially single threaded but with some trickery included to make it look like it's multi threaded for certain operations. I usually execute trade functions in a loop to retry the operation until it either succeeds or returns a failure (see GetLastError, ErrorCode, TradeContextBusy).

Also bear in mind that the order of trades (open or history) must be treated as arbitary. You can't rely on any specific order when iterating through all trades to check values or whatever...

Regards, Paul.

Ahh, WH beat me to it...

 
Thanks guys. This is helpful. I'll call OrderSelect() first and bear in bind that it will block execution while the trade is going through.
Reason: