如何使用R包的MACD函数?

发布于 2024-10-18 16:16:24 字数 2076 浏览 2 评论 0原文

我正在学习使用 R。我对提取股票数据并根据股票数据计算各种技术指标感兴趣。我的测试基准是 Google Finance。也就是说,我将我的结果与GF的结果进行核对。

在尝试实施某种 MACD 分析时,我注意到了一些事情。这些可能是我对文档的误解。我尝试了很多变体,但在某些情况下我无法与 Google 财经的数据达成一致。

library(quantmod) 为我提供了 MACD(),它返回列 macdsignal

library(fTrading) 为我提供了 cdsTA()cdoTA(),它们返回 cdsTAcdoTA分别。

我的测试股票是 IBM,希望此链接能够显示包含价格、成交​​量、慢速随机指标和带有直方图的 MACD 的图表。

http://www.google.com//finance?chdnp=1&chdd=1&chds =1&chdv=1&chvs=Linear&chdeh=0&chfdeh=0&chdet=1298224745682&chddm=46920&chddi=86400&chls=CandleStick&q=NYSE:IBM&ntsp=0

正在加载将 IBM 的价格数据转换为 R 并生成上述 3 个函数的值 8、17、9 和 MACD() 我设置 percent=FALSE 给出以下结果输出。

MACD(close, 8, 17, 9, maType="EMA", percent=FALSE)
cdsTA(close, lag1 = 8, lag2 = 17, lag3 = 9)
cdoTA(close, lag1 = 8, lag2 = 17, lag3 = 9)

      date     close     macd   signal      cdsTA      cdoTA
2011-02-07    164.17 3.187365 3.208984   3.208984 -0.7673435
2011-02-08    166.05 3.246812 3.216549   3.216549 -0.7996041
2011-02-09    164.65 3.052187 3.183677   3.183677 -1.0496306
2011-02-10    164.09 2.780047 3.102951   3.102951 -1.3332292
2011-02-11    163.85 2.496591 2.981679   2.981679 -1.5867962
2011-02-14    163.22 2.168977 2.819138   2.819138 -1.8408138
2011-02-15    162.84 1.846701 2.624651   2.624651 -2.0507546
2011-02-16    163.40 1.640518 2.427824   2.427824 -2.1262626
2011-02-17    164.24 1.550798 2.252419   2.252419 -2.0854783
2011-02-18    164.84 1.517145 2.105364   2.105364 -1.9968608

如果您参考上面的谷歌财经图表,cdsTA 和 MACD 列是相同的,并且与谷歌的 EMA 数据非常吻合。 MACD() 的 macd al 值也非常接近 GF 的值。所以我得到

MACD - 信号 = 背离。

然而,cdoTA 还很遥远。我做错了什么?

I am learning to use R. I have an interest in pulling stock data and calculating various technical indicators on the stock data. My test benchmark is Google Finance. That is, I check my results with GF's results.

While trying to implement some sort of MACD analysis, I have noticed a couple of things. These are probably my misinterpretation of the documentation. I have tried many variations and I cannot get agreement with Google Finance's numbers in some cases.

library(quantmod) gives me MACD(), which returns columns macd and signal.

library(fTrading) gives me cdsTA() and cdoTA(), which return cdsTA and cdoTA respectively.

My test stock is IBM, and hopefully this link will pull up a chart with prices, volume, slow stochastics and MACD with histogram.

http://www.google.com//finance?chdnp=1&chdd=1&chds=1&chdv=1&chvs=Linear&chdeh=0&chfdeh=0&chdet=1298224745682&chddm=46920&chddi=86400&chls=CandleStick&q=NYSE:IBM&ntsp=0

Loading up IBM's price data into R and generating the values of the 3 functions above for the values 8, 17, 9 and for MACD() I set percent=FALSE gives me the following output.

MACD(close, 8, 17, 9, maType="EMA", percent=FALSE)
cdsTA(close, lag1 = 8, lag2 = 17, lag3 = 9)
cdoTA(close, lag1 = 8, lag2 = 17, lag3 = 9)

      date     close     macd   signal      cdsTA      cdoTA
