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: candles, endofday, getaggregationperiod, intraday, strategies, Thinkscript
March 30, 2009 at 9:43 am
Excellent. Thank you!
April 17, 2009 at 7:50 pm
I am curious on how discover these hidden functions of ThinkScript. Is there a manual somewhere which documents these?
April 17, 2009 at 7:56 pm
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.
July 29, 2009 at 7:17 pm
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…
July 29, 2009 at 8:17 pm
Sure. I’ll put something together when I get a chance.
July 30, 2009 at 6:07 pm
Supercool…
As “non-technical hobby indicator designer” I just canĀ“t figure it out despite of endless hours experimenting.
July 30, 2009 at 6:12 pm
If you don’t see it within a week or so, remind me.