3

I am using talib technical analysis library to calculate MACD.
I used AAPL data to calculate MACD(8, 17, 9) but the talib values are completely different from Google and Yahoo finance.
Here is my javascript (I copied last AAPL close data since 2015-08-21):

var talib = require('./node_modules/talib/build/Release/talib');
var marketData = { open: [], close: [106.2199999999999989,
 112.6500000000000057,
 115.0100000000000051,
 116.5000000000000000,
 117.1599999999999966,
 116,
 115.1500000000000057,
 115.2399999999999949,
 113.5498999999999938,
 119.6901000000000010,
 115.5199999999999960,
 115.1700000000000017,
 115.4000000000000057,
 114.6400000000000006,
 118.4350000000000023,
 121.4599999999999937,
 122.3700000000000045,
 122.9899999999999949,
 123.3199999999999932,
 122.8900000000000006,
 124.4800000000000040,
 125.1599999999999966,
 125.2199999999999989,
 130.7500000000000000,
 132.0699999999999932],high: [], low: [], volume: [] };
 talib.execute({
    name: "MACD",
    startIdx: 0,
    endIdx: marketData.close.length - 1,
    inReal: marketData.close,
    optInFastPeriod: 8,
    optInSlowPeriod: 17,
    optInSignalPeriod: 9
}, function (result) {
   console.log(result);
});

MACD value of 2005-08-21 from Yahoo and Google finance is -2.73, talib value is 3.83 and with more data MACD are very different. What I am doing wrong?
I also noticed that talib SMA and EMA give the same results.
By the way, inverting in Google charts MACD slow and fast periods, does not change the chart... Yahoo does.

Stefano Piovesan
  • 1,185
  • 3
  • 19
  • 37
  • What logic was used for choosing these optInXXX params? The defaults seems to be 12, 26, 9. – truf Aug 25 '15 at 10:10
  • I am building a meteorjs application for [Rule#1](http://www.ruleoneinvesting.com/), that uses `MACD(8,17,9)`. Of course I could use the default, but I am expecting that the talib library works for any value... – Stefano Piovesan Aug 25 '15 at 12:23
  • 1
    Btw, have you seen [this discussion](https://www.quantopian.com/posts/ta-dot-macd-values-dont-match-google-or-yahoo-finance-charts) there is following explanation: "The reason that Yahoo and Google have different MACD values is that they are using different window lengths.  Yahoo and Google don't use 34 days every time - they use all the way back to the first of the year, a window that grows in size each trading day. ". Could you try TALib with all data since 2015-01-01? – truf Aug 25 '15 at 15:22

3 Answers3

1

Maybe you could use talib-binding to do this, I wrote it yesterday. The code as follow:

import * as talib from 'talib-binding'
talib.MACD([106.2199999999999989,
    112.6500000000000057,
    115.0100000000000051,
    116.5000000000000000,
    117.1599999999999966,
    116,
    115.1500000000000057,
    115.2399999999999949,
    113.5498999999999938,
    119.6901000000000010,
    115.5199999999999960,
    115.1700000000000017,
    115.4000000000000057,
    114.6400000000000006,
    118.4350000000000023,
    121.4599999999999937,
    122.3700000000000045,
    122.9899999999999949,
    123.3199999999999932,
    122.8900000000000006,
    124.4800000000000040,
    125.1599999999999966,
    125.2199999999999989,
    130.7500000000000000,
    132.0699999999999932], 8, 17, 9)

The result is:

[ [ 3.8371737131517705 ],
    [ 2.7731844591512465 ],
    [ 1.063989254000524 ] ]
acrazing
  • 1,955
  • 16
  • 24
0

25 data points is not enough to get a good result. Calculation of an EMA series (MACD is based on multiple sets of EMA series) is recursive. for calculating EMA/MACD, i'd say use at least a year's worth of closing price data.

smoke-screen testing of technical indicator software can be rather arduous since you have to be certain you're using the exact same set of data, ensure you're starting with the same initial priming values, etc., as the reference system/calculation is.

as far as getting the data, yahoo is one option obviously. markit also offers an api that might work which returns historical prices.

user2524973
  • 1,087
  • 6
  • 14
  • Yes, I noticed the difference using [quandl data](https://www.quandl.com/api/v3/datasets/WIKI/AAPL.json). I tried the same comparison downloading the yahoo data (http://real-chart.finance.yahoo.com/table.csv?s=AAPL&d=7&e=24&f=2015&g=d&a=11&b=12&c=1980&ignore=.csv)
    . The results are almost the same as with 25 points: last MACD is still 3.85.
    – Stefano Piovesan Aug 24 '15 at 20:00
  • hmm, for fun can you try reversing values assigned to `startIdx` and `endIdx`? – user2524973 Aug 25 '15 at 03:32
  • I get `Segmentation fault: 11`. `MACD(8,17,9)` line is `EMA8-EMA17`, and `Signal` is `EMA9(MACD)`. If I do it manually I get the same `MACD=-1.67` value reported by Yahoo (a little bit different because talib SMA and EMA are the same...) – Stefano Piovesan Aug 25 '15 at 05:33
  • what i noticed is it appears you're feeding in a dataset that starts at the most recent data point and moves backwards -- that is, index 0 is aug 21, index 1 is aug 20, index 2 is aug 19, and so on. is this the order that talib expects? my only other thought is to reverse the order of the dataset, leaving all other inputs the same, and see what you get. – user2524973 Aug 25 '15 at 11:39
  • Yes, the order is ok (element 0 is the newest one): at least, the SMA is working this way. Reversing the order, the MACD is always wrong, – Stefano Piovesan Aug 25 '15 at 21:17
0

I'm not an specialist but i understand is if you are using 3 different periods ( 8, 17 and 9 ) you will need at least the double amount of values of your longest period in order to calculate the current value.

For instance, imagine you are at time T and you calculating the 17 period for the value T-17 you will need at least 34 values so T-17 can be calculated correctly and T-16... until you reach your current value

Does that make sense?

kroe
  • 1,116
  • 3
  • 11
  • 23