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));