event handler for storing position ticket when stop order is filled

 

Hi,

I want to store a position ticket in result of stop order has been filled. I already store the order ticket when opening stop order and when the order is filled I want to update the stored ticket to a position ticket. If I loop all the position and find the position for that symbol and that strategy it will require a lot of resources since it will repeat the loop action every tick. Is there an event handler when the stop order is filled then I retrieve the position ticket and store it?


Update:

I tried to use OnTradeTransaction() but I don't get the result that I want. When I tried printing position ticket the result is different between deal and position ticket.

void OnTradeTransaction(
   const MqlTradeTransaction& trans,
   const MqlTradeRequest&     request,
   const MqlTradeResult&      result) {
   for(int SymbolLoop = 0; SymbolLoop < NumberOfTradeableSymbols; SymbolLoop++) {
      //... if there's an update from pending order
      if(trans.type == TRADE_TRANSACTION_DEAL_ADD) {

         ulong deal_ticket = trans.deal;
         ENUM_DEAL_TYPE deal_type = trans.deal_type;

         if(HistoryDealSelect(deal_ticket))
            deal.Ticket(deal_ticket);
         else
            return;

         if(deal.Symbol() == SymbolArray[SymbolLoop]) {

            //... looked for the deal type and find deal entry state
            if(deal.Magic() == GlobalExpertID(SymbolLoop, InpTradeTimeframe, InpMagicEA) + 1 && deal_type == DEAL_TYPE_BUY) {
               Print("history deal entry buy: ", HistoryDealGetInteger(BuyTicket[SymbolLoop], DEAL_ENTRY), ", (0=in, 1=out)");
               if(HistoryDealGetInteger(BuyTicket[SymbolLoop], DEAL_ENTRY) == DEAL_ENTRY_IN) {
                  BuyTicket[SymbolLoop] = deal_ticket;
                  Print("buy ticket: ", BuyTicket[SymbolLoop]);
               }
            }

            //... looked for the deal type and find deal entry state
            if(deal.Magic() == GlobalExpertID(SymbolLoop, InpTradeTimeframe, InpMagicEA) + 2 && deal_type == DEAL_TYPE_SELL) {
               Print("history deal entry sell: ", HistoryDealGetInteger(SellTicket[SymbolLoop], DEAL_ENTRY), ", (0=in, 1=out)");
               if(HistoryDealGetInteger(SellTicket[SymbolLoop], DEAL_ENTRY) == DEAL_ENTRY_IN) {
                  SellTicket[SymbolLoop] = deal_ticket;
                  Print("sell ticket: ", SellTicket[SymbolLoop]);
               }
            }
         }
      }

      Print("buy ticket: ", BuyTicket[SymbolLoop], ", sell ticket: ", SellTicket[SymbolLoop]);
   }
}

Log in finding order deal and position ticket.

CS      0       14:53:30.125    Lybra Super Scalper EA Master - multi currency (EURUSD,H1)      2018.01.02 08:00:40   POSITION BUY TICKET: 1
CS      0       14:53:30.125    Lybra Super Scalper EA Master - multi currency (EURUSD,H1)      2018.01.02 08:00:40   POSITION SELL TICKET: 2
CS      0       14:53:30.125    Lybra Super Scalper EA Master - multi currency (EURUSD,H1)      2018.01.02 08:00:40   deal buy ticket: 2
CS      0       14:53:30.125    Lybra Super Scalper EA Master - multi currency (EURUSD,H1)      2018.01.02 08:00:40   order buy ticket: 2
CS      0       14:53:30.125    Lybra Super Scalper EA Master - multi currency (EURUSD,H1)      2018.01.02 08:00:40   deal buy ticket: 2, deal sell ticket: 3
 

Deal ticket is different from position ticket. To access position ticket you should use:

https://www.mql5.com/en/docs/standardlibrary/tradeclasses/cdealinfo/cdealinfopositionid

Documentation on MQL5: Standard Library / Trade Classes / CDealInfo / PositionId
Documentation on MQL5: Standard Library / Trade Classes / CDealInfo / PositionId
  • www.mql5.com
PositionId - CDealInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5