I have an issue for getting the last closed order's open price in mt5

 
double H_LastOrderOpenPrice(int Type)
  {
   double price=0; ulong ticket =0;
   HistorySelect(0,TimeCurrent()+1);
   int orders=HistoryDealsTotal()-1;
   for(int i=orders;i>=0;i--)
     {
      ticket = HistoryDealGetTicket(i);
      if(ticket>0 && HistoryDealGetInteger(ticket,DEAL_TYPE)==Type && HistoryDealGetInteger(ticket,DEAL_MAGIC)==MagicNumber &&
      HistoryDealGetString(ticket,DEAL_SYMBOL)==_Symbol && HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_IN)
        {
         price=NormalizeDouble(HistoryDealGetDouble(ticket,DEAL_PRICE),_Digits);
         break;
        }
     }

   return(price);
  }

the strategy is in which this function is using placed multiple limit orders and when an order become market order and closed on Take Profit above function returns the price of that closed order and a new limit order is placed on that price so, when 1st order closed on TP this function returns correct value and order is placed correctly but when 2nd order closed on TP above function returns the value of 1st closed order open price which is wrong can someone help here?

Reason: