Apply Secondary Aggregation Period to RateOfChange Study


Category:
0
0

Is there a way to easily add the aggregation period input to any indicator?

I tried adding input period to the Rate of Change indicator, but it still only calculates based on the selected chart period.

 

declare lower;

input length = 14;
input colorNormLength = 14;
input period= Aggregationperiod.thirty_min;
input price = close;

assert(length > 0, “‘length’ must be positive: ” + length);

plot ROC = if price[length] != 0 then (price / price[length] – 1) * 100 else 0;
plot ZeroLine = 0;

ROC.DefineColor(“Highest”, Color.YELLOW);
ROC.DefineColor(“Lowest”, Color.LIGHT_RED);
ROC.AssignNormGradientColor(colorNormLength, ROC.color(“Lowest”), ROC.color(“Highest”));
ZeroLine.SetDefaultColor(GetColor(5));

I have been able to setup the RSI, Stoch, moving averages…and a few other indicators in a similar manner. What am I missing, in order to make this work with the Rate of Change?

 

Thanks in advance.

RESOLVED
Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on November 17, 2019 8:17 am
168 views
0
Private answer

Well congratulations on getting the secondary aggregation period to work with those other indicators. The process is exactly the same for all indicators so I'm not sure why this one has tripped you up. The code you provided merely adds an input to contain the secondary aggregation period. The input is not applied anywhere in the code you provided. So that is the number one issue to address. If you add an input to a study, you need to wire that up so that it actually does something.

I see that you are trying to learn so I will go into a bit more detail to explain things rather simply provide a solution.

Properly Formatted Code Statements:

From your code we want to examine the following:

input period= Aggregationperiod.thirty_min;

First thing to mention. The word "period" is actually a keyword used to describe one of the input parameters of the price series data arrays. Which are: open, high, low, close, volume and a few others. Thinkorswim refers to these data arrays as "Fundamentals".

So we try to avoid using keywords as the names for inputs and variables. It's not required. But it makes the code easier to read, troubleshoot and maintain. So in place of the word "period" we'll use "timeFrame".

I will also clean up the code a bit. You did not properly apply the upper/lower case characters in your input statement. Again, this is not required as it would be an actual programming language. But it makes the code easier to read and maintain.

input timeFrame = AggregationPeriod.THIRTY_MIN;

The Solution:

Now that we have this patched up, we can get into the solution. Actually I don't need to explain things any further. I can simply provide you the link to the documentation in Thinkorswim and I'm done.

https://tlc.thinkorswim.com/center/reference/thinkScript/tutorials/Advanced/Chapter-11---Referencing-Secondary-Aggregation.html

Be sure to read through that documentation carefully. It will provide everything you need to know about referencing secondary aggregation periods. They employ a slightly different method than I prefer. However their technique provides the most flexibility. So we'll follow their lead in solving your particular request. (Hat Tip: I actually learned something new while writing this solution. Thanks for that. I LOVE to learn new things!)

Here are the two lines of code that require modifications to make use of the new "timeFrame" user input.

input price = close;

plot ROC = if price[length] != 0 then (price / price[length] – 1) * 100 else 0;

To those lines of code, we now apply the techniques taught in the documentation I linked above.

input price = FundamentalType.CLOSE;

plot ROC = if Fundamental(price, period = timeFrame)[length] != 0 then (Fundamental(price, period = timeFrame) / Fundamental(price, period = timeFrame)[length] – 1) * 100 else 0;

This solution allows the user input for "price" to be maintained. The solution I prefer requires that the "price" input be removed entirely. It makes the code easier to read and write. But it removes the ability to adjust the price from the user settings. (yes, I am very lazy and I cut corners wherever I find it appropriate)

I have attached a screenshot showing the results. The modified version is in the lower right subgraph. Compare that to the values in the lower subgraph to the left.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on November 17, 2019 10:07 am