Download MetaTrader 5

Trendline always 10 bars long (problem with exchange closed times and weekends)

To add comments, please log in or register
Jagg
106
Jagg  

Hi,

I want to add a new trendline object to an intraday chart for the DAX for example. DAX trading hours are from 8:00-22:00 on my broker.

I want to draw trendlines (within a script) to that dax intraday chart which are always 10 bars long to the future from a time/object given.
So I can do this on a 24hr chart:

datetime x = WindowTimeOnDropped();
datetime TrendlineStart = x+(Period() * 60 * 3); // start trendline 3 bars away from x to future
datetime TrendlineEnd  = TrendlineStart+(Period() * 60 * 10); // make trendline 10 bars wide from actual timeframe used

But how can I handle the problem of that DAX chart which isn't tradable from 22:00-8:00? 

Example:
My x points to a time like '2015.09.15 21:00:00', using above code my TrendlineEnd would be '2015.09.16 10:00:00' and in this case the trendline wouldn't be 10 bars long but only 3 bars. Or other example when x points to Friday 21:00, then the trendline can't be seen because of the weekend.

Is there a solution for this?

Thanks 

Alain Verleyen
Moderator
31439
Alain Verleyen  

You will have to calculate your dates taking into account the missing bars. No other way. If your broker change the server time (DST or whatever), you have to take it into account.

The easiest solution is to set 2 inputs for start (08:00) and end trading session (22:00), then calculate your dates from there :

Pseudo code : if Hour(TrendLine)>SESSION_END then trendline=SESSION_START+(Hour(Trendline)-SESSION_END)

etc...

Jagg
106
Jagg  

Ok, I came up with "a monster like that" which produces the correct 10 bars trendline in 99% of all cases (not considering DST changes for example)

datetime x = WindowTimeOnDropped();
datetime TrendlineStart = x+(Period() * 60 * 3); // start trendline 3 bars away from x to future
              
int h_trendline_start = TimeHour(TrendlineStart);

if (h_trendline_start > 19) { 
   TrendlineStart+=13*60*60; // add 13 hours
   h_trendline_start = TimeHour(TrendlineStart);
} else if (h_trendline_start < 8) { 
   TrendlineStart+=(8-h_trendline_start+8)*60*60; // add x hours +3 for 22/23/24 o'clock
   h_trendline_start = TimeHour(TrendlineStart);
}

if (TimeDayOfWeek(TrendlineStart)== 0) { // 0 Sunday
   TrendlineStart+=24*60*60; // add 1 day
}
else if (TimeDayOfWeek(TrendlineStart)== 6) { // 6 Saturday
   TrendlineStart+=24*2*60*60; // add 2 days
}
else if ((TimeDayOfWeek(TrendlineStart)== 5) && (h_trendline_start > 12)) { // Friday > 12:00
   TrendlineStart+=24*3*60*60; // add 3 days
}

datetime TrendlineEnd = TrendlineStart+(Period() * 60 * 10); // make trendline 10 bars wide from actual timeframe used
int h_trendline_end = TimeHour(TrendlineEnd);

if (h_trendline_end < 8) { 
   TrendlineEnd+=(8-h_trendline_end+3)*60*60; // add x hours +3 for 22/23/24 o'clock
   h_trendline_end = TimeHour(TrendlineEnd);
}
else if (h_trendline_end > 21) { 
   TrendlineEnd+=(24-h_trendline_end+8)*60*60; // add hours
   h_trendline_end = TimeHour(TrendlineEnd);
}

if (TimeDayOfWeek(TrendlineEnd)== 0) { // 0 Sunday
   TrendlineEnd+=24*60*60; // add 1 day
}
else if (TimeDayOfWeek(TrendlineEnd)== 6) { // 6 Saturday
   TrendlineEnd+=24*2*60*60; // add 2 days
}
else if ((TimeDayOfWeek(TrendlineEnd)== 5) && (h_trendline_end > 21)) { // 5  Friday > 21:00
   TrendlineEnd+=24*3*60*60; // add 3 days
}

 (hour times are still hardcoded to DAX times in that code, so I will add new external parameters for SESSION_START/SESSION_END times and/or a check to Symbol() DAX

Alain Verleyen
Moderator
31439
Alain Verleyen  
Jagg:

Ok, I came up with "a monster like that" which produces the correct 10 bars trendline in 99% of all cases (not considering DST changes for example)

 (hour times are still hardcoded to DAX times in that code, so I will add new external parameters for SESSION_START/SESSION_END times and/or a check to Symbol() DAX

Congratulations
To add comments, please log in or register