Sleep() will be ignored

 

Hi guys,

I´m very frustrated because my Sleep-function doesn´t work. Everytime my system open a pending order, and I only wnat to open one order, it opened more than one order.

Why does the Sleep-function not work?


//+------------------------------------------------------------------+
//| A4.mq4 |
//| Copyright © 2010, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"


//+------------------------------------------------------------------+
//| extern input parameters |
//+------------------------------------------------------------------+
extern int Slippage=5;
extern int LotDiff=1000000;
extern double ProfitDiff=0.001;
extern int MagicNumber_101=101000;
extern int MagicNumber_102=102000;


double dXPoint=1;


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

if (Digits==3||Digits==5)
{
dXPoint=10;
}

if (Digits==3||Digits==5)
{
dXPoint=10;
int SLIPPAGE=SLIPPAGE*dXPoint;
}




//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double entry_high=iHigh(0,0,1);
double entry_low=iLow(0,0,1);

{
startLookingForProfits(MagicNumber_101);
startLookingForProfits(MagicNumber_102);
}

// Variante 1

if ((Minute()==14 || Minute()==29 || Minute()==44 || Minute()==59) && iHigh(0,0,2)<iHigh(0,0,1) && iHigh(0,0,1)>iHigh(0,0,0))

{

OrderSend("EURUSD",OP_BUYSTOP,AccountBalance()/LotDiff,entry_high,2,NULL,NULL,"Magic Number 101000",MagicNumber_101,0,CLR_NONE);

}

if ((Minute()==14 || Minute()==29 || Minute()==44 || Minute()==59) && iLow(0,0,2)>iLow(0,0,1) && iLow(0,0,1)<iLow(0,0,0))

{

OrderSend("EURUSD",OP_SELLSTOP,AccountBalance()/LotDiff,entry_low,2,NULL,NULL,"Magic Number 102000",MagicNumber_102,0,CLR_NONE);

}

return(0);
}

Sleep(180000);

int startLookingForProfits(int MagicNumber)
{
int ProfitTarget=AccountBalance()*ProfitDiff;
double dblProfit=0;
int POS=0;
bool boolTerm=false;
while(boolTerm==false)
{
if(OrderSelect(POS,SELECT_BY_POS))
{
if(OrderMagicNumber()==MagicNumber) dblProfit=dblProfit+OrderProfit();
POS++;
}
else
boolTerm=true;
}
if(dblProfit>=ProfitTarget)
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderMagicNumber()==MagicNumber)
{
int type=OrderType();
bool result=false;

switch(type)
{
//Close opened long positions
case OP_BUY :
result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Red);
break;

//Close opened short positions
case OP_SELL :
result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,LimeGreen);
break;
}

if(result==false)
{
Sleep(1000);
}
}
}
Print("Profit Target For MagicNumber based Trades Reached & They have been Closed");
return(0);
}
/*
Comment("AccountBalance: $",AccountBalance(),"\n",
"Account Equity: $",AccountEquity(),"\n",
"Profit Target MagicNo. Based Trades: $",ProfitTarget,"\n",
"Current Profit MagicNo. Based Trades: $",dblProfit,"\n");
*/
return(0);
}
 

Its not going to sleep forever. Theres allot of different ways to limit orders to just one. Sleep isn't one of them.

 
ubzen:

Its not going to sleep forever. Theres allot of different ways to limit orders to just one. Sleep isn't one of them.


Hi ubzen,


I only want to open a pending order when Minute()==57 and then I place a Sleep()=180000 so it had to open only one order and sleep for 3 minutes. The 3 minutes later it is not Minute()==57 and it should wait for the next hour and Minute()==57.

Is there another possible way to place only one pending order per hour?


Kind regards

 
jimbofx7:

Hi ubzen,


I only want to open a pending order when Minute()==57 and then I place a Sleep()=180000 so it had to open only one order and sleep for 3 minutes. The 3 minutes later it is not Minute()==57 and it should wait for the next hour and Minute()==57.

Is there another possible way to place only one pending order per hour?


Kind regards

How about if( Minute() != 57 ) return from start?

IMO, If someone do-not have intro to programming learned then you're wisting your time. I'm not a big fan of learning by guessing. Try reading the book.

 
jimbofx7:

Hi guys,

I´m very frustrated because my Sleep-function doesn´t work. Everytime my system open a pending order, and I only wnat to open one order, it opened more than one order.

Why does the Sleep-function not work?



Diificult to tell without indentation of the braces, but as far as I can tell, return(0) comes before Sleep().

So you exit the start function without calling sleep, so the code will run again at the next tick.

 

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Compile your code. Your sleep is outside of start so what you posted will NOT compile
  3. What are Function return values ? How do I use them ? - MQL4 forum
 

Thanks for your cooments!


I changed the position of the sleep function and it works.


Many thanks!

Reason: