EA start and stop hour + GMToffset - page 2

 
deVries:


No, take your time reading this topic

read this twice if one time is not enough

and do it again if reading twice is not enough

... the answer why not is in this topic !!


You mean he should be MathMod'ing for 24 right?

But I still want to know whats wrong with Hour(). I forgot :)

 
alladir: But I still want to know whats wrong with Hour(). I forgot :)

Documentation:

int Hour( )
Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).

Note: At the testing, the last known server time is modelled.

When you launch terminal, Hour() stores the hour of the last_time a tick was received from the broker. This hour_value wouldn't change while the terminal is opened. Last_known_server_time may_not technically be the last time a tick was received however that's the easiest way I can think of explaining it.

 
ubzen:

Documentation:

int Hour( )
Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).

Note: At the testing, the last known server time is modelled.

When you launch terminal, Hour() stores the hour of the last_time a tick was received from the broker. This hour_value wouldn't change while the terminal is opened. Last_known_server_time may_not technically be the last time a tick was received however that's the easiest way I can think of explaining it.

I don't think I understand what you actually mean. The way I read what you quoted and wrote, you are saying that: Hour() stores the hour of the last time a tick was received by the terminal and Hour() doesn't change while the terminal is open. If that is what you meant to say and meant to convey (please correct me if I misread what you said), then, respectfully, I believe you might be mistaken.

Hour() returns a integer which represents the hour component of the last known server time. If the last known server time is 10:59, then Hour() returns 10. If the last known server time becomes 11:00 on the next tick, then Hour() returns 11.

I believe the documentation is vague due to translation or has been mistranslated. I believe "the moment of the program start" means the moment start() begins executing and "the time of the program execution" means during the execution of start(). This understanding would be congruent with the behavior that any incoming ticks are ignored while start() is executing. EDIT: This view seems to be conveyed in the Book:

Seconds (), Minute(), Hour(), Day(), TimeMonth(), TimeYear(), DayOfWeek () and DayOfYear() Functions

This is the group of functions that return the current second, minute, hour, day, month, year, day of week and day of year for the last known server time. The last known server time is the server time that corresponds with the moment of the program launch (launch of any special function by the client terminal). The server time is not changed during the execution of the special function.

I use Hour() is my EA without any problems, even though some have suggested (see, for example, here) that Hour() hasn't performed correctly in the Strategy Tester in the past. Currently (build 509), Hour() works correctly in the Strategy Tester.

Here is a small test in the Strategy Tester for 09/11/2013:

int start() {
   static int cHour = -1;
   if (Hour() != cHour) {
      Print ("Current Hour is: ", Hour());
      cHour = Hour();
   }

   return(0);
}

Hour Test #1

 
alladir:


You mean he should be MathMod'ing for 24 right?


i was meaning this

RaptorUK:
Where you have Hour() replace it with TimeHour( TimeCurrent() )



first part only, is this enough.?

if(TimeHour(TimeCurrent())>=FromHour+GMToffset && Hour()<=ToHour+GMToffset) return(0);

As you can see in the message of RaptorUK: what was replied

was not written only first part but Where you have Hour()

So NO how can you ask this question ... with Replying the answer

And yes also it's not TimeHour(TimeCurrent())

it has to the right timezone so you see GMT

if( TimeHour(TimeCurrent()+ ((  0 - GMToffset) * 3600)) >= FromHour  && TimeHour(TimeCurrent()+ ((  0 - GMToffset) * 3600)) < ToHour && ToHour>FromHour) return(0);

in other case

if(( TimeHour(TimeCurrent()+ ((  0 - GMToffset) * 3600)) >= FromHour  || TimeHour(TimeCurrent()+ ((  0 - GMToffset) * 3600)) < ToHour) && ToHour<FromHour) return(0);

...

that might be like something you see here

 

As Thirteen has pointed out, I don't see why Hour() wouldn't work

Maybe the problem is elsewhere in the code?

int extern FromHour = 8;
int extern ToHour = 14;
int extern GMToffset = 1;

if(Hour()>=FromHour+GMToffset && Hour()<=ToHour+GMToffset) return(0);

Why does the code return(0) when the if condition is met?

int extern FromHour = 8; // does this make a difference?

//Should be 

