Get ticket immediately after opening position - page 2

 
Samuel Manoel De Souza #:
No. You can get ResultOrder immediately after send the order. But the fact that you get a success code and the order ticket, doesn't mean that the positions is already available to select. That is why i told to use Sleep.

What he is saying is that you have other ways to handle the opening of positions, like you get the order, then later in OnTradeTransaction you can check if the order was filled, partially filled, canceled, rejected, etc. in the same way you can know the position affected by an order execution handling trade transactions events, especially the TRADE_TRANSACTION_DEAL_ADD.

Good morning,

Firstly, I'd like to thank everyone for their availability.

Returning to the topic at hand, when using the OnTradeTransaction function, is the code below sufficient to retrieve the ticket of the last opened position?

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {

   if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
     {
      if(trans.order_state == ORDER_STATE_STARTED)
        {
         if(trans.order_type == ORDER_TYPE_BUY)
            gblLastTicketBuy = trans.order;
         else
            gblLastTicketSell = trans.order;
        }
     }
  }

Additionally, with this code, could I eliminate the need for using Sleep?

Thank you in advance for the help.

 
Jeovane Reges #:

Good morning,

Firstly, I'd like to thank everyone for their availability.

Returning to the topic at hand, when using the OnTradeTransaction function, is the code below sufficient to retrieve the ticket of the last opened position?

Additionally, with this code, could I eliminate the need for using Sleep?

Thank you in advance for the help.

The code shown is retrieving the order ticket.

You need to distinguish between deal, order and position. Each has a ticket number, but they don't correlate. To get the matches between these three lists/tables, you need to use the position_id property.

A position can have multiple orders, and an order can have multiple deals. To match them, use the unique position_id, they are given for each element, be it deal, order or position.

As long as there is only an order, like a limit order, which has not resulted in an execution via a deal, you will also have no position_id.

Once an order comes to be executed, a deal is created/added, and a corresponding position is created. From there on forward, you have a position ticket and a position_id.

With this information you can match all three tables to find your details.
 
Dominik Egert #:
The code shown is retrieving the order ticket.

You need to distinguish between deal, order and position. Each has a ticket number, but they don't correlate. To get the matches between these three lists/tables, you need to use the position_id property.

A position can have multiple orders, and an order can have multiple deals. To match them, use the unique position_id, they are given for each element, be it deal, order or position.

As long as there is only an order, like a limit order, which has not resulted in an execution via a deal, you will also have no position_id.

Once an order comes to be executed, a deal is created/added, and a corresponding position is created. From there on forward, you have a position ticket and a position_id.

With this information you can match all three tables to find your details.

The position id is the same as the ticket of the order that opened the position. Check documentation. Thanks.

POSITION_IDENTIFIER

Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.

 

Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.

 

When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.

 
Since he is opening a position, not adding or closing partially, the order ticket will work on the same way.
 
Samuel Manoel De Souza #:
Since he is opening a position, not adding or closing partially, the order ticket will work on the same way.
I don't understand what you are saying...

Position_id (position identifier) has nothing to do with ticket numbers, they do not correlate at all, only on coincidence, maybe.

Only matching of positions, orders and deals can be performed via position id, there is no other way.

This is also confirmed by what you have shown from the docs...

So, either I completely do not understand what you are saying, or it is wrong.... not sure though.
 
Dominik Egert #:
I don't understand what you are saying...

Position_id (position identifier) has nothing to do with ticket numbers, they do not correlate at all, only on coincidence, maybe.

Only matching of positions, orders and deals can be performed via position id, there is no other way.

This is also confirmed by what you have shown from the docs...

So, either I completely do not understand what you are saying, or it is wrong.... not sure though.

Position identifier  corresponds to the ticket of an order used to open a position. It means if he does not have an open position and do send a market order, the ticket of this orders will be used as position identifier once it opens. So there is no point on  distinguish between deal, order and position, in this particular case where there is no open position.

 
Jeovane Reges #:

Good morning,

Firstly, I'd like to thank everyone for their availability.

Returning to the topic at hand, when using the OnTradeTransaction function, is the code below sufficient to retrieve the ticket of the last opened position?

Additionally, with this code, could I eliminate the need for using Sleep?

Thank you in advance for the help.

don't need check the state, since the deal has been added to history, you can just check if is a buy or sell and any other filter like symbol, magic number, if needed.

 
Samuel Manoel De Souza #:

Position identifier  corresponds to the ticket of an order used to open a position. It means if he does not have an open position and do send a market order, the ticket of this orders will be used as position identifier once it opens. So there is no point on  distinguish between deal, order and position, in this particular case where there is no open position.

I would need to check this, but "corresponds" is not "equals"...

Even though it mitght be understood in such way,I am confident, the position_id is not necessarily equal to ticket number.

I already thought, you would be referring to that expression.

If I remember right, in strategy tester the identifyer is in fact equal to the ticket number, but not on live accounts.

Whatever you take for granted from interpreting the documentation, I suggest to check such interpretations. If it were actually meant to be always equal, I am sure the docs would state this explicitly. Since that don't, and use the term "corresponds", I personally am quite confident, my interpretation is correct in this case. But as said, I would need to check this (again) with some code.

EDIT:
Also note the docs state "corresponds to the ticket" and not "corresponds to the ticketnumber".