Thinkscript Tip: GetAggregationPeriod

In Thinkscript, there is a function called GetAggregationPeriod(). This function returns the current aggregation period in milliseconds. The aggregation period is defined as the number of milliseconds it takes to complete a candle on the current chart timeframe. Here are the aggregation period values for the different ToS chart timeframes:

Any Tick Chart = 0
1 min = 60,000
2 min = 120,000
3 min = 180,000
4 min = 240,000
5 min = 300,000
10 min = 600,000
15 min = 900,000
20 min = 1,200,000
30 min = 1,800,000
1 hr = 3,600,000
2 hr = 7,200,000
4 hr = 14,400,000
Daily = 86,400,000
Weekly = 604,800,000
Monthly = 2,592,000,000

There are a few built in constants for some of these aggregation periods, such as Aggregationperiod.MIN and Aggregationperiod.DAY. These constants would return the values for 1 min and Daily from the list above. But as you can see, there are many available values, more than there are defined constants.

If you want to tell the difference between intraday and end of day charts, you simply check if the GetAggregationPeriod() function returns a value of 86,400,000 or more. If so, it’s daily or greater. Less than that, it’s an intraday chart. You can also use it in any script that needs to know the duration of a candle. Just call the function and divide the answer by 60,000 to get the number of minutes, or divide by 1,000 to get the number of seconds.

In my earlier usage of GetAggregationPeriod in strategies, I used it incorrectly to try to indentify the differences in intraday and end of day charts. I was trapping specific values of aggregation period instead of just checking for daily or above. My check for the market close was also messed up; it only worked right if you showed aftermarket data. I will be updating all of my strategy codes over time to calculate these the correct way.

The following code will get the aggregation period, and check if that value is equal to or greater than the value for a daily candle. If it is, then the market is always considered to be “open” as far as the study/strategy is concerned. If the aggregation period value is less than the value for a daily candle, then we know we are on an intraday chart and the check is made to see if the market is open or not:

input opentime=0930;
input closetime=1600;
def AP=getaggregationperiod();
def daily=if AP>=aggregationPeriod.DAY then 1 else 0;
def isopen=if daily then 1 else if secondsFromTime(opentime)>=0 and secondstillTime(closetime)>=0 then 1 else 0;

Then you can use isopen as a boolean to check if the market is open before entering a strategy trade.

The next code will use aggregation period to check if we are on the last candle of the market session, which is useful for an “exit at close” strategy:

input closetime=1600;
def AP=getaggregationperiod();
def daily=if AP>=aggregationPeriod.DAY then 1 else 0;
def islastcandle=if daily then 0 else if secondstillTime(closetime)<=AP/1000 then 1 else 0;
addorder(islastcandle,close);

Note that this doesn’t really work for tick charts, where the candle is not time-based. If you do testing with tick charts, I recommend showing all data, not just market hours. If I figure out a better way, I’ll post it.

Tags: , , , , ,

10 Responses to “Thinkscript Tip: GetAggregationPeriod”

  1. Pupsor Says:

    Excellent. Thank you!

  2. AVIAT72 Says:

    I am curious on how discover these hidden functions of ThinkScript. Is there a manual somewhere which documents these?

  3. Prospectus Says:

    You’re reading it!

    Seriously, there’s only the monthly ToS software release notes, maybe an email to the ToS developers, and trial and error. Between my site and Thinkscripters, that’s about it that I’m aware of. Thinkscripter had a forum now, which might be a good place to learn as well.

  4. filip Says:

    Is there a way of using these constants to calculate the pivot of the last 4h? Something like getting the high of the last 14,400,000 milliseconds + the low & close/3…

  5. Prospectus Says:

    Sure. I’ll put something together when I get a chance.

  6. filip Says:

    Supercool…

    As “non-technical hobby indicator designer” I just can´t figure it out despite of endless hours experimenting. 😀

  7. Prospectus Says:

    If you don’t see it within a week or so, remind me.

  8. MJ Says:

    Hi, I’m not sure if you still keep up with this blog, but I was trying to add an end of the day exit criteria, so I used your exit at close method; unfortunately, it only works if I click “show overnight hours”- and this subsequently messes up my strategy. I used this with your isopen command, but isopen seems to be ignored by my strategy (it keeps entering orders in the afterhours). Any thoughts on how to fix this so it will work with “after hours” turned off? Thank you.

  9. parasshrestha74@gmail.com Says:

    Hi,

    I have two Charts.

    -Chart #1 –1536 Tick Candle Chart. (Chart Type – Candle and Aggregation Type Tick)

    -Chart #2 – 512 Tick Candle Chart. (Chart Type – Candle and Aggregation Type Tick)

    As you know right after three different 512 Tick Candle Chat has formed; than 1536 Tick Candle Chart forms.

    Can we program to (thinkorswim Script) so that once three 512 tick candle Charts have formed I want to shading from open to close the on Chart #2. (Basically shade 1536 time frames Tick Candle Chart on 512 Tick Candle chart) – No worry for Wicks or Shadows.

    I really appreciate and thank you for help and advice.

    Paras

  10. Angelo Motta Says:

    Your site has been a breath of fresh air. And every time I look to write a script my Google searches lead me back here.

    On a weekly chart, I am trying to determine if the last candle is closed or if it is still active. Trying to determine if the last candle or the one before it should be used in a comparison. The close time doesn’t make sense because there are 5 close times in a weekly candle. I was looking for a Today() function to determine if we are on Friday post close, Saturday, Sunday, or Monday pre-open.

    Any comments are appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: