start a function when the market is closed?

 

Hi,

the idea: start a function on Saturday or Sunday when the market is closed that e.g. investigates the performance of a (each single) system or of all together in order to e.g. change the parameter that e.g. calculates the lotsizes for the next week.

The start()-function is started at each incoming tick. But Sa. and Su. no tick will wake up that function.

Is there another possibility (time-controlled) to start a function other than a tick (=> start()) or a re-start (=>init()) of the whole MT4-terminal?

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. :(


Gooly

 
gooly:

Hi,

the idea: start a function on Saturday or Sunday when the market is closed that e.g. investigates the performance of a (each single) system or of all together in order to e.g. change the parameter that e.g. calculates the lotsizes for the next week.

The start()-function is started at each incoming tick. But Sa. and Su. no tick will wake up that function.

Is there another possibility (time-controlled) to start a function other than a tick (=> start()) or a re-start (=>init()) of the whole MT4-terminal?

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. :(


Gooly

You have to decide when the market is closing and capture a tick before it does, then sleep for a while then run your function, the sleep will allow time for the market to actually close so when your function starts it is doing so when the market has closed.
 
gooly:
the idea: start a function on Saturday or Sunday when the market is closed that e.g. investigates the performance of a (each single) system or of all together in order to e.g. change the parameter that e.g. calculates the lotsizes for the next week.
Why not just recalculate what you want when you get the first tick of the new week? KISS.
 

Well,

to me it would be the better solution to split trading and a backward analyse.

Beside that the possibility to start a function by time and not by tick would offer a wide range of things to be done in the spare time by the terminal.

You don't have to have an if(..) {..} at every tick 24/5 only to catch the last or the first tick of the week.

Gooly

 

you can use some external ticksender software to trigger start() function execution.

You can find out whether the market is closed with functions like TimeCurrent, TimeLocal, TimeDayOfWeek, etc.

(the difference of local and server time changes noticably on weekends)

 
maybe you will find some useful info here: https://www.mql5.com/en/articles/1467
 
WHRoeder:
Why not just recalculate what you want when you get the first tick of the new week? KISS.
gooly:

Well,

to me it would be the better solution to split trading and a backward analyse.

Beside that the possibility to start a function by time and not by tick would offer a wide range of things to be done in the spare time by the terminal.

You don't have to have an if(..) {..} at every tick 24/5 only to catch the last or the first tick of the week.

I happen to agree with WHRoeder. It is probably better (and may be easier) to recalculate upon the first tick of the new week. By the way, how do you know when the last tick of the week will happen?

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

datetime StartOfWeek = 0;

int init() {
   StartOfWeek = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day())) - (DayOfWeek() * 86400);
        
   return (0);
}

int start() {
   // the following code executes once per week (and once when the EA is reinitialized)
   datetime today = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day()));
   if (today >= StartOfWeek) {
      // put calculation code here
      Print ("Current Sunday = ", TimeToStr(StartOfWeek), " and Next Sunday = ", TimeToStr(StartOfWeek+604800));
      StartOfWeek += 604800;
   }
        
   return (0);
}

OncePerWeek Test

The above code is only an example. There are several ways to achieve the same result.

 
Thirteen:

By the way, how do you know when the last tick of the week will happen?



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.)

to be sure we can check last tick date.

 
Thirteen:

By the way, how do you know when the last tick of the week will happen?

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.
 

I am still not convinced, waiting for the last or the first tick of the week makes me still depending on the server of the broker and still needs for the one and only moment that if()..

Last week I realized a 'gap' of ticks for 90 sec - but of course the bar that I missed was beautifully drawn.

What if the server after the weekend fails to come up...

I realize there is no other solution but the start-by-tick.

Is there a wish-list for mt4?

Gooly

 

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.

Reason: