The AggregationPeriod constant in thinkScript returns the chart’s time frame that the script calculates on.
For example, the four days chart would be represented by AggregationPeriod.FOUR_DAYS. Similarly, a 5-minute chart is presented by AggregationPeriod.FIVE_MIN.
Here’s the complete list of AggregationPeriod variables to use when creating custom indicators for ThinkorSwim.
thinkScript AggregationPeriod Values
| Chart time frame | AggregationPeriod value |
|---|---|
| 1 minute | MIN |
| 2 minutes | TWO_MIN |
| 3 minutes | THREE_MIN |
| 4 minutes | FOUR_MIN |
| 5 minutes | FIVE_MIN |
| 10 minutes | TEN_MIN |
| 15 minutes | FIFTEEN_MIN |
| 30 minutes | THIRTY_MIN |
| 1 hour | HOUR |
| 2 hours | TWO_HOURS |
| 4 hours | FOUR_HOURS |
| 1 day | DAY |
| 2 days | TWO_DAYS |
| 3 days | THREE_DAYS |
| 4 days | FOUR_DAYS |
| 1 week | WEEK |
| 1 month | MONTH |
| Option Expiration | OPT_EXP |
| 1 Quarter | QUARTER |
| 1 Year | YEAR |
An aggregation period is the number of seconds in a time frame. For example, HOUR is 60 minutes or 3,600 seconds.
Here’s how to define an aggregation period in your script.
def agg_period = AggregationPeriod.DAY;
plot data = close(period = agg_period);
In the example above, I defined the DAY (1 day) time frame as my aggregation period. Then I plot the daily closing price of the stock onto my chart.

Notice how my ThinkorSwim chart is set to the 1H (one-hour) timeframe. In addition to the standard candles from the 1H timeframe, there is also a blue line. That line is the daily closing price of the stock based on the script above.
AggregationPeriod Examples
To better understand the time frame period variable in thinkScript, let’s start with this indicator.
## Archive Name: Label-Chart Aggregation Length and Detail_v04_JQ
## Archive Section: Labels
## Suggested Tos Name: ChartAggLengthDetail_v04_JQ
## Archive Date:
## Archive Notes:
# 04.19.2018 added Length and barNumber Bars JohnnyQuotron
# 10.10.2018 adjusted label to eliminate repetition
# 10.16.2018 adjusted label to eliminate repetition
# This label relies on code from StanL as posted in his Snippets. Thank you Stan for all your efforts
# The purpose of this script is to create a label that will remind the user of both Aggregation and chart length
# Aggregations .. not needed for this script but helpful to have around
#Defines aggregation period equal to one minute (60,000 milliseconds).
#Defines aggregation period equal to one hour (3,600,000 milliseconds).
#Defines aggregation period equal to one day (86,400,000 milliseconds).
#Defines aggregation period equal to one week (604,800,000 milliseconds)
#Defines aggregation period equal to one month (2,592,000,000 milliseconds).
#Defines aggregation period equal to option expiration (2,678,400,000 milliseconds).
#Defines aggregation period equal to one calendar quarter (7,776,000,000 milliseconds).
#Defines aggregation period equal to one calendar year (31,536,000,000 milliseconds).
# Script loosely based on: Days On Chart _Mobius
# If the script is broken or spewing crazy results blame me not Mobius.
# His script worked great until I hacked it to death
def RTHstart = SecondsFromTime(0930) == 0;
def daysOnChart = if RTHstart then daysOnChart[1] + 1
else daysOnChart[1];
def AggPeriod = GetAggregationPeriod();
#> Aggregation Identification Label
AddLabel(yes, (" " +
if AggPeriod == AggregationPeriod.MIN then "1 Min"
else if AggPeriod == AggregationPeriod.TWO_MIN then "2 Min"
else if AggPeriod == AggregationPeriod.THREE_MIN then "3 Min"
else if AggPeriod == AggregationPeriod.FOUR_MIN then "4 Min"
else if AggPeriod == AggregationPeriod.FIVE_MIN then "5 Min"
else if AggPeriod == AggregationPeriod.TEN_MIN then "10 Min"
else if AggPeriod == AggregationPeriod.FIFTEEN_MIN then "15 Min"
else if AggPeriod == AggregationPeriod.TWENTY_MIN then "20 Min"
else if AggPeriod == AggregationPeriod.THIRTY_MIN then "30 Min"
else if AggPeriod == AggregationPeriod.HOUR then "1 Hour"
else if AggPeriod == AggregationPeriod.TWO_HOURS then "2 Hour"
else if AggPeriod == AggregationPeriod.FOUR_HOURS then "4 Hour"
else if AggPeriod == AggregationPeriod.DAY then "Daily"
else if AggPeriod == AggregationPeriod.TWO_DAYS then "2 Day"
else if AggPeriod == AggregationPeriod.THREE_DAYS then "3 Day"
else if AggPeriod == AggregationPeriod.FOUR_DAYS then "4 Day"
else if AggPeriod == AggregationPeriod.WEEK then "Weekly"
else if AggPeriod == AggregationPeriod.MONTH then "Monthly"
else "Use time charts only") + " Aggregation ",
Color.WHITE);
#> Time on Chart Label
addlabel( aggPeriod <= 86400000, (daysOnChart -1 ) + " Days on Chart ", color.WHITE);
addlabel( aggPeriod == 604800000, (daysOnChart -1 ) + " Weeks on Chart ", color.WHITE);
addlabel( aggPeriod == 2592000000, (daysOnChart -1 ) + " Months on Chart ", color.WHITE);
# End Code
The code above plots two labels on your ThinkorSwim chart. As you shift through different time frames, it will display the current aggregation period and time interval.

