David Varadi’s RSI(2) alternative

[This article was first published on FOSS Trading, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:
  1. Varadi’s RSI(2) Alternative: The DV(2)
  2. RSI(2) vs. DV(2)
  3. Last Couple of Notes on DV(2)
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 their blog: FOSS Trading.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)