RateOfChange (ROC) between moving average and current price


Category:
0
0

Hello peter,

I have discovered that rate of change calculate the current price to a n price in the past ROC = [(Today’s Closing Price – Closing Price n periods ago) / Closing Price n periods ago] x 100. I want it to calculate the current priceoto a price average.  It can be 10 or 20. How can I modify the tos indication or if you can make me one?

Marked as spam
Posted by (Questions: 16, Answers: 12)
Asked on August 31, 2023 3:03 pm
46 views
0
Private answer

Before providing this solution I contacted the author of this post to make sure I understood the request. Once I understood the request, updated the title of the question to better reflect the context. So that other viewers who might be searching for a similar solution will be more likely to find this post using the search function.

This question has also been moved out of the "Stock Scanners" topic and into the "Chart Studies" topic, because there are no conditions provided in the question which would result in a true/false signal used for a custom scan.

The request is to produce a modified ROC which computes its values by comparing the change between the moving average from a specified number of bars ago to the current price.

The solution below is for a chart study. One that displays very much like the built-in chart study named "RateOfChange" which is included with Thinkorswim. I have included a screenshot below which shows both the original ROC as well as the modified one. Drawings have been added to the screenshot demonstrating how the values are being computed.

declare lower;
input rocLength = 14;
input colorNormLength = 14;
input maLengthOne = 21;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
plot roc = if maOne[rocLength] != 0 then 100 * (close / maOne[rocLength] - 1) else 0;
roc.DefineColor("Highest", Color.YELLOW);
roc.DefineColor("Lowest", Color.LIGHT_RED);
roc.AssignNormGradientColor(colorNormLength, roc.Color("Lowest"), roc.Color("Highest"));
plot zeroLine = 0;
ZeroLine.SetDefaultColor(GetColor(5));

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on September 17, 2023 10:51 am
0
Thank Pete, I understand that the default moving average here is 21 which can be changed, right?
( at September 17, 2023 11:05 am)
0
You are correct. I have included a total of 5 user inputs from which you can adjust every aspect of this indicator without modifying a single line of code.
( at September 17, 2023 11:13 am)
0
I see, perfect Pete.
( at September 17, 2023 11:15 am)