Macd Function Returning Incorrect Values

MACD Function Returning Incorrect Values

I don't know PHP, but I can explain ta-lib behavior from C/C++ perspective. I've debugged its MACD with your dataset. If you call ta-lib's C function ta_macd() for your data you'll get following.

macd     signal    hist
-3.08486 0.869685 -3.95455
-3.4801 -0.000272319 -3.47983
-3.05529 -0.611275 -2.44401
-2.79613 -1.04825 -1.74788
-2.92258 -1.42311 -1.49947
-3.78208 -1.89491 -1.88718
-3.90264 -2.29645 -1.60619
-5.82247 -3.00166 -2.82082
-8.11782 -4.02489 -4.09293
-9.9449 -5.20889 -4.73601
-10.6137 -6.28985 -4.32385
-10.4216 -7.11621 -3.30543
-10.2058 -7.73414 -2.47171
-9.68753 -8.12482 -1.56272
-8.46746 -8.19335 -0.274115
-6.38441 -7.83156 1.44715
-3.66972 -6.99919 3.32947
-1.77941 -5.95524 4.17582
0.218855 -4.72042 4.93927
2.13774 -3.34879 5.48653
4.47273 -1.78448 6.25722
6.08764 -0.210058 6.2977
7.69672 1.3713 6.32543
8.3057 2.75818 5.54752
8.38504 3.88355 4.50149
8.49283 4.80541 3.68743
7.61258 5.36684 2.24574
5.6388 5.42123 0.217565
4.48522 5.23403 -0.748811
3.42501 4.87223 -1.44722
3.32672 4.56312 -1.23641
2.95095 4.24069 -1.28974
2.75772 3.9441 -1.18637

It doesn't match your excel file bcs of following:

  1. TA-Lib calculates 26-day EMA where first EMA result is average of initial 26-days period. This is a default behavior (non-Metastock).
  2. TA-lib calculates 12-day EMA where first EMA result is average of initial 12-days period. But it adjusted to a day where first 26-day EMA could be got. So it uses days 15-26 to calculate average that is first value for 12-day EMA. That's a main difference with your Excel. Just put formula =AVERAGE(B19:B30) in cell C30 and the excel results will match with C function results.
  3. Then TA-lib calculates signal\hist values using 9-day (default signal period) EMA over values it got on steps 2 and 3. And it returns results only for days what have both MACD, signal and hist. In other words it doesn't return first 8 MACD values. If you made the change proposed on step 2 in your Excel you'll see that the C++ function results will match with range E38-G70.

Well, that's how TA-Lib works as far as I can see. But if you compare my results with your PHP function results you'll find that they doesn't match too. But you can notice that my MACDs are equal to values in your 1st array starting from

[33]=>
float(-3.085)

And if you compare PHP results with Excel (updated) you'll find that first elements of 1st array are equal to E30-E37. These are first 8 MACDs that TA-Libs omits as they don't have corresponding signal\hist values bcs signal is calculated with 9-days EMA. It also means following:

  1. trader function authors doesn't use vanilla ta-lib. Perhaps they've rewritten TA_MACD function logic in PHP, or forked ta-lib or used very old version that I don't know. Because ta-lib just don't return MACD values that they returns (although it calculates them).
  2. I don't understand their result arrays format or I don't understand how they calculate signal\hist values (bcs first array is quite explainable).

I would recommend looking inside PHP function source code. I couldn't find it.
If you want to doublecheck vanilla C implementation of MACD in ta-lib, you shall refer to following pages: macd, ema.

Something Wrong With MACD outout in R

There are couple of things wrong:
MACD Line in Google Finance is -19.5 and not 19.5

I used the same data from Yahoo Finance (1 yr NSE data) and the found the MACD to be -19.28 so not much difference. The code that I used is

library(RCurl)
url="http://real-chart.finance.yahoo.com/table.csv?s=%5ENSEI&a=00&b=1&c=2016&d=09&e=31&f=2016&g=d&ignore=.csv"
x <- getURL(url)
library(TTR)
mydata <- read.csv(textConnection(x))
mydata=mydata[!is.na(mydata$Close),]
mydata=mydata[nrow(mydata):1,]
rownames(mydata)=c(1:nrow(mydata))
mydata$macd=MACD(mydata[c('Close')],nFast=12, nSlow=26, nSig=9, maType=EMA,percent = FALSE)

Calculation of MACD using List in R

As you already figured out, the MACD function provides two columns of values - macd and signal.
All you need to do is assign them to not one but two new columns in DATA[[1]] by referencing their MACD indexes - 1 and 2.
Here is the code:

library(quantmod)
DATA <- list(getSymbols('AAPL', from=Sys.Date()-100, auto.assign=FALSE))
names(DATA[[1]])=c('open','high','low','close','volume','adjusted')
macd <- MACD(DATA[[1]]$close, maType='EMA')
tail(macd, 2) #verify
DATA[[1]]$macd <- macd[,1]
DATA[[1]]$sgnl <- macd[,2]
tail(DATA[[1]][,c('macd','sgnl')], 2) #verify

P.S. since you are using MACD default values (nFast = 12, nSlow = 26, nSig = 9), you can omit them in the code.

Rollmean returning incorrect values when Zeros present

The better option would be to use rollapply which got support for partial data.

library(zoo)

Test2 %>%
group_by(Player.Name) %>%
mutate(Chronic_cal = rollapply(OdometerTotal, 28, mean, align='right',
partial = TRUE))

TTR:MACD gives different result than I get in Python and chart

I haven't checked the python function, but TTR::MACD() is definitely correct. Maybe it is that percent=FALSE argument?

library(TTR)

xx <- rep(c(1, rep(0, 49)), 4)

fast <- 20
slow <- 40
sig <- 10

macd <- MACD(xx, fast, slow, sig, maType="EMA", percent=FALSE)

macd2 <- EMA(xx, fast) - EMA(xx, slow)
macd2 <- cbind(macd2, EMA(macd2, sig))

par(mar=c(2, 2, 1, 1))

matplot(macd[-1:-40, ], type="l", lty=1, lwd=1.5)
matlines(macd2[-1:-40, ], type="l", lty=3, lwd=3, col=c("green", "blue"))

Sample Image



Related Topics



Leave a reply



Submit