(This article was first published on

Here's a quick R implementation of David Varadi's alternative to the RSI(2). Michael Stokes over at the MarketSci blog has three great posts exploring this indicator:**FOSS Trading**, and kindly contributed to R-bloggers)Here's the R code:

DV <- function(HLC, n=2, bounded=FALSE) {

# "HLC" is an _xts_ object with "High", "Low", and "Close"

# columns, in that order.

# This is David Varadi's alternative to the RSI(2). Calculations

# taken from the marketsci blog -- http://marketsci.wordpress.com

# Author of this implementation: Joshua Ulrich

# Calculate each day's high/low mean

hlMean <- rowMeans( HLC[,-3] )

# Calculate the running Mean of the Close divided by the

# high/low mean, then subtract 1.

res <- runMean( HLC[,3] / hlMean, n ) - 1

# If we want the bounded DV...

if(bounded) {

# Set the range to calculated the bounded DV

rng <- 252:NROW(res)

# Grab the index of the unbounded results, so we can convert

# the bounded results back to an xts object.

indx <- index(res)

# A simple percent rank function hack

pctRank <- function(x,i) match(x[i], sort(coredata(x[(i-251):i])))

# Apply the percent rank function to the coredata of our results

res <- sapply(rng, function(i) pctRank(res, i) / 252)

# Convert the bounded results to xts

res <- xts(c(rep(NA,251),res), indx)

}

# Return results

return(res)

}

To

**leave a comment**for the author, please follow the link and comment on his blog:**FOSS Trading**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...