How to use the functions of MACD R packets?

I am learning to use R. I am interested in pulling stock data and calculating various technical indicators from stock data. My test test is Google Finance. That is, I am checking the results with the GF results.

While trying to implement some kind of MACD analysis, I noticed a couple of things. This is probably my misinterpretation of the documentation. I tried many options, and in some cases I can’t negotiate with Google Finance numbers.

library(quantmod)gives me MACD()which returns columns macdand signal.

library(fTrading)gives me cdsTA()and cdoTA()that return cdsTAand cdoTAaccordingly.

My test supply is IBM, and hopefully this link brings up a chart with prices, volume, slow stochastics and a MACD with a bar chart.

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

Downloading the IBM price data into R and creating the values ​​of the three functions above for the values ​​8, 17, 9 and for MACD()I set percent=FALSE, gives me the following conclusion.

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 link to the Google finance chart above, the cdsTA and macd columns are identical and consistent with Google EMA data. The value MACD()for macd al is also very close to GF. And so I get

macd - signal = discrepancy.

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

+3
source share
1 answer

. cdoTA lag1 lag2 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
+3

Source: https://habr.com/ru/post/1793455/


All Articles