2011-02-07    164.17 3.187365 3.208984   3.208984 -0.7673435
2011-02-08    166.05 3.246812 3.216549   3.216549 -0.7996041
2011-02-09    164.65 3.052187 3.183677   3.183677 -1.0496306
2011-02-10    164.09 2.780047 3.102951   3.102951 -1.3332292
2011-02-11    163.85 2.496591 2.981679   2.981679 -1.5867962
2011-02-14    163.22 2.168977 2.819138   2.819138 -1.8408138
2011-02-15    162.84 1.846701 2.624651   2.624651 -2.0507546
2011-02-16    163.40 1.640518 2.427824   2.427824 -2.1262626
2011-02-17    164.24 1.550798 2.252419   2.252419 -2.0854783
2011-02-18    164.84 1.517145 2.105364   2.105364 -1.9968608

If you refer to the google finance chart above, the columns cdsTA and macd are identical and agree closely with Google's EMA figures. MACD()'s value for macd al also pretty close to GF's. And so I get

macd - signal = divergence.

However, cdoTA is way off. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-10-25 16:16:24

你没有做错任何事。 cdoTA 代码不会将 lag1lag2 传递给 cdsTA,因此它仅使用默认值 12 26.

> cdoTA
function (x, lag1 = 12, lag2 = 26, lag3 = 9) 
{
    cdo = macdTA(x, lag1 = lag1, lag2 = lag2) -
           cdsTA(x, lag3 = lag3)                 # no lag1 or lag2, so...
    if (is.timeSeries(x)) 
        colnames(cdo) <- "CDO"
    cdo
}
> args(cdsTA)                                    # default arg values are used
function (x, lag1 = 12, lag2 = 26, lag3 = 9) 
NULL

您可以定义自己的函数CDOTA

CDOTA <- function (x, lag1 = 12, lag2 = 26, lag3 = 9) {
    cdo = macdTA(x, lag1 = lag1, lag2 = lag2) -
           cdsTA(x, lag1 = lag1, lag2 = lag2, lag3 = lag3)
    if (is.timeSeries(x)) 
        colnames(cdo) <- "CDO"
    cdo
}

或者自己用TTR::MACD 的结果进行减法。

require(quantmod)
getSymbols("IBM", source="google")
ibm <- merge(Cl(IBM), MACD(Cl(IBM), 8, 17, 9, "EMA", FALSE))
ibm$macdOsc <- ibm$macd - ibm$signal
tail(ibm)
#            IBM.Close      macd   signal    macdOsc
# 2011-02-15    162.84 1.8361263 2.643950 -0.8078238
# 2011-02-16    163.40 1.6248017 2.440120 -0.8153187
# 2011-02-17    164.24 1.5319154 2.258479 -0.7265640
# 2011-02-18    164.84 1.4965394 2.106091 -0.6095520
# 2011-02-22    161.95 1.1140192 1.907677 -0.7936578
# 2011-02-23    160.18 0.6253874 1.651219 -1.0258316

You're not doing anything wrong. The cdoTA code doesn't pass lag1 or lag2 to cdsTA, so it just uses the default values of 12 and 26.

> cdoTA
function (x, lag1 = 12, lag2 = 26, lag3 = 9) 
{
    cdo = macdTA(x, lag1 = lag1, lag2 = lag2) -
           cdsTA(x, lag3 = lag3)                 # no lag1 or lag2, so...
    if (is.timeSeries(x)) 
        colnames(cdo) <- "CDO"
    cdo
}
> args(cdsTA)                                    # default arg values are used
function (x, lag1 = 12, lag2 = 26, lag3 = 9) 
NULL

You can define your own function CDOTA:

CDOTA <- function (x, lag1 = 12, lag2 = 26, lag3 = 9) {
    cdo = macdTA(x, lag1 = lag1, lag2 = lag2) -
           cdsTA(x, lag1 = lag1, lag2 = lag2, lag3 = lag3)
    if (is.timeSeries(x)) 
        colnames(cdo) <- "CDO"
    cdo
}

Or just do the subtraction yourself with the results from TTR::MACD.

require(quantmod)
getSymbols("IBM", source="google")
ibm <- merge(Cl(IBM), MACD(Cl(IBM), 8, 17, 9, "EMA", FALSE))
ibm$macdOsc <- ibm$macd - ibm$signal
tail(ibm)
#            IBM.Close      macd   signal    macdOsc
# 2011-02-15    162.84 1.8361263 2.643950 -0.8078238
# 2011-02-16    163.40 1.6248017 2.440120 -0.8153187
# 2011-02-17    164.24 1.5319154 2.258479 -0.7265640
# 2011-02-18    164.84 1.4965394 2.106091 -0.6095520
# 2011-02-22    161.95 1.1140192 1.907677 -0.7936578
# 2011-02-23    160.18 0.6253874 1.651219 -1.0258316
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文