Simple code using SLEEP not working

 

Hello everyone,

I'm new to mql4 and I've been trying to use this very simple code with no success.

The idea is to trade at specific time, puting the script into sleep for about 2 hours, and then making the order.

It works for 6 minutes, but does not for such time as 2 hours, i.e., after 2 hours it is removed (uninit reason 0).

Does anyone can help me, or have other idea to make it work?

Many thanks in advance.

int start()
{

if ((Hour()==10) && Minute()==00)
{
Sleep(7200000);
OrderSend(Symbol(),OP_BUY,0.01,Ask,3,Ask-50*Point,Ask+20*Point,"",0,Green);
}

}

 

Did you see about function Sleep() ?

The Sleep() function suspends execution of the current expert within the specified interval.
The Sleep() function cannot be called from custom indicators since they calculate in the interface thread and may not decelerate it.
The checking of the expert stop flag status every 0.1 second has been built into the function.

In addition to it see article Testing Features and Limits in MetaTrader 4:

Special Features of Testing Strategies on History Data

  • Some functions are processed/passed without output

    These are Sleep(), Alert(), SendMail(), PlaySound(), MessageBox(), WindowFind(), WindowHandle(), WindowIsVisible()

 
Rosh wrote >>

Did you see about function Sleep() ?

In addition to it see article Testing Features and Limits in MetaTrader 4:

Thanks Rosh,

As i said i'm really a rookie...

Do you know other way to make it work?

What i really want is to be able to make the order at a specific time, i.e., 3 or 4 hours from the moment i initialize the script in the pair.

Thank you once more.

 
Store current time in global variable and then compare incoming tick time to saved value in that global variable. This is best way in my opinion.
 

Seeing you are a rookie!

//+------------------------------------------------------------------+
//|                                            _DelayMarketOrder.mq4 |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| input parameters:                                                |
//+-----------0---+----1----+----2----+----3]------------------------+
extern bool                         SellOrder = true;
extern double                            Lots = 0.1;
extern int                           StopLoss = 30;
extern int                         TakeProfit = 50;
extern int    MinutesToWaitBeforePlacingOrder = 120;

//+------------------------------------------------------------------+
//| global variables to program:                                     |
//+------------------------------------------------------------------+
bool     gbOrderPlaced;
datetime gtPlaceOrder;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
void init() {
  gtPlaceOrder=TimeCurrent()+ MinutesToWaitBeforePlacingOrder*60;
}

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
void deinit() {
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void start() {
  int iTicket;
    
  if((TimeCurrent()<gtPlaceOrder) || gbOrderPlaced) return;
  if(SellOrder) iTicket=OrderSend(Symbol(),OP_SELL,Lots,Bid,5,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Delayed order",2008,0,Green);
  else          iTicket=OrderSend(Symbol(),OP_BUY, Lots,Ask,5,Bid-StopLoss*Point,Ask+TakeProfit*Point,"Delayed order",2008,0,Green);
  if(iTicket<0) {
    Print("OrderSend failed with error #",GetLastError());
    return;
  }
  gbOrderPlaced=true;
}
//+------------------------------------------------------------------+
 
sxTed wrote >>

Seeing you are a rookie!

Thank you very much!

keep working...

Reason: