Testing For Correct Time Window in EasyLanguage

In issue #1 of the 2019 Future Truth Magazine, George Pruitt proves a solution for the problem of 0:00 time in EasyLanguage. What problem is that?

Let's take a look at George's example. He proposes tracking the highest high and lowest low in the overnight session on an intraday bar chart. Let's say we want to track these values from 9:00 PM to 4:00 PM. Lets call this our desired window where we'll track the price action.

Obviously, we need to create two variables that will update these two values we wish to track on every bar between the times of 9:00 PM and 4:00 PM. Ok, that sounds simple enough. We can start out with the following code.

If ( Time > 2100 and Time <= 1600 ) then...

The above code looks to see if the current time is greater than 9:00 PM and less than 4:00 PM. But we have a problem.

What happens when time is 9:30 PM. Well, that would mean the logical test would look like this...

If ( 2130 > 2100 and 2130 <= 1600 ) then...

The first condition (2130 > 2100) is true but the second condition ( 2130 <= 1600) is not true. 2130 is not less than or equal to 1600. This is not what we want. The current time of 9:30 PM is within our desired window when we wish to track the highest high and the lowest low. So, there must is a problem with our end time logic.

This problem is due to the fact our desired window cross over to a new day. Thus, our clock resets with 0:00 to start the new day. This results in a logical problem for our if-then condition.

One way that I might handle this problem is not to track if Time is between a start time and end time but instead, track the number of bars to count to reach our end time. When 2100 rolls around start counting bars until you reach the appropriate number of bars to reach your end time. If you're trading a 5-minute chart, that would mean there are 12 bars per hour. There are seven hours within our desired window. Thus, we need to count 84 (12*7) bars to reach the end of our desired window.

This may work in most cases but it's not perfect. First, you have to be using time-based bars. Next, if you change the bar interval then the number of bars change as well. Finally, we must always assume there are no days where the market may be closed or interrupted during our desired window. Why? Because these interruptions would change the bar count throwing off our algorithm.

George proposes a different method that involves using an end-time offset value if the current time falls within a specific range. Here is the code.

EndTimeOffset=0;
If ( Time > 2100 and Time <= 2359 ) then
EndTimeOffset = 2400 - EndTime;

Well, we first see that our current time is greater than 2100 and less than the value 2359. So we calculate our EndTimeOffset to be 270. So, we if go back to our original example using our new end time we get the following.

If ( 2130 > 2100 and 270 <= 1600 ) then..

The first condition (2130 > 2100) is true and the second condition ( 270 <= 1600) is true. It works! Our condition evaluates to true and we continue to track our highest high and lowest low values within our desired window.

Of course, there is a similar issue when dealing with the start time. If the time is 1:00 AM (100) we are still within our desired window but, our evaluation will not work properly.

If ( 100 > 2100 and 100 <= 1600 ) then...

We can see the first condition (100 > 2100) will fail even though we are within our desired window. The solution is to set our StartTimeOffset value to 2400...

If ( Time < Time[1] ) then
   StartTimeOffset = 2400;

..and then compare it to our start time. But before we do that we also must reset StartTimeOffset when we cross the StartTime threshold. The code would look something like this.

If Time >= StartTime and Time[1] < StartTime then
   StartTimeOffset = 0;

With this information, we can build a function that can be used to calculate our two offset values: StartTimeOffset and EndTimeOffset. In the original article, George provides a function which returns the offset values to the caller. It is then up to the caller to add and subtract the appropriate offset values. I've decided to create a function which does everything for you.

The function is called, isTimeWithinWindow. This function takes your start time and end time as inputs. It returns a boolean value. Boolean TRUE if the current time is within the window or Boolean FALSE if the current time is outside the window.

Use of this function is demonstrated below.

if ( isTimeWithinWindow( StartTimeWindow, EndTimeWindow ) ) then
   Print( Date, " ", Time, " inside time window.")
Else
   Print( Date, " ", Time, " outside time window.");

You can simply call the isTimeWithinWindow within your strategy or indicator code to quickly determine if the current time falls within the desired window. The code is available to download.

Thanks Geroge for the helpful tip!

About the Author Jeff Swanson

Jeff is the founder of EasyLanguage Mastery - a website and mission to empowering the EasyLanguage trader with the proper knowledge and tools to become a profitable trader. Join our EasyLanguage FaceBook group to interact with other EasyLanguage traders! Click the FaceBook icon to join.

follow me on:
  • cb says:

    Thanks Jeff and George!

  • ashley says:

    rewrote the code to eliminate the function, and replace it with simple Boolean operators to the same job, here:Inputs:
    StartTimeWindow(2100),
    EndTimeWindow(1600);

    Variables:
    StartOffset(0),
    EndOffset(0);

    {if ( isTimeWithinWindow( StartTimeWindow, EndTimeWindow ) ) then
    Print( Date, ” “, Time, ” inside time window.”)
    Else
    Print( Date, ” “, Time, ” outside time window.”);}
    Vars:adjstart(0),adjend(0);

    adjstart=endtimewindow;
    adjend=starttimewindow;

    print(“time “,time:4:4);
    if (time>adjstart or time<adjend) then
    Print( Date, " ", Time, " MOD inside time window.")
    else
    Print( Date, " ", Time, " MOD outside time window.")

  • >