Two questions in OnTradeTransaction code found in Forum

501
11
501  

I have two questions in below code I found in MQL5 forum.

Question 1 (Highlight in red): Why this needed? When "trans.type" is TRADE_TRANSACTION_ORDER_ADD, there should be no deal yet.

Question 2 (Highlight in green): When there are partial fills, order ID on 2nd deal will be automatically generated by the trading server side? In other word, the original order ID will not be carried over?

void  OnTradeTransaction(
    const MqlTradeTransaction&    trans,        // trade transaction structure
    const MqlTradeRequest&        request,      // request structure
    const MqlTradeResult&         result        // result structure
    )
{
    static ulong position = 0;

    if (
            (ENUM_DEAL_ENTRY)HistoryDealGetInteger(trans.deal,DEAL_ENTRY) == DEAL_ENTRY_IN && 
            trans.type == TRADE_TRANSACTION_ORDER_ADD &&
            trans.order_type == ORDER_TYPE_BUY_LIMIT
        )
    {
        Print(StringFormat("A BUY_LIMIT pending order [%d] has been added.", trans.order ));
    }
    if ( 
            (ENUM_DEAL_ENTRY)HistoryDealGetInteger(trans.deal,DEAL_ENTRY) == DEAL_ENTRY_IN && 
            trans.type == TRADE_TRANSACTION_DEAL_ADD && 
            trans.order == trans.position
        )
    {
        position = trans.order;
        Print(StringFormat("A limit order [%d] has been executed.", position));
    }
    if ( 
            (ENUM_DEAL_ENTRY)HistoryDealGetInteger(trans.deal,DEAL_ENTRY) == DEAL_ENTRY_IN && 
            trans.type == TRADE_TRANSACTION_DEAL_ADD &&
            trans.order != trans.position &&
            trans.position == position
        )
    {
        Print(StringFormat("More has been added to position [%d].", position));
    }
    if ( 
            (ENUM_DEAL_ENTRY)HistoryDealGetInteger(trans.deal,DEAL_ENTRY) == DEAL_ENTRY_OUT && 
            trans.type == TRADE_TRANSACTION_DEAL_ADD && 
            trans.position == position
        )
    {
        Print(StringFormat("Position [%d] has been closed.", position));
    }
}
Dominik Christian Egert
1652
Question 1:

As far as I know, this function only gets called when there was a transaction. It is meant to process the result.

Question 2:
Every deal, order and position get their own ticket number. Only Position ID is unique and let's you connect a position with orders and deals. This ID gets carried over. Ticket numbers do not get carried over.