Buy on previous nigh and low

 

Hello I will appreciate your help, I have written a function but it is not doing out operations as required. Upon launching EA on chart it should open buy position on break of previous high later on the preceeding orders will be on break of previous low. I f any of the orders hit sl the EA resumes on buying on break of previous high then wait for next break on prev low to open another buy. This should be cycle of EA. Here is my code and I will appreciate your corrections;

string GetEntrySignal()
  {
       string signal = "";
       datetime last_time = 0;
       double profit = 0;
      
       
       double ihigh = iHigh(_Symbol, PERIOD_CURRENT, 1);
       double ilow  = iLow(_Symbol, PERIOD_CURRENT, 1);
      
       if(Bid > ihigh )
         {
              signal = BUYSIGNAL;
             
         }
      
       if(data.positions > 0 )
         {
            if(Bid < ilow)
              {
                  signal = BUYSIGNAL;
                  
              }
         }
   
   //check for loss trade
   for(int i = OrdersHistoryTotal() - 1;i >= 0; i--)
       {
             if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))break;
             if(OrderSymbol() != Symbol())continue;
             if(OrderMagicNumber() != Magic)continue;
               last_time = OrderCloseTime();
              if(OrderOpenTime() > last_time) 
               {
                    if(OrderProfit() < 0)
                    profit = 0;
                    profit = OrderProfit();
               }  
            break;      
        } 
        
    
       return signal;
   
  }