How to Count Losses Trades until Get TakeProfit

 

hellow, i want to county losses trades untli one of them will not Get TakeProfit. if order gets takeprofit order count must set to 0.

i tried but my code dosn't work.

//+------------------------------------------------------------------+

int TotalOrderCount(int MagicNumber)

{

   int count = 0;

   

   for(int i = OrdersTotal() - 1; i >= 0; i--)

   {

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

      {

         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

            count++;

      }

   }

   

   for(int i = OrdersHistoryTotal() - 1; i >= 0; i--)

   {

      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

      {

         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

            if(OrderProfit() < 0) count++;

            else count = 0; 

      }

   }

   return(count);

}