"For" cycle never ending and making the same action multiple times mql4

 

anyone help me?

here's the code: 

   { if (OrdersTotal() == 0 && iRSIm51 > 70 && iRSIh41 > iRSIh42){
    int ticket = OrderSend (Symbol() , OP_SELL , 0.1 , Bid , 0 ,Bid + 100*Point , Bid - 220*Point , "venda" , 0, TimeCurrent ()+ 3600 * 24 , clrRed );}
    }
    
 
      for (int i = 0; i < OrdersHistoryTotal(); i++){
         if (OrderSelect(i,SELECT_BY_POS,  MODE_HISTORY)== true){  // to get OrderProfit
         int profit = OrderProfit();
            if (OrderSelect(i,SELECT_BY_POS,  MODE_TRADES)== true){ // to make sure the last order made is a sell order
            int type = OrderType();}
              
               {if (profit < 0 && type == OP_SELL )  //if profit is negative and last operation == sell, make an ordersend
               ticket = OrderSend (Symbol(), OP_BUY, 0.1, Ask, 0, Ask - 100*Point, Ask + 220*Point, "compra", 0, TimeCurrent ()+ 3600 * 24, clrGreen);
               i++;  //+1 to i since OrderHIstoryTotal is increasing by 1 for the buy order
               
               //code should've ended here and gone again to the top
               }
               

I actually wanted it to wait the s/l from the order 1 and then make 1 only order, but it is making 2 sell orders first and then infinite buy orders

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
Trade Orders in DOM - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5