Sleep function problem

ALI AMRIOUI  

i wana build EA with   Sleep() Function to delay 1 hour between each trades ..but i dont know why its dont work

  • can somme one slove it plz??

   if(LastClosed("CloseTime")==TimeCurrent()    )

{

Sleep(36000000);  

}

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+


bool Trade =True;
extern double lot = 0.01 ;
extern double TP = 0 ;
extern double SL = 0 ;
extern int MagicNumber1 = 1917 ;
double MyPoint, Buyt, Buys, Sellt, Sells, cnt  ;
int Ticket1,Decimal ;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   if(Digits ==4 || Digits <=2)
      MyPoint=Point ;
   if(Digits ==3 || Digits ==5)
      MyPoint=Point*10 ;

   if(MarketInfo(Symbol(),MODE_MINLOT)<0.1)
      Decimal =2;
   else
      Decimal=1;


   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {


  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()

  {

   if(TP == 0)
      Buyt = 0;
   else
      Buyt= Ask+TP*MyPoint;
   if(SL == 0)
      Buys = 0;
   else
      Buys= Ask-SL*MyPoint;

   if(TP == 0)
      Sellt = 0;
   else
      Sellt= Bid-TP*MyPoint;
   if(SL == 0)
      Sells = 0;
   else
      Sells= Bid+SL*MyPoint;




//SLEEP FUNCTION
   if(LastClosed("CloseTime")==TimeCurrent() ) {          Sleep(36000000);    }    if(Trade)      {       //SELLSTOP       if(TS()==0)         {          Ticket1= OrderSend(Symbol(),OP_SELL,lot,Bid,1,   Sells,  Sellt,EaName,MagicNumber1,0,clrRed);         }      }   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ int TS()   {    int s=0,i;    for(i =0 ; i < OrdersTotal() ; i++)      {       if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))          if(Symbol()== OrderSymbol()&& OrderMagicNumber() == MagicNumber1&& OrderType() == OP_SELL)            {             s++ ;            }      }    return (s) ;   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ // last closeinfo double LastClosed(string S,int type =-1)   {    for(int f = OrdersHistoryTotal() -1 ; f>=0; f--)      {       if(OrderSelect(f,SELECT_BY_POS,MODE_HISTORY))          if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber1 && (OrderType()== type || type ==-1))            {             if(S=="Lots")                return(OrderLots());             else                if(S=="OpenPrice")                   return(OrderOpenPrice());                else                   if(S=="ClosePrice")                      return(OrderClosePrice());                   else                      if(S=="TP")                         return(OrderTakeProfit());                      else                         if(S=="SL")                            return(OrderStopLoss());                         else                            if(S=="Type")                               return(OrderType());                            else                               if(S=="Profit")                                  return(OrderProfit());                               else                                  if(S=="OpenTime")                                     return(OrderOpenTime());                                  else                                     if(S=="CloseTime")                                        return(OrderCloseTime());                                     else                                        if(S=="Com")                                           return(OrderCommission());                                        else                                           if(S=="SWAP")                                              return(OrderSwap());                                           else                                              if(S=="Magic")                                                 return(OrderMagicNumber());            }      }    return(0);   }
Fernando Carreiro  
ALI AMRIOUI: i wana build EA with   Sleep() Function to delay 1 hour between each trades ..but i dont know why its dont work

Don't use sleep(). Monitor the trade server time (i.e. TimeCurrent) and detect when at least one hour has passed.

When you need to start the countdown, set a static variable to the current time plus the delay duration, and then when the current time is beyond that value, you can resume normal operations again.

Documentation on MQL5: Date and Time / TimeCurrent
Documentation on MQL5: Date and Time / TimeCurrent
  • www.mql5.com
TimeCurrent - Date and Time - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
ALI AMRIOUI  
Fernando Carreiro #:

Don't use sleep(). Monitor the trade server time (i.e. TimeCurrent) and detect when at least one hour has passed.

When you need to start the countdown, set a static variable to the current time plus the delay duration, and then when the current time is beyond that value, you can resume normal operations again.

But i wana work with Sleep() function,, coz i wana using milisecond in other Ea,, what i should doing then?? 
ALI AMRIOUI  
Fernando Carreiro #:

Don't use sleep(). Monitor the trade server time (i.e. TimeCurrent) and detect when at least one hour has passed.

When you need to start the countdown, set a static variable to the current time plus the delay duration, and then when the current time is beyond that value, you can resume normal operations again.



  • also like that .. its dont work  when i use  OrderType() function

   if(LastClosed("Type" )==OP_SELL)
{
   
      Sleep(36000000);
   
}
Samuel Manoel De Souza  
ALI AMRIOUI:

i wana build EA with   Sleep() Function to delay 1 hour between each trades ..but i dont know why its dont work

  • can somme one slove it plz??

   if(LastClosed("CloseTime")==TimeCurrent()    )

{

Sleep(36000000);  

}

Often order close time won't match TimeCurrent. that is why it doesn't works.

instead do that:

if(TimeCurrent() < LastClosed(ORDER_CLOSE_TIME) + 3600)
   return;

Also avoid using strings. Use enumerations as parameters and switch statement instead of if on your LastClosed() function.

ALI AMRIOUI  
Samuel Manoel De Souza #:

Often order close time won't match TimeCurrent. that is why it doesn't works.

instead do that:

Also avoid using strings. Use enumerations as parameters and switch statement instead of if on your LastClosed() function.

thanks friend for u advice

but i wana using Sleep() function coz its work with  milliseconds ,, and i will use milliseconds  in other EA

Sleep(1000)=1 Second

https://docs.mql4.com/common/sleep
Sleep - Common Functions - MQL4 Reference
Sleep - Common Functions - MQL4 Reference
  • docs.mql4.com
Sleep - Common Functions - MQL4 Reference
Fernando Carreiro  
ALI AMRIOUI #: But i wana work with Sleep() function,, coz i wana using milisecond in other Ea,, what i should doing then?? 

Why do you insist on working with sleep in milliseconds if your delay is for one hour?

Also, using sleep should only be used for very short delays, because it blocks the thread and no other events can be processed by the EA (tick events, chart events, time events, etc.).

MQL programs are event driven and if you block the processing for too long it will hang them. Do it properly as explained above. Use sleep only very small delays (a few seconds at most).

EDIT: Even for a short delays, I prefer monitoring the tick data for the millisecond time stamp, so as not to block event processing on the thread. I NEVER use sleep at all, especially because it does not work on Indicators.

ALI AMRIOUI  
Fernando Carreiro #:

Why do you insist on working with sleep in milliseconds if your delay is for one hour?

Also, using sleep should only be used for very short delays, because it blocks the thread and no other events can be processed by the EA (tick events, chart events, time events, etc.).

MQL programs are event driven and if you block the processing for too long it will hang them. Do it properly as explained above. Use sleep only very small delays (a few seconds at most).

EDIT: Even for a short delays, I prefer monitoring the tick data for the millisecond time stamp, so as not to block event processing on the thread. I NEVER use sleep at all, especially because it does not work on Indicators.

Even i use short delay it does not work
ALI AMRIOUI  
Fernando Carreiro #:

The reason it does not work has already been explained to you in post #4 by @Samuel Manoel De Souza.

And I quote ... "Often order close time won't match TimeCurrent. that is why it doesn't works."

But you ignored his statement without considering it.

Coz he is using seconds and i told u my friend i wana working with Sleep function means miliseconds
Samuel Manoel De Souza  
ALI AMRIOUI #:

thanks friend for u advice

but i wana using Sleep() function coz its work with  milliseconds ,, and i will use milliseconds  in other EA

Sleep(1000)=1 Second

https://docs.mql4.com/common/sleep
If you replace the return by Sleep it shall work.