Let’s say you want to carve out a special session of data from the 24-hour data session – maybe keep track of the highest high and lowest low from 9:00 p.m. to 4:00 p.m. the next day. How would you do it?
To start with you would need to reset the highest high and lowest low values each day. So you could say if the current bars time > StartTime and the prior bars time <= StartTime then you know the first bar of your specialized session has started. So far so good. If the time falls outside the boundaries of your special session then you want to ignore that data -right? What about this:
If t >StartTime and t <= EndTime then…
{Remember EasyLanguage uses the end time stamp for its intraday bars}
Sounds good. But what happens when time equals 2300 or 11:00 p.m.? You want to include this time in your session but the if-then construct doesn’t work. 2300 is greater than 2100 but it’s not less than 1600 so it doesn’t pass the test. The problems arise when the EndTime < StartTime. It really isn’t since the EndTime is for the next day, but the computer doesn’t know that. What to do? Here is a quick little trick to help you solve this problem: use a special offset if the time falls in a certain range.
EndTimeOffset = 0 ;
If t >=StartTime and t <= 2359 then EndTimeOffset= 2400 – EndTime;
Going back to our example of the current time of 2300 and applying this little bit of code our EndTimeOffset would be equal to 2400 – 1600 or 800. So if t = 2300, you subtract 800 and get 1500 and that works.
2300 – 800 = 1500 which is less than 1600 –> works
What if t = 300 or 3:00 a.m. Then EndTimeOffset = 0; 300 – 0 is definitely less than 1600.
That solves the problem with the EndTime. Or does it? What if EndTime is like 1503? So you have 2400 – 1503 which is something like 897. What if time is 2354 and you subtract 897 you get 1457 and that still works since its less than 1503. Ok, what about if EndTime = 1859 then you get 2400 – 1859 which equals 541. If time = 2354 and you subtract 541 you get 1843 and that still works.
Is there a similar problem with the StartTime? If t = 3:00 a.m. then it is not greater than our StartTime of 2100, but we want it in our window. We need another offset. This time we want to make a StartTime offset equal to 2400 when we cross the 0:00 timeline. And then reset it to zero when we cross the StartTime timeline. Let’s see if it works:
t = 2200 : is t > StartTime? Yes
t=0002 : is t > StartTime? No, but should be. We crossed the 0000 timeline so we need to add 2400 to t and then compare to StartTime:
t + 2400 = 2402 and it is greater than StartTime. Make sense?
Probably not but look at the code:
Here is an the indicator code that calls the function:
Hope this helps you out. I am posting this for two reasons: 1) to help out and 2) prevent me from reinventing the wheel every time I have to use time constraints on a larger time frame of data.
StartTimeWindow = 2300
EndTimeWindow = 1400
Time = 2200, FALSE
Time = 2315, TRUE [2315 > 2300 and 2315 – (2400 -1400) <1400)]
This code should work with all times. Shoot me an email if you find it doesn’t.