Resource icon

Define thinkScript AggregationPeriod in ThinkorSwim

The 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

Ruby:
AggregationPeriod.[insert period]

Examples

Ruby:
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:

Ruby:
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

  • Like
Reactions: craigjjmorrison
Author
BenTen
Views
516
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from BenTen

Top