首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R包MACD函数

R包MACD函数
EN

Stack Overflow用户
提问于 2014-09-08 14:07:47
回答 1查看 1.7K关注 0票数 1

我很难协调R包TTR 0.22中MACD函数的输出(顺便说一句,这是一个很好的包,我使用了很多,感谢约书亚)。

一个具体的例子是APPL 2013年2月19日至2013年5月22日的日收盘价。我选择了这个数据集,因为在电子表格这里中有一个有用的示例。5月22日macd和信号的预期最终值分别为2.762和3.96 (我已经使用标准EMA值12、26、9对这些值进行了双重检查,参见下面的示例代码)。但是,MACD函数对于macd和信号值返回0.63和0.903。我漏掉了什么明显的东西吗?

代码语言:javascript
复制
require(TTR)
require(xts)

## Apple APPL daily closes 19 Feb 13 to 22 May 13
## Data take from example : http://investexcel.net/how-to-calculate-macd-in-excel/
appl = c(459.99,448.85,446.06,450.81,442.8,448.97,444.57,441.4,430.47,420.05,431.14,425.66,430.58,431.72,437.87,428.43,428.35,432.5,443.66,455.72,454.49,452.08,452.73,461.91,463.58,461.14,452.08,442.66,428.91,429.79,431.99,427.72,423.2,426.21,426.98,435.69,434.33,429.8,419.85,426.24,402.8,392.05,390.53,398.67,406.13,405.46,408.38,417.2,430.12,442.78,439.29,445.52,449.98,460.71,458.66,463.84,456.77,452.97,454.74,443.86,428.85,434.58,433.26,442.93,439.66,441.35)
expected.last.macd = 2.7625612158
expected.last.signal = 3.9606012968

macd.maType='EMA'
fast=12
slow=26
sig=9

## 1. Call TTR MACD function and test last() values against expected values 
m = MACD(x=appl, nFast=fast, nSlow=slow, nSig=sig, maType = macd.maType )

actual.last.macd = last(m[,1])
actual.last.signal = last(m[,2])

cat('Test 1. TTR MACD\n')

if ( abs(expected.last.macd-actual.last.macd) > 1e-6 ) {
  cat('macd results differ: expected/actual', expected.last.macd, actual.last.macd, '\n')
} else {
  cat('macd results match\n')
}

if ( abs(expected.last.signal-actual.last.signal) > 1e-6 ) {
  cat('signal results differ: expected/actual', expected.last.signal, actual.last.signal, '\n')
} else {
  cat('signal results match\n')
}


## 2. Build our own MACD
ema.fast = EMA(x=appl, n = fast)
ema.slow = EMA(x=appl, n = slow)
macd = ema.fast - ema.slow
signal = EMA(x=macd, n=sig)

actual.last.macd = last(macd)
actual.last.signal = last(signal)

cat('Test 2. DIY MACD\n')
if ( abs(expected.last.macd-actual.last.macd) > 1e-6 ) {
  cat('macd results differ: expected/actual', expected.last.macd, actual.last.macd, '\n')
} else {
  cat('macd results match\n')
}

if ( abs(expected.last.signal-actual.last.signal) > 1e-6 ) {
  cat('signal results differ: expected/actual', expected.last.signal, actual.last.signal, '\n')
} else {
  cat('signal results match\n')
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-08 18:03:22

您需要设置percent=FALSE

代码语言:javascript
复制
R> m = MACD(x=appl, nFast=fast, nSlow=slow, nSig=sig, percent=FALSE)

来自?MACD

百分比:逻辑;如果“真”,则返回快速和慢移动平均线之间的百分比差,否则返回各个平均值之间的差异。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25726181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档