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.