3

First, I used stoch function in TTR package to calculate the slow stochastic and then add it to plot from chartSeries function by using addTA function, however, these two lines in the plot is black in color and I would like to change them into different color.

Input:

chartSeries(df, subset='last 3 years', TA = NULL, theme = "white", up.col = "green", dn.col = "red")

slow.stoc <- stoch(na.omit(HLC(df)), 25, 25, 9, 'SMA')[,2:3]

addTA(slow.stoc)

I tried to use:

 lines(slow.stoc[2], col="red", lty="solid")

 addLines(slow.stoc[2], col = "red")

But both are not working. Please advise. Thank you.

1 Answer 1

6

Try this:

   chartSeries(df, subset='last 3 years', TA = NULL, theme = "white", up.col = "green", dn.col = "red")
    slow.stoc <- stoch(na.omit(HLC(df)), 25, 25, 9, 'SMA')[,2:3]
    addTA(slow.stoc, col = c("red", "green"))

Alternatively, which I would recommend, use the enhanced chart_Series instead:

library(quantmod)

# optional, set up bar colours as in your question, for chart_Series:
getSymbols("GOOG")
myTheme<-chart_theme()
myTheme$col$up.col<-'darkgreen'
myTheme$col$dn.col<-'darkred'
myTheme$col$dn.border <- 'black'
myTheme$col$up.border <- 'black'
myTheme$rylab <- FALSE
myTheme$col$grid <- "lightgrey"

# get your desired result
df <- GOOG
slow.stoc <- stoch(na.omit(HLC(df)), 25, 25, 9, 'SMA')[,2:3]
chart_Series(df, subset='2017', theme = myTheme)
add_TA(slow.stoc["2017", 1], col = "purple", lty = "dashed")
add_TA(slow.stoc["2017", 2], col = "red", lty = 3, on = 2)

enter image description here

3
  • It would be nice if there was some way of passing the plot parameters directly into chart_Series(), instead of having to first create a theme object (myTheme <- chart_theme()), and then to modify it.
    – algoquant
    Commented Aug 28, 2017 at 13:49
  • @algoquant I personally just make a wrapper function that passes back my chart_theme() object and pass it into chart_Series as a "single" argument. e.g. chart_Series(df, ......, theme = myThemeFun()). myThemeFun is defined somewhere in a package, and returns the chart theme object, and is available on call with configs desired so they dont' have to reprogrammed each time I use them. Commented Feb 7, 2018 at 11:46
  • 1
    Yes, adding a wrapper is a good solution in this case, although it adds more function calls in R, which makes it a little slower. But if you're not performing a loop with many calls to plot, then it's OK.
    – algoquant
    Commented Feb 8, 2018 at 13:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.