HELP! EA MQL4 - Sleep

hello I could never work with sleep.
I’m working on an EA that seeks the historic find a negative order, need to sleep (5000).
Someone help me please.
Thank you for responding.

static int closedOrders=0;
      if(OrdersHistoryTotal()!=closedOrders)
      {
         closedOrders=OrdersHistoryTotal();
         int hstTotal = OrdersHistoryTotal(); 
        
         if(OrderSelect(hstTotal-2,SELECT_BY_POS,MODE_HISTORY))
         if(OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber3)
         {
            if(OrderOpenPrice()<OrderClosePrice())
            {
              Balancoanterior = AccountBalance();
 
              Sleep(50000);
            }
            
           
         } //FIM SELEÇÃO OP_SELL
}


Your Sleep function is fine.

The other logic seems incorrect to me, but its your ea.

You use ‘static’ to define closedOrders, so after the first pass closedOrders wont change, unless you update it somewhere else. Or until a new order comes in , which I suppose is what you want.
“if(OrdersHistoryTotal()!=closedOrders)” will only run once.

I dont see any loop, so you only check 1 order in the history as well. Again that might be on purpose.

Without seeing any other code, your EA will only Sleep if the last order is negative and its a SELL order.

To test sleep you can run it on the tick function and print something after it .

Sleep(5000);
Print(“Just woke up”);

Why use this feature ?

it is better to remove all of the code

he is trying to see if a previous trade was a lose. And then stop his EA for a set period of time.

The way he is attempting it is wrong though.

HI,
Can any one help me please…
In this EA i have set cci >200 to buy and -200 to sell for scalping but,
its countunisly opening oders how to set is to sleep for at least 60 second.
thanks you…

//±-----------------------------------------------------------------+
//| CCI EA.mq4 |
//| Copyright 2018, MetaQuotes Software Corp. |
//| |
//±-----------------------------------------------------------------+
#property copyright “Copyright 2018”
#property link “”
#property version “1.00”
#property strict

input double StopLoss=100;
input double TakeProfit=40;
input double TrailingStop=15;
input double Moving_Avarage=5;
input int RSIperiod=14;
input double BuyLevel=30;
input double SellLevel=70;
input bool AutoLot=False;
input double Risk=10;
input double ManualLots=0.01;
input int MagicNumber=1&2;
input string Comment=“Candle_Pro_EA”;
input int Slippage=10;

int OrderBuy,OrderSell;

int LotDigits;
double MacdCurrent,MacdPrevious;
double SignalCurrent,SignalPrevious;
double MaCurrent,MaPrevious;
int ticket;
int total;

//±-----------------------------------------------------------------+
//| Expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
return(0);
}
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
int deinit()
{
return(0);
}
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
int start()
{
OrderBuy=0;
OrderSell=0;
for(int cnt=0; cnt<OrdersTotal(); cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==Comment)
{
if(OrderType()==OP_BUY) OrderBuy++;
if(OrderType()==OP_SELL) OrderSell++;
}
}

// double rsi=iRSI(Symbol(),0,RSIperiod,PRICE_CLOSE,0);
double x1 = iCCI(Symbol(),_Period,20,PRICE_TYPICAL,0);

//open position
if(OrderBuy < 1 && x1< 200 && x1>= 200 ) OPBUY();
if(OrderSell < 1 && x1> -200 && x1>= -200 ) OPSELL();

//close position by signal
if(OrderBuy > 0 && x1> 200 ) CloseBuy();
if(OrderSell > 0 && x1< 200 ) CloseSell();

//— it is important to enter the market correctly, but it is more important to exit it correctly…
for(int cnt=0;cnt<total;cnt++)
{
if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
//— long position is opened
if(OrderType()==OP_BUY)
{
}
//— check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>PointTrailingStop)
{
if(OrderStopLoss()<Bid-Point
TrailingStop)
{
//— modify order and exit
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
Print("OrderModify error ",GetLastError());
return(0);
}
}
}
}
else // go to short position
{

        //--- check for trailing stop
        if(TrailingStop>0)
          {
           if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
             {
              if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                {
                 //--- modify order and exit
                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                    Print("OrderModify error ",GetLastError());
                 return(0);
                }
             }
          }
       }
    }

return(0);
}

//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
void OPBUY()
{
double StopLossLevel;
double TakeProfitLevel;
if(StopLoss>0) StopLossLevel=Bid-StopLossPoint; else StopLossLevel=0.0;
if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit
Point; else TakeProfitLevel=0.0;

ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Comment,MagicNumber,0,DodgerBlue);
}
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
void OPSELL()
{
double StopLossLevel;
double TakeProfitLevel;
if(StopLoss>0) StopLossLevel=Ask+StopLossPoint; else StopLossLevel=0.0;
if(TakeProfit>0) TakeProfitLevel=Bid-TakeProfit
Point; else TakeProfitLevel=0.0;

ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Comment,MagicNumber,0,DeepPink);
}
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
void CloseSell()
{
total=OrdersTotal();
for(int y=OrdersTotal()-1; y>=0; y–)
{
if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber)
{
ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
}
}
}
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
void CloseBuy()
{
total=OrdersTotal();
for(int y=OrdersTotal()-1; y>=0; y–)
{
if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber)
{
ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
}
}
}
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
double LOT()
{
double lotsi;
double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT);
double ilot_min =MarketInfo(Symbol(),MODE_MINLOT);
double tick=MarketInfo(Symbol(),MODE_TICKVALUE);

double myAccount=AccountBalance();

if(ilot_min==0.01) LotDigits=2;
if(ilot_min==0.1) LotDigits=1;
if(ilot_min==1) LotDigits=0;

if(AutoLot)
{
lotsi=NormalizeDouble((myAccount*Risk)/10000,LotDigits);
} else { lotsi=ManualLots;
}

if(lotsi>=ilot_max) { lotsi=ilot_max; }

return(lotsi);
}
//±-----------------------------------------------------------------+