Variable Lookback Pivot: Using AggregationPeriod To Reference Past Bars in Think or Swim

Reader filip asked about using the Aggregation Period constants to grab past bar values. He was specifically interested in making a rolling pivot using the last 4 hours high, low and close. There’s a few building blocks we can use to set this up: declare fullrange, some recursive price series, and some freaky use of “if/then/else” blocks and double.nan constants. What a pain! But here we go:

The obvious way to look back for a high is to use the “highest(data,length)” function. The problem (there’s always a problem) is that the “length” argument has to be a constant. That’s no good for us since we would have to calculate “length” to know how far back to look, and the function would throw an error.

Instead, we start by multiplying the number of milliseconds in our aggregation period of choice by the number of units we want. For the 4 hour example:

3,600,000 milliseconds per hour x 4 hours = 14,400,000 milliseconds lookback

Then, we divide by the number of milliseconds per bar in our chart. We find this with the GetAggregationPeriod() function. For a 1 min chart:

14,400,000 milliseconds / 60,000 milliseconds per bar = 240 bars

With that lookback period we set a new series for highs and lows that is double.nan for all bars before the latest bar – 240, and set a value for all bars after. We make it recursive so that it will keep the highest high and lowest low in the lookback period. We also set the close of the most recent bar as our pivot-closing value, and then we calculate pivots from these levels. With “declare fullrange;” the chart is constantly updated on each new tick, leaving us a continually evolving look a the pivot levels.

Here’s what the 1 min chart looked like this evening with a 4 hour lookback:


Now, here’s the thing: I would normally write the tutorial for those who want to put in their own elbow grease and then put my ready-to-go indicator out as a “donor only” offering. However, I have neglected the blog lately with work pressures and now the vacation I am on, so I am putting this out free to all. Know that I love you. However, feel free to donate anyway. 🙂

The file is called “VariableLookbackPivotSTUDY.ts” and can be found on my Google site under “Released Thinkscript Studies“. Read the script for more details about what it does. I commented it a lot. Leave a comment if you don’t follow any of it and I’ll try to explain.

Tags: , , , ,

2 Responses to “Variable Lookback Pivot: Using AggregationPeriod To Reference Past Bars in Think or Swim”

  1. filip Says:

    You´re the man… I even wrote to thinkscript support and they told me it can´t be done…

    I checked out your doner page but there are no fixed amounts.. Are the donations discretionary? This really deserves a couple of beers 😀

  2. Prospectus Says:

    Thanks for the compliments! I don’t have a set amount for a donation. Whatever you think it’s worth.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: