How to use aggregationPeriod


0
0

Hi Pete, just wondering how to use aggregationPeriod in different periods. For example, def period= aggregationPeriod.Day; plot a=if(period==1, ATR(length = 14, averageType = “WEIGHTED”), Double.NaN); It seems not working. Thanks.

Marked as spam
Posted by (Questions: 7, Answers: 9)
Asked on May 24, 2018 4:32 pm
3303 views
0
Private answer

Without even taking the time to test this for myself I checked the Thinkscript language reference. Because I didn’t really think that testing for “period == 1” would be anywhere close to the actual value for the daily time frame.

VERY IMPORTANT NOTE

Before we go any further I want to mention that you posted this in the “Alerts and Notifications” topic. So long as this pertains to Chart Studies, we can work with secondary aggregation periods. Be aware that secondary aggregation periods are not usually permitted for Scans and Watchlist Columns. 

Style Elements

Before I get to the details let me make some comments on style. Please ALWAYS place spaces between variable names and operators. “period==1” is not as readable as “period == 1”. And it gets even muddier when you start doing greater than and less than comparisons. Want some authoritative source for this? Just take some time to review all the code examples given in the Thinkrscript tutorials: http://toslc.thinkorswim.com/center/howToTos/tutorials/Basic.html

Also, be sure to avoid using excessive abbreviations even for example code. New folks will see def a=if..... and they will be led to believe that using a single letter for a variable name is desirable. You will see all of my examples use the minimum amount of abbreviations. Spell it out. The goal is readability and no performance gains are realized by using abbreviations in place of whole words.

Troubleshooting (what’s that?)

Ok, now let’s take a look at what you are trying to do and provide your solution. So when you are testing the numeric value of the aggregation period, it is assumed that you are trying to apply a specific setting for the ATR when the user selects the Daily time frame. Did you try to troubleshoot this code by changing:

def period = AggregationPeriod.DAY;

to

plot period = AggregationPeriod.DAY;

Yes, the A in AggregationPeriod gets capitalized. Source: http://toslc.thinkorswim.com/center/reference/thinkScript/Constants/AggregationPeriod/AggregationPeriod-DAY.html

The Fix

From that link I just provided, please notice the description of the daily aggregation period. In particular, notice what it says about the value:

Defines aggregation period equal to one day (86,400,000 milliseconds).

So without doing any testing of my own. My first attempt to try to get this working would be to check if the period variable is equal to 86,400,00. Yes?

Give that a try and let us know if it works.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 24, 2018 5:09 pm
0

Unfortunately, jx2012 posted a follow up question by using the “Post your Answer” box at the bottom of the thread. This is reserved for new answers that solve the question. This should have been posted here in the comments section. So I will include the follow up comment from jx2012 here:

“Man, you are good. Thanks for your response. I will definitely follow your instruction about the style. I guess that you want to check if period is >= AggregationPeriod.DAY is to make sure that the chart is big enough for the daily data. Correct :-)? I know the below script works. But why plot data = ATR(period = agg); is not working :-)? How to write a correct code for ATR? I am only a newbie and it is difficult to do it on my own. Your help is greatly appreciated.

def agg = AggregationPeriod.DAY;
plot data = close(period = agg);”

( at May 24, 2018 7:17 pm)
0

From your additional follow up question: “I guess that you want to check if period is >= AggregationPeriod.DAY is to make sure that the chart is big enough for the daily data. Correct :-)?”

Actually I don’t have the slightest clue why your code is checking the aggregation period like this. I don’t take time to try and read between the lines. So if you have some particular goal in mind it’s best to explain that clearly rather than submit some code and expect me to extrapolate your intention.

However I can tell you that: plot data = ATR(period = agg); is completely invalid. On two counts. First, the ATR study requires 2 inputs. Length and Average Type. You are providing only one input, and that input is neither the length nor the average type. Not to mention you cannot reference the ATR study in this way. Why? Because the ATR requires data from the High, Low and Close but it does not accept any of these as inputs. You must use the full code from the ATR study. I will avoid providing additional information until I understand your ultimate goal for this code.

( at May 24, 2018 7:22 pm)
0

Pete, sorry I might have made you confused. My goal is simple. I could read ATR easily from my daily chart. But I normally used 3 min chart for trading so I need to move very often between min chart and daily chart. Now I think maybe I can put ATR into min chart but I don’t know how to do it :-). Thanks.

( at May 24, 2018 7:39 pm)
0

Ah hah. Ok that makes things perfectly clear. So you want to plot the daily ATR on a 3 min chart? Or do you just need to see the current daily ATR value on a 3 min chart? If it is the former, please post this as a new request. Title should be something like “Plot daily ATR study on intraday chart”. If it is the later, then you can try one of these two posts:

https://www.hahn-tech.com/ans/add-chart-label-for-average-true-range-atr/

https://www.hahn-tech.com/ans/atr-custom-watch-list/

( at May 24, 2018 8:27 pm)
0

Pete, thank you for your input. I think I got what I was looking for :-).

( at May 26, 2018 9:48 am)