Help with code

 
Please look at code below, I have a time based stop loss function below but it is not working properly as It only looping on on the first time only (TimeBasedSLperiodLoss1), and does not go to TimeBasedSLperiodLoss2. Any help would be highly appreciated.

 

extern string  Percent_Close="% of position to close at each time interval of a losing trade, <=100%";
extern double  PercentClose1       = 25;//position percent to close
extern double  PercentClose2       = 50;//position percent to close
extern double  PercentClose3       = 75;//position percent to close
extern double  PercentClose4       = 100;//position percent to close

extern string  Loss_Time="Seconds: For each interval how many seconds to wait before losing trade is closed"; 
extern double  TimeBasedSLperiodLoss1   = 300; // in seconds
extern double  TimeBasedSLperiodLoss2   = 600;
extern double  TimeBasedSLperiodLoss3   = 900;
extern double  TimeBasedSLperiodLoss4   = 1200;

void TimeBasedSL(string symbol,double TimeBasedSLperiodLoss1,double TimeBasedSLperiodLoss2,double TimeBasedSLperiodLoss3,double TimeBasedSLperiodLoss4) {
 bool result;
 double TimeBasedSL=0.0;
 

 string sTimeBasedSLperiod;
 
 int totalopenorders=0;
 for (int cnt=OrdersTotal()-1; cnt>=0; cnt--) {
  if (!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) continue;
  if (OrderSymbol()!=symbol) continue;
   {
    totalopenorders++;
    double profit = (OrderProfit()+OrderCommission()+OrderSwap());
    //Order is in loss
     TimeBasedSL = TimeBasedSLperiodLoss1;//seconds
    
    
        double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
                 lotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
                 Lots    = OrderLots();
    
    bool alreadyExecuted1 = false;
    bool alreadyExecuted2 = false;
    bool alreadyExecuted3 = false;
    bool alreadyExecuted4 = false;
    

// I DO NOT KNOW HOW TO CODE THE BELOW. PLEASE HELP! It keeps looping on TimeBasedSLperiodLoss1 (300 seconds) and does not go to TimeBasedSLperiodLoss2 (600 seconds).  
// How do I make sure for each symbol's trade  TimeBasedSLperiodLoss1 is only executed once.

    if (profit<=0.0) {

    if(!alreadyExecuted1) {
    
    if ((TimeCurrent() - OrderOpenTime()) >= TimeBasedSLperiodLoss1) {

     while(IsTradeContextBusy()) Sleep(100);
     RefreshRates();
     if (OrderType()==OP_BUY) {//buy
      result=OrderClose(OrderTicket(),MathFloor(Lots*(PercentClose1/100)/lotStep)*lotStep,MarketInfo(OrderSymbol(),MODE_BID),9999,CLR_NONE);
      if (result) Print("TimeBasedSL(): **** BUY order ticket "+OrderTicket()+" (opentime: "+TimeToStr(OrderOpenTime())+") "+sTimeBasedSLperiod+" of "+DoubleToStr(profit,2)+" "+AccountCurrency());
     }
     if (OrderType()==OP_SELL) {//sell
      result=OrderClose(OrderTicket(),MathFloor(Lots*(PercentClose1/100)/lotStep)*lotStep,MarketInfo(OrderSymbol(),MODE_ASK),9999,CLR_NONE);
      if (result) Print("TimeBasedSL(): **** SELL order ticket "+OrderTicket()+" (opentime: "+TimeToStr(OrderOpenTime())+") "+sTimeBasedSLperiod+" of "+DoubleToStr(profit,2)+" "+AccountCurrency());
     }
     }
     alreadyExecuted1 = true;

    }
  }
   }
 
}
 

and does not go to TimeBasedSLperiodLoss2

When it must go tp period2? 

 
Can you please fix the above bug?
 

Make these variables static or declare globally.

    static bool alreadyExecuted1 = false;
    static bool alreadyExecuted2 = false;
    static bool alreadyExecuted3 = false;
    static bool alreadyExecuted4 = false;
Also, use integers when working with time.
extern int  TimeBasedSLperiodLoss1   = 300; // in seconds
extern int  TimeBasedSLperiodLoss2   = 600;
extern int  TimeBasedSLperiodLoss3   = 900;
extern int  TimeBasedSLperiodLoss4   = 1200;

int TimeBasedSL=0;
 
Thanks, but the above did not fix the bug. :(
Reason: