How to get TP of closed position?

To get the current TP of an open position (hedging mode), I use


Once the position is closed, I search for the corresponding ENTRY_IN deal and order, and query the order for the TP:

if (HistorySelectByPosition(positionId)) {
   for (int i = HistoryDealsTotal()-1; i >= 0; i--) {
      ulong dealId = HistoryDealGetTicket(i);
      if (HistoryDealGetInteger(dealId, DEAL_POSITION_ID) != positionId) continue;
      if ((ENUM_DEAL_ENTRY)HistoryDealGetInteger(dealId, DEAL_ENTRY) != DEAL_ENTRY_IN) continue;
      ulong orderId = HistoryDealGetInteger(dealId, DEAL_ORDER);
      if (HistoryOrderSelect(orderId)) {
         return HistoryOrderGetDouble(orderId, ORDER_TP);

However, this only works if the TP was set directly while opening the position. If the TP was set later, this does not work. I examined all corresponding deals and orders, but was not able to find the corresponding TP anywhere.

What is a robust method to obtain the TP of a closed position?


there is something wrong about char arrays I guess

Created a product review summarizer which clustered thousands of product reviews and summarized them into a maximum of 500 characters