Making the EA sleep for an hour without using Sleep()

 

Im trying to get my ea to sleep for 1 hour after closing a trade, i use sleep() in live trading but this isnt viable in backtesting so my backtests are not completely accurate.


I have opened 2 threads over the past couple of months on this forum about this,and have followed peoples suggestions, but it still doesnt sleep for an hour (in backtesting) .


I have written the following function but it doesnt work expectedly, not sure why because i have been told TimeCurrent() uses the current backtest time not the actual current time, so it should work when backtesting.



bool timeCheck()
{
if(OrderSelect(1, SELECT_BY_POS,MODE_HISTORY)==true)
{
if(TimeCurrent() - OrderCloseTime() < 3600)return(false);

return(true);
}


else
{
return(true);
}
}

 

Hello a


My musings below based on idea that you are not interested in physical time. That you are interested in virtual time.

This suggestion/idea probably already thought of, anyway here is my 'off top of head' idea (of course 1orMore of these statements may/mayNot be valid :)


1. The constant in bar data: Time[n] //barTime

2. barTime in tester seems asif can view as virtual time as opposed to physical time

3. if 2. then successive barTime's occur at known/regular intervals, ie. Period()

4. A time window or Sleep() period (eg, your 1hr) is composed of a series of these time windows (ignoring that milliseconds and smallest Period()=M1, not same)

5. so, a series of virtual time windows should be able to mimic 1hr physical time

6. I end up here: if assume M1 then counting 60 time windows or bars results in virtual Sleep(1hr)

7. with simple once off (say in init()+global) calculation you can work out bars to count to achieve desired virtual sleep time

8. guess if 7. then start() must ignore data ticks as doing bar counting (viz: the "is this data tick first tick indicating new bar" and by inference end of 1 time window)

9. if must process data ticks 8. still holds i think because you are into post trade closure sleep state and data tick processing not going to happen in physical time because of Sleep(1hr)...


10. above may not fit exactly into live or test running. could protect above when are ready for 1hr sleep virtual||physical:

if(IsOptimization() || IsTesting()) {do above.. or something like this! ;} else {normal start() code leading to Sleep(1hr)}


i go back to sleep myself now...

:o))

 

alpha


have you found solution?


if so, could you publish?


is instructive to see how things developed - increases my/others knowledgebase.


thank you

 
I'm also interested in this so please post any updates
 

oik, ive got an idea:


- get current time

- add seconds*n to time

- get the difference of the 2 above

- start a counter until difference = true

- continue processing ....


anyone want to start me off with some basic code ????

 
buju:

oik, ive got an idea:

- get current time

- add seconds*n to time

- get the difference of the 2 above

- start a counter until difference = true

- continue processing ....

anyone want to start me off with some basic code ????

alpha430's original code looks as though it should be fine. If there's a problem with it, then it probably lies in the selection of the old historic order you're comparing against. How do know that the order you want to compare against will always be the second one (i.e. with index #1) in the MODE_HISTORY list? The bit where you compare TimeCurrent() against OrderCloseTime() looks fine. If it's not working as intended, then I'm guessing that you're comparing against the wrong order.

 

Sleep() doesn't work in the tester

TimeCurrent() is the time NOW, you want the time of the tester's current tick

start() {
  static datetime Time.update;			
  if (CurTime() < Time.update)	return(0);	// continue waiting.
  ...
  if (CONDITION) {			        // Not good enough 
    Time.update	= CurTime() + Period()*60;	// Wait a bar
 
WHRoeder:

TimeCurrent() is the time NOW, you want the time of the tester's current tick

No, it isn't. At least, not on my computer. When I backtest the following EA, I see the time of the tester's current tick:


int start()
{
   Comment("Latest time in tester: " + TimeToStr(TimeCurrent(), TIME_DATE | TIME_SECONDS));
}
 
buju:

oik, ive got an idea:


- get current time

- add seconds*n to time

- get the difference of the 2 above

- start a counter until difference = true

- continue processing ....


anyone want to start me off with some basic code ????


// this code will get the current time, wait until a certain amount of Seconds
// has passed and then continue processing.

// NOTE: the first run will produce a error as the stored Time
// will be re initialised, after that it will function correctly.

//Author: Desmond aka " buju"

extern int wtime=60; //waiting time in seconds
int handle;
int stime;
int ftime;

int start()
{

int xtime=(TimeCurrent()); //get current time

//Print ("current time ", ctime," ",xtime);

int newtime=(xtime)+(wtime); //current time + time to wait
//Print ("newtime ",newtime);

handle=FileOpen("timefile",FILE_BIN|FILE_READ); //open external file
stime=FileReadInteger(handle,LONG_VALUE); //read stored time in file
FileClose(handle); //close file
//Print ("stored time ",stime);
//Print ("read from file error ",GetLastError());

{


if (xtime > stime) //if current time greater than stored time

//Print ("time reached ",xtime," ",stime);
ftime = (xtime+wtime); //add waiting time to current time = future time
}

//if (xtime > stime) Print ("ftime ",ftime);
if (xtime > stime) handle=FileOpen("timefile",FILE_BIN|FILE_WRITE); //if current time greater tha stored time
if (xtime > stime) FileWriteInteger(handle, ftime); //open and write future time
if (xtime > stime) FileClose(handle);
Print ("write to file error ",GetLastError());

if (xtime > stime) Alert ("time reached ",stime); //if future time is reached process ........

Comment("Order Open: ",OrderOpenPrice(), //comments ....
"\ncurrent time ", xtime,
"\nfuture time ", ftime,
"\nstored time ",stime
);

} //end prog
Reason: