start a function when the market is closed? - page 2

 
szgy74:

There are 48 hours in a weekend. I think you can easily calculate and check and wait for a timestamp when the markets are closed.

The TimeLocal() returns the actual time (computer) even in weekends.

No, without an incoming tick (or a restart, which invokes init()) the terminal isn't doing anyting but waiting.

And if you have a function call weekendWork(,,sleep(); ..) in start() you will stay 'sleeping' in that function and will miss all the other action of the market.

 
szgy74: depending on how many bars are there in the history we can simply find last weekend start and end. (if we assume markets are always closed on the weekend.)

Can't depend on how many bars. What happens on Christmas when the market is closed all day?

There are not 48 bars over the weekend even ignoring Xmas and other market holidays.

You can compute when the market will close (ignoring Xmas.) But you can't know exactly which tick.

 
Thirteen: The following code runs once per week (or once after the EA is initialized):

You are assuming the market open Sunday. Does only for GMT(0) and in winter GMT(-1) - GMT(-12) brokers. There is no Sunday for GMT(+1)-GMT(+12)

You can simply use

static int DOWcurr;
int DOWprev = DOWcurr; DOWcurr = TimeDayOfWeek(Time[0]);
bool newWeek = DOWcurr < DOWprev;
 
szgy74:

There are 48 hours in a weekend. I think you can easily calculate and check and wait for a timestamp when the markets are closed.

The TimeLocal() returns the actual time (computer) even in weekends.

RaptorUK:
Programmatically it's not a simple thing to do, I have a solution but it can easily be thrown off by missing history data during a BackTest.

Will your solution handle missing bars in the history data when you are back testing ?
 
WHRoeder:

Can't depend on how many bars. What happens on Christmas when the market is closed all day?

You can compute when the market will close. But you can't know exactly which tick.


On how many bars I meant 5000 bars in M1 timeframe is not enough to find start and end for a week (which is ~7200 M1 bars).

I tried to suggest that it is not necessary to know which is the last tick when we know what's the time.

 
gooly:

No, without an incoming tick (or a restart, which invokes init()) the terminal isn't doing anyting but waiting.



That is why I suggested the use of a tick sender software. https://forum.mql4.com/27100

 
WHRoeder:

There are not 48 bars over the weekend even ignoring Xmas and other market holidays.

I meant plenty of time, not 48 bars, of course there are no bars on weekend (market weekend, not broker weekend).

 
gooly:
I even can't start a function that sleeps for several hours, because first the control won't come back to start() and without ticks the terminal doesn't check the 'sleep-counter' so that this function will continue only right after that market has reopened. :(

How about changing things in the sleeper? First you wait for some time and then check whether the market is still open or not.

This way you have the incoming tick and the execution may continue after market close.


some kind of pseudocode:


start()

{

Sleep(for some time);

if (market_open()) { do nothing; }

if (!market_open()) { it's weekend, calculate what you want; }

}

 
WHRoeder:

You are assuming the market open Sunday. Does only for GMT(0) and in winter GMT(-1) - GMT(-12) brokers. There is no Sunday for GMT(+1)-GMT(+12)

You can simply use

static int DOWcurr;
int DOWprev = DOWcurr; DOWcurr = TimeDayOfWeek(Time[0]);
bool newWeek = DOWcurr < DOWprev;

First, my code does not assume that the market opens only on a Sunday. It assumes that the market will open no earlier than Sunday (i.e., Saturday) and assumes the market may open on or later than Sunday (i.e., Monday, Tuesday, Wednesday, Thursday, or Friday). Hence, the use of the ">=" operator:

if (today >= StartOfWeek) { ... }

Second, it appears (and I could be wrong, so please correct me if I am) that your code needs to cycle through a weekend before it determines there is a new week. For example, the following code was run in the Tester from 07/01/2013 to 08/03/2013:

int start() {
   static int DOWcurr;
   int DOWprev = DOWcurr; DOWcurr = TimeDayOfWeek(Time[0]);
   bool newWeek = DOWcurr < DOWprev;
   if (newWeek)
      Print ("New Week: ", DOWcurr, " < ", DOWprev);

   return(0);
}

OncePerWeek test #2

This test seems to show that newWeek was false on Monday, July 01, 2013, and only became true on the first tick of Monday, July 08, 2013.

Third, if I may ask, why are you using TimeDayOfWeek() rather than DayOfWeek()?

 
szgy74:

That is why I suggested the use of a tick sender software. https://forum.mql4.com/27100





Ok, you are right! That is definitely a workaround.
A terminal started with very exotic market chart just waiting for the weekends and the tick sender sending single ticks every 5 hours - looks funny and I assume this was not the intention of tick sender but at least it should work.

Thanks for that idea.