MACD Function Returning Incorrect Values

你离开我真会死。 提交于 2019-11-28 10:19:59

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!