Monday, November 17, 2008

Using Time Conditionals in thinkScript


This study is a very simple example of how to use the time functions in thinkScript. This will plot the close during market hours only (8:30am CT – 3:00pm CT) and it will plot 0 if outside of the time range. Please note that the closing bar is not the bar at 3:00pm CT (16:00 ET) rather, it is the last bar before that time. Thus, as you can see, the logic in evaluating the end function (which is the function that tells us when the market closes) excludes zero as the bar at 1600 isn’t included in the market hours. However, the begin function includes zero (greater than or equal to expression) as the market open does include the 8:30am CT bar (9:30am ET).

Please recall that the time functions in thinkScript only will work if you use 24-hour time format and it is stated in Eastern Time.

Note, for simplicity and layout purposes, this study has been coded as a lower study...

!!! FOR EDUCATIONAL PURPOSES ONLY !!!! PLEASE READ DISCLAIMER POST!

declare lower;

input begin_time = 930;

input end_time = 1600;


def begin = secondstilltime(begin_time);

def end = secondstilltime(end_time);

plot data = if(begin<=0 and end>0,close,0);

2 comments:

TenaciousG said...

Thanks for posting your scripts - very helpful. Have you attempted to reference a variable from a Thinkscript strategy that has triggered? For example, you want to reference a buy or sell price from an executed strategy to set a stop and/or target price?

Thanks again!

Unknown said...

So, if I want to scan for pre-maket volume between 7amEST and 929amEST would I simply do the following:

input begin_time = 700;
input end_time = 929;
def begin = secondstilltime(begin_time);
def end = secondstilltime(end_time);
plot data = if(begin<=0 and end>0,volume,0);

Can't test this is a Sunday but will check tomorrow am.

Thanks