R quantmod addTA功能改变颜色

时间:2017-06-24 02:32:41

标签: r plot xts quantmod stock

首先,我使用TTR包中的stoch函数来计算慢速随机,然后使用addTA函数将其添加到chartSeries函数的绘图中,但是,图中的这两行是黑色的,我想更改它们变成不同的颜色。

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)

我试图使用:

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

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

但两者都不起作用。请指教。谢谢。

1 个答案:

答案 0 :(得分:6)

试试这个:

   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"))

或者,我建议使用增强型chart_Series代替:

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