What is the default value of order ticket? Is checking order ticket's value a proper way to confirm a market order is filled?

 

Hi everyone!

I am using CTrade library to open a long position at the market price then get the order ticket number.

The CTrade document says "Successful completion of the PositionOpen(...) method does not always mean successful execution of the trade operation.", so I want to check if the order is filled.

My question is : Is the default value(before filled) of an order ticket zero? If it is, I want to use the following code to confirm and get the order ticket number. Is it appropriate?

...
bool xResult = false;
trade.PositionOpen(Symbol(), ORDER_TYPE_BUY, Lot_vol, Price, 0, 0, NULL);
while(!xResult)
{
   if(trade.ResultOrder()!=0)
   {
      Order_ticket = trade.ResultOrder();
      xResult = true;
   }
}
...

 *Furthemore: Is the default value(before opened) of a position ticket also zero? I might use RequestPosition() to check if a new position is successfully opened and get the position ticket number too.


Thank you very much!!

Documentation on MQL5: Trade Functions / OrderGetTicket
Documentation on MQL5: Trade Functions / OrderGetTicket
  • www.mql5.com
Do not confuse current pending orders with positions, which are also displayed on the "Trade" tab of the "Toolbox" of the client terminal. An order is a request to conduct a transaction, while a position is a result of one or more deals. Function OrderGetTicket() copies data about an order into the program environment, and further calls of...
 

I use such a construction (it is in all my recent advisor codes in CodeBase).

      if(m_trade.Buy(long_lot,m_symbol.Name(),
                     m_symbol.Ask(),sl,tp)) // CTrade::Buy -> "true"
        {
         if(m_trade.ResultDeal()==0)
           {
            if(m_trade.ResultRetcode()==10009) // trade order went to the exchange
              {
               SPosition[index].waiting_transaction=true;
               SPosition[index].waiting_order_ticket=m_trade.ResultOrder();
              }
            else
              {
               SPosition[index].waiting_transaction=false;
               if(InpPrintLog)
                  Print(__FILE__," ",__FUNCTION__,", ERROR: ","#1 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
                        ", description of result: ",m_trade.ResultRetcodeDescription());
              }
            if(InpPrintLog)
               PrintResultTrade(m_trade,m_symbol);
           }
         else
           {
            if(m_trade.ResultRetcode()==10009)
              {
               SPosition[index].waiting_transaction=true;
               SPosition[index].waiting_order_ticket=m_trade.ResultOrder();
              }
            else
              {
               SPosition[index].waiting_transaction=false;
               if(InpPrintLog)
                  Print(__FILE__," ",__FUNCTION__,", OK: ","#2 Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
                        ", description of result: ",m_trade.ResultRetcodeDescription());
              }
            if(InpPrintLog)
               PrintResultTrade(m_trade,m_symbol);
           }
        }
      else
        {
         SPosition[index].waiting_transaction=false;
         if(InpPrintLog)
            Print(__FILE__," ",__FUNCTION__,", ERROR: ","#3 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
                  ", description of result: ",m_trade.ResultRetcodeDescription());
         if(InpPrintLog)
            PrintResultTrade(m_trade,m_symbol);
        }
 
yum1573: My question is : Is the default value(before filled) of an order ticket zero?

You shouldn't care. If the operation fails, what good is the ticket number? If the operation succeeds, get it.

 
Vladimir Karputov:

I use such a construction (it is in all my recent advisor codes in CodeBase).

Thank you for sharing.

I'm not sure how does "SPosition[index].waiting_transaction" work.

My understanding is that your method is to check the result code after sending a market order successfully. If the result code is 10009 then proceed, and if not, wait for a while then check again. Is my understanding correct?

 
William Roeder:

You shouldn't care. If the operation fails, what good is the ticket number? If the operation succeeds, get it.

Thank you for your reply.

You are right but my concern is not only when operation fails. In cases when the position is successfully  opened, if I try to fetch and store the ticket number right after the PositionOpen() function, it might be too quick that I can't get the correct result(*). So I want to make sure the trade operation is correctly placed then get information such as ticket number.

I would appreciate it if you have any advices/suggestions such as checking the result code.


* The CTrade document says "Successful completion of the PositionOpen(...) method does not always mean successful execution of the trade operation."
 
yum1573 :

Thank you for sharing.

I'm not sure how does "SPosition[index].waiting_transaction" work.

My understanding is that your method is to check the result code after sending a market order successfully. If the result code is 10009 then proceed, and if not, wait for a while then check again. Is my understanding correct?

Yes, right. The result in case of a successful order placement is expected in OnTradeTransaction.

 
Vladimir Karputov:

Yes, right. The result in case of a successful order placement is expected in OnTradeTransaction.

Thank you.

I will check the OnTrade approach.

Have a nice day:)

Reason: