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
Here’s the complete list of AggregationPeriod variables to use when creating custom indicators for ThinkorSwim.
thinkScript AggregationPeriod Values
|Chart time frame||AggregationPeriod value|
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.
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 else daysOnChart; 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, length); def TotChgAvg = MovingAverage(averageType, AbsValue(price - price), 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));
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
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.