AggregationPeriod
variable returns the time frame of your choice for the script to pull data from. This is useful for implementing multi-timeframe into an existing indicator.You can this to assign a specific aggregation period for your indicator. It works for backtesting strategies as well.
Here is a list of supported
AggregationPeriod
that you can define in your thinkScript code.- MIN
- TWO_MIN
- THREE_MIN
- FOUR_MIN
- FIVE_MIN
- TEN_MIN
- FIFTEEN_MIN
- TWENTY_MIN
- THIRTY_MIN
- HOUR
- TWO_HOURS
- FOUR_HOURS
- DAY
- TWO_DAYS
- THREE_DAYS
- FOUR_DAYS
- WEEK
- MONTH
- OPT_EXP
- QUARTER
- YEAR
Usage
AggregationPeriod.[insert period]
Examples
AggregationPeriod.DAY
What does AggregationPeriod.DAY mean in thinkScript? This defines the aggregation period equal to the Daily timeframe.
Expanding further into an usable script:
input aggregationPeriod = AggregationPeriod.HOUR;
def price = close(period = aggregationPeriod);
input length = 50;
input displace = 0;
plot SMA = Average(price[-displace], length);
In the code above, we're using the AggregationPeriod of the HOUR (1HR) timeframe. When you add this script to your chart, it will plot the 50 SMA (simple moving average) of the Hourly chart. When you switch to another timeframe lower than the 1HR chart, the moving average line remains unchanged.
Important to note that in ThinkorSwim, you can display data from a higher timeframe onto a lower timeframe. The opposite CAN NOT be done. If you apply the code above to the Daily chart, you will not see the moving average. That is because the HOUR period is less than the Daily period.
Additional scripts related to AggregationPeriod
- https://usethinkscript.com/threads/macd-mtf-multi-time-frame-indicator-for-thinkorswim.474/
- https://usethinkscript.com/threads/multiple-time-frame-mtf-squeeze-indicator-for-thinkorswim.350/
- https://usethinkscript.com/threads/fix-secondary-period-not-allowed-in-thinkorswim-scanner.927/
- https://usethinkscript.com/threads/mtf-rsi-indicator-for-thinkorswim.612/