extern int FromHour = 8;
 
GumRai:

. . . I don't see why Hour() wouldn't work

Maybe the problem is elsewhere in the code?

I believe the code does work. The code below . . .

int start() {

   int FromHour = 2;
   int ToHour = 4;
   int GMToffset = 1;

   if (Hour() >= FromHour + GMToffset && Hour() <= ToHour + GMToffset)
      Print ("Server Time is ", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), ".  The current hour, ", Hour(), ", is >= ", FromHour + GMToffset, " and <= ", ToHour + GMToffset);

   return(0);
}

. . . produces the following output in the journal/log:

Hour Test #2

So, I think the OP needs to be more specific about what doesn't work and probably needs to post more of his/her code. Also, as GumRai has pointed out, the OP's original code doesn't compile because the extern needs to come before the int declaration.

extern int FromHour = 2;
extern int ToHour = 4;
extern int GMToffset = 1;

int start() {
   if (Hour() >= FromHour + GMToffset && Hour() <= ToHour + GMToffset)
      Print ("Server Time is ", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), ".  The current hour, ", Hour(), ", is >= ", FromHour + GMToffset, " and <= ", ToHour + GMToffset);

   return (0);
}

The above code produces the same output as above.

 

@Thirteen: I stand corrected. It was one of those things which was changed over time. It is modeled correctly in the tester now. When I went through the book and used Hour() [in strategy tester] it give me the hour from the last Friday or when I disconnected the terminal. Therefore I stopped using it. I'm obviously not alone in this. Just what I'm hardwire to remember. lol.

Added: **I cannot help but say it** If metaQuotes would put something as simple as Below on the Documentation page. Perhaps there wouldn't be such contradiction between Old_School and New_School on this forum. With all the new changes to come, I imagine its just gonna get worse.

  • 12.31.2012: Hour() is now correctly modeled within the strategy tester.

Sure they post a [ MetaTrader 4 Client Terminal Built ], but those are posts which floats for about a Week and its harder to remember which change came within which built.

 
GumRai:

As Thirteen has pointed out, I don't see why Hour() wouldn't work

Maybe the problem is elsewhere in the code?

Why does the code return(0) when the if condition is met?

I know TimeHour(time) works so I suggested what does work rather than something which I know has caused issues in the past . . . perhaps the OP was having problems live and not in the Strategy Tester. What does "Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution)." mean ? is the program execution the execution of the whole EA/Indicator or just start() ?
 

Thank you guys !

I run BT in mt4 v 4.8.2 and it works also with hour() function aslong I did not add "+GMToffset"

this one is working well also if GMToffset changed

if(TimeHour(TimeCurrent()) >= FromHour + GMToffset && TimeHour(TimeCurrent()) <= ToHour + GMToffset) return(0);
 
RaptorUK:
I know TimeHour(time) works so I suggested what does work rather than something which I know has caused issues in the past . . . perhaps the OP was having problems live and not in the Strategy Tester. What does "Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution)." mean ? is the program execution the execution of the whole EA/Indicator or just start() ?

I use this code in my EAs to find the current server time for checking session times and also to update a label so that I can always see the current server time. I have not had any issues and it updates the time every tick. If I use the EA in the strategy tester, visual mode, the time is updated with no problems.

double min=Minute();
   double minD= min/100;
   double servertime = Hour() + minD;    //---Get time as a decimal-----

I tested this bit of code and left it running

if (BarStartTime == Time[0])
      return(0);
   BarStartTime=Time[0];
   Print("Hour is ",Hour() );
   Print("TimeHour is ",TimeHour(TimeCurrent()) );

00:01:25 Hour EURUSD,H1: Hour is 17

00:01:25 Hour EURUSD,H1: TimeHour is 17

01:01:33 Hour EURUSD,H1: Hour is 18

01:01:33 Hour EURUSD,H1: TimeHour is 18

02:01:42 Hour EURUSD,H1: Hour is 19

02:01:42 Hour EURUSD,H1: TimeHour is 19

03:01:50 Hour EURUSD,H1: Hour is 20

03:01:50 Hour EURUSD,H1: TimeHour is 20

I don't doubt that there have been issues in the past. The question is whether we can assume that those issues have now been resolved in MT4 now?

Reason: