Search code examples
rquantmodquantitative-financetechnical-indicator

quantmod display weekly indicator on daily chart


I am trying to display the daily and weekly RSI on the daily charts but have no success

getSymbols('JNUG')
chartSeries(JNUG, subset = 'last 4 months', dn = 'red', TA = 'addRSI(n=14); addLines(h = c(30, 70), on = 2, col = "red"); addTA(RSI(Cl(to.weekly(JNUG)), n =2))')

I also tried the following:

chartSeries(JNUG, subset = 'last 4 months', dn = 'red', TA = 'addRSI(n=14); addLines(h = c(30, 70), on = 2, col = "red")')
addTA(RSI(Cl(to.weekly(JNUG)), n =14))

The weekly RSI does not appear on the plot. Can someone help.


Solution

  • When you plot the weekly points, NA values are filled in between on the merge to the original daily data. The weekly RSI plot wants to join together points and NAs as a line plot, but no line is drawn where there are NAs, no line gets drawn in the end.

    Try this:

    chartSeries(JNUG, subset = 'last 4 months', dn = 'red', TA = 'addRSI(n=14); addLines(h = c(30, 70), on = 2, col = "red"); addTA(RSI(Cl(to.weekly(JNUG)), n =2), type = "p")')
    

    enter image description here Or, if you want lines instead, try this (see lowest add_TA call):

    library(quantmod)
    chart_Series(JNUG, subset = '2016-11/')
    add_RSI(n= 14)
    v <- to.weekly(JNUG)
    add_TA(merge(xts(order.by = index(JNUG)), RSI(Cl(v))), type = 'p')
    #na.spline interpolates between points smoothly.  Could also use fill = na.locf (produces a step function), etc ...
    add_TA(merge(xts(order.by = index(JNUG)), RSI(Cl(v)), fill = na.spline), type = 'l')
    

    which produces this:

    enter image description here

    EDIT: One way to add horizontal lines on add_TA subplots:

    chart_Series(JNUG, subset = '2016-01/')
    add_RSI(n= 14)
    v <- to.weekly(JNUG)
    # Note: na.locf does not introduce 'lookforward bias' in visualised technical indicator plots, while na.spline does.
    add_TA(merge(xts(order.by = index(JNUG)), RSI(Cl(v)), fill = na.locf), type = 'l')
    low_rsi <- 30 
    hi_rsi <- 70
    xrsi_low <- xts(order.by = index(JNUG), x = rep(low_rsi, NROW(JNUG)))
    xrsi_hi <- xts(order.by = index(JNUG), x = rep(hi_rsi, NROW(JNUG)))
    add_TA(xrsi_low, col = "purple", type = 'l', on = 3, lty = 2)
    add_TA(xrsi_hi, col = "purple", type = 'l', on = 3, lty = 4)
    

    enter image description here