issue with checking result of last two orders from the same Symbol

 

The following code works if it's attached to only a single pair, but if the EA attached to multiple pairs, it also counts the result of cross pair and results wrong alert. 

The idea of the following code is to decide if the attached pair has the last two trade ends positively.  It should not count the other pair result from the history. 


void Alert_if_the_last_two_ordes_for_same_pair_was_positive()



  {



   



   double profit = 0;



   int orderId = -1;



   datetime lastCloseTime = 0;



   int cnt = OrdersHistoryTotal();



   for(int i=0; i < cnt; i++)



     {



      if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))



         continue;



      orderId = OrderMagicNumber();



      if(OrderSymbol() == Symbol() && lastCloseTime < OrderCloseTime() && orderId == 114)



        {



         lastCloseTime = OrderCloseTime();



         profit = OrderProfit();







        }



     }







   double profit1 = 0;



   int orderId1 = -1;



   datetime lastCloseTime1 = 0;



   int cnt1 = OrdersHistoryTotal()-1;



   for(int i1=0; i1 < cnt1; i1++)



     {



      if(!OrderSelect(i1, SELECT_BY_POS, MODE_HISTORY))



         continue;



      orderId1 = OrderMagicNumber();



      if(OrderSymbol() == Symbol() && lastCloseTime1 < OrderCloseTime() && orderId1 == 114)



        {



         lastCloseTime1 = OrderCloseTime();



         profit1 = OrderProfit();



        }



     }



     



        double profit2 = 0;



   int orderId2 = -1;



   datetime lastCloseTime2 = 0;



   int cnt2 = OrdersHistoryTotal()-2;



   for(int i2=0; i2 < cnt2; i2++)



     {



      if(!OrderSelect(i2, SELECT_BY_POS, MODE_HISTORY))



         continue;



      orderId2 = OrderMagicNumber();



      if(OrderSymbol() == Symbol() && lastCloseTime2 < OrderCloseTime() && orderId2 == 114)



        {



         lastCloseTime2 = OrderCloseTime();



         profit2 = OrderProfit();



        }



     }



     



     







   if(profit > 0 && profit1 > 0 )



     {

Alert(" profit > 0 && profit1 = up for " + Symbol());



)
Reason: