【问题标题】:How to create custom indicator? Slope of the line of 50 day EMA如何创建自定义指标? 50 天 EMA 线的斜率
【发布时间】:2017-11-21 17:49:25
【问题描述】:

我一直在使用 Quantmod 的 NewTa 函数创建一些技术指标。 我一直在尝试创建一个自定义指标,理想情况下应该使用ChartSeries 绘制图表。该指标应显示调整后收盘价的 50 天 EMA 线的斜率。

getSymbols("NOVO-B.CO")
p <- na.omit('NOVO-B.CO')
FiftyEMA <- function(x){


MA <- removeNA((EMA(p[,6],n=50)))

}
SlopeFiftyEMA <- function(x){
  run=(FiftyEMA(y)/FiftyEMA(x))
}
Slope.Indicator <- newTA(SlopeFiftyEMA,legend.name = "50 Day EMA Slope of Line Indicator")
Slope.Indicator()

这给了我错误:get.current.chob() 中的错误:不正确设置或缺少图形设备

我还尝试了一个新代码,它给了我一个实际的指标!请让我知道您的想法(如果您认为它看起来正确与否):

首先我将数据导出到excel:(股票数据仍然表示为p

write.csv(p,"data")

导入数据

x <- data[,1]
y <- data[,7]
MA <- removeNA(EMA(y,n=50))
length(MA)

MA 的长度 = 1923

l=1:1923
SlopeFiftyEMA <- function(x){
(diff(MA)/diff(l))
}
Slope.Indicator <- newTA(SlopeFiftyEMA,legend.name = "50 Day EMA Slope of Line Indicator")
twelvemonths="last 12 months"
chartSeries(p,subset = twelvemonths,theme = 'white',up.col = 'blue',dn.col = 'grey',name ="Custom Indicators")
Slope.Indicator()

任何输入任何人?我上次发帖时没有指标

提前致谢!

【问题讨论】:

    标签: r plot quantmod moving-average technical-indicator


    【解决方案1】:

    您的第一个错误似乎存在,因为您在调用Slope.indicator() 之前没有调用chartSeries。但是你的代码有点乱,包括没有定义y(可能你以后在导入数据中引入)。

    这里介绍的方法将根据线性回归绘制 MA 的斜率,使用 chart_Series(可以说比原始 chartSeries 更清晰的图)。计算了两种类型的斜率,包括您提出的一种,即 EMA 的差异。

    getSymbols(c("NOVO-B.CO"))
    
    x <- `NOVO-B.CO`
    x[, c(1:4, 6)] <- na.locf(x[, c(1:4, 6)])
    x$EMA <- EMA(Cl(x), n = 50)
    x <- merge(x, rollSFM(Ra = x[, "EMA"], Rb = 1:NROW(x), n = 20))
    
    x <- merge(x, setNames(diff(x$EMA), "diff1"))
    
    chart_Series(x, subset = "2016/")
    add_TA(x$EMA, on = 1, col = "purple")
    # Plot the slope of the MA:
    add_TA(x$beta, col = "green")
    # Plot the 1 lag diff of the moving average:
    add_TA(x$diff1, lty = 2)
    

    【讨论】:

    • 非常感谢(再次)。我同意你的观点,Chart_Series 调用产生的图表比 ChartSeries 更好。但是,它没有提供那么多的内置 TA。再次感谢
    • FXQuantTrader,这段代码有什么作用? x[, c(1:4, 6)]
    • c(1:4, 6) 给出索引 1,2,3,4,6。所以 x[, c(1:4, 6)] 总共返回 5 列(它不返回第 5 列)。然后,这些列中的任何 NA 值都会在同一列(即na.locf(x[, c(1:4, 6)]))中用最近的非 NA 观察值填充。尝试阅读?na.locf
    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2016-01-22
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多