Categories
Uncategorized

Rolling percent rank using R

A quest for the uncomputable it seems. I’ve lost 4 hours trying to solve this problem.

I am loading a stock ticker, and the object class is xts/zoo. I thought I’d model the percent rank problem like I did for a simple moving average:

VXX$VXX_30day_SMA<-zoo::rollmean(VXX$VXX.Adjusted, k=30, align="right")

But that didn’t work as there is no roll_perc_rank function. So I went to use rollapply with a function like:

VXX$VXX_vol_22day_pct_rank<-zoo::rollapply(VXX$VXX.Volume, width=22, FUN = percent_rank, align='left', fill = NA)

Which uses the dply::percent_rank function, but that didn’t work.

What finally worked was TTR::runPercentRank

https://www.rdocumentation.org/packages/TTR/versions/0.2/topics/runPercentRank

VXX$VXX_vol_220day_pct_rank<-runPercentRank(VXX$VXX.Volume, n = 220, cumulative = FALSE, exact.multiplier = 0.5)

Since the internet doesn’t have a good page to solve this issue, I wanted to post on it. Be sure to load the TTR library first!