Create a Multi-Time Frame Indicator
With the help of AggregationPeriod, we can create a multi-timeframe indicator for ThinkorSwim.
The example indicator below calculates and plots the daily simple moving average.
We use the AggregationPeriod variable to ensure that the requested data is from the Daily (DAY) time frame, not the current time frame on our chart.
def agg_period = AggregationPeriod.DAY;
def data = close(period = agg_period);
input length = 20;
plot Daily_SMA = Average(data, length);
Daily_SMA.SetDefaultColor(GetColor(1));
That said, the daily simple moving average line is still visible when viewing the 5-min chart.

The magenta line illustrates the current 20-period simple moving average of the 5-min chart. On the other hand, the cyan line is the 20-period simple moving average of the DAY (daily) chart.
Weekly RSI Indicator on Daily Chart
The script below is the RSI indicator. It doesn’t calculate the RSI value on the chart’s time frame.
Instead, it uses the time frame we configure with AggergrationPeriod. In this case, it’s set to WEEK (weekly).

declare lower;
def agg_period = AggregationPeriod.WEEK;
def close = close(period = agg_period);
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
def price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;
def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else if RSI < over_Sold then RSI.color("OverSold") else RSI.color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
AggregationPeriod Switcher
Our examples thus far have defined a fixed value for the aggregation period. While there’s nothing wrong with that method, adjusting the time frame is harder for the end user.
What if someone needs to switch the time frame to another aggregation period other than the default value?
To allow user input, we apply the input reserved word to AggregationPeriod.
input agg_period = AggregationPeriod.DAY;
plot data = close(period = agg_period);
All you had to do was replace def with input. Therefore, it creates a time frame drop-down menu in the indicator settings.
Now the script is flexible. You can quickly switch between different AggregationPeriod values without touching the code.

When you don’t specify a particular aggregation period in your thinkScript code, ThinkorSwim automatically calculates based on data from the current chart’s time frame.
If your calculation requires a different time frame, you must define it using the AggregationPeriod constant.
Happy testing!
Great post Melvin!
I was trying to show a Monthly aggregation period on a 1 min chart and its not working, the highest i can show is the Weekly aggregation period. My day trading 1 and 5 min charts consist of many levels which include: Previous day Close (shows perfectly), Premarket High and Low (also shows perfectly), previous day and week high low (all good) and i was looking to add the monthly but for some reason on lower time frames its not showing do you know why is this?
Make sure you have at least 30 days of data shown on your 1 min chart to have the Monthly aggregation period working.
Thank you Merlin. Very wise information, but I still haven’t found the answer to my question:
Please tell me how to correctly finish the script:
Draw a horizontal line from the last earnings on the W, M timeframes. The vertical coordinate is the maximum price of the next bar from the last earnings on the daily timeframe?
code:
# ““““““ select AggregationPeriod “““““““““““““““““““`
#
def tf_now = GetAggregationPeriod() ;
def is_tf_day = ( tf_now == AggregationPeriod.DAY ) ;
def is_tf_week = ( tf_now == AggregationPeriod.WEEK ) ;
def is_tf_month = ( tf_now == AggregationPeriod.MONTH ) ;
AddLabel( is_tf_day, ” Day TF “, Color.LIME, location = Location.TOP_RIGHT, size = FontSize.MEDIUM);
AddLabel( is_tf_week, ” Week TF ” , Color.VIOLET, location = Location.TOP_RIGHT, size = FontSize.MEDIUM);
AddLabel( is_tf_month , ” Month TF “, Color.CYAN, location = Location.TOP_RIGHT, size = FontSize.MEDIUM);
def period = AggregationPeriod.DAY;
# def hd = high(period = period)[-1];
def hd = high(period = period);
def is_real_hd = !IsNaN(hd) and IsNaN(hd[-1]);
# def isEarnings = HasEarnings();
# def earningsDate = if isEarnings then GetYYYYMMDD() else earningsDate[1];
# def isDay = GetAggregationPeriod() == AggregationPeriod.DAY;
def earningsOffset = GetEventOffset(Events.EARNINGS);
def earningsHigh = fold i = 0 to 100 with price = Double.NaN do
if is_tf_day and i == earningsOffset then high[i] else price;
rec storedEarningsHigh = if !IsNaN(earningsHigh) then earningsHigh else storedEarningsHigh[1];
plot EarningsLine = if (is_tf_week or is_tf_month) and !IsNaN(storedEarningsHigh) then storedEarningsHigh else Double.NaN;
EarningsLine.SetDefaultColor(Color.ORANGE);
EarningsLine.SetLineWeight(2);
EarningsLine.SetStyle(Curve.SHORT_DASH);
AddLabel( !IsNaN(storedEarningsHigh) , ” Earnings Day High: ” + storedEarningsHigh, Color.YELLOW,
location = Location.TOP_LEFT, size = FontSize.MEDIUM);
# end .
Good LUCK.
I am looking to add to the aggregation period for the one hour. I understand after typing the aggregation period for open and example the script would be def o = open(period = agg); if I wanted a previous one hour I would simply add brackets def o = open(period = agg)[1]; I’m not looking for a previous one hour. I interested to know how do I add an hour to the script? Thank you