Put a delay on next Trade after Stop loss hit

 

Hi,

I am trying to put a 20 sec delay after  STOP LOSS hits and wrote some code about it but it fails, where am i wrong, any help please?

Orange highlighted area is the part where i am trying to put some delay

int start()

    {

    
//----   
   double MaCurrent;
   int ticket, total;

   if(Bars<20)
     {
      Print("bars less than 20");
      return(0);  
     }
   if(TakeProfit<8)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
   MaCurrent=iMA(NULL,0,EMA,0,MODE_EMA,PRICE_CLOSE,0);
   

  //...
  //---- Find my open order,
  for(int index = OrdersTotal() - 1; index >= 0; index--) if (
     OrderSelect(index, SELECT_BY_POS)
  && OrderMagicNumber() == 34567 || 56789       // with my magic number,
  && OrderSymbol()      == Symbol() )   // in my chart.



    static datetime newAllowed;
    newAllowed = TimeCurrent() + 20 * 1; // No new trade until this one closes 20 sec * min


if (TimeCurrent() < newAllowed) return(0);  // No new trades yet.

// Now I can open a new one.
   
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(3*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
     
      
     if(OrdersTotal()>0){
         for(i=1; i<=OrdersTotal(); i++)          // Checking to make sure there are no open orders. Keep this set to zero unless you want the advisor to open more than one buy at a time or more than one sell at a time.
         {
            if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
               {
                   if(OrderMagicNumber()==34567) {int halt1=1;}///if this magicnumber has an order open... halt!
                   if(OrderMagicNumber()==56789) {int halt2=1;}///if this magicnumber has an order open... halt!
                   }
                  }
                  }
       // check for long position (BUY) possibility
      if(
         
          (Close[0]-MaCurrent)<=buyfark )
         
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-Stoploss*Point,Bid+TakeProfit*Point,"Scalp",34567,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if ((Close[0]-MaCurrent)>=sellfark)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+Stoploss*Point,Ask-TakeProfit*Point,"Scalp",56789,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
     return(0);
   }
// the end.
 
  1.   for(int index = OrdersTotal() - 1; index >= 0; index--) if (
        ( OrderSelect(index, SELECT_BY_POS) && OrderMagicNumber() == 34567    )
     || ( true                              && OrderSymbol()      == Symbol() )
     )
         static datetime newAllowed;
    
    newAllowed = TimeCurrent() + 20 * 1; // No new trade until this one closes 20 sec * min
    if (TimeCurrent() < newAllowed) return(0);  // No new trades yet.
    
    Rewrote your code to Precedence Rules.
  2. True is non-zero. Your if selects magic number 34567 on any chart or any trade on the current symbol.
  3. The body of the for and if does nothing.
  4. NewAllowed is always set to the future each tick and you never trade.

  5.    total=OrdersTotal();
       if(total<1) {
          // no opened orders identified
          if(AccountFreeMargin()<(3*Lots)) …     
          
          if(OrdersTotal()>0){
             for(i=1; i<=OrdersTotal(); i++)
    When will the last if and for ever run?
  6. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles