Hi Gary,
As you probably already know, MACD involves computing three moving averages of varying lengths, in your case of lengths 7, 26 and 9. In general, if you ask a TALib function for a moving average of length N, it will give you back NaN for the first N-1 values of the passed array, because the there isn't enough data to compute the average over the whole window length. Thus, for example, doing
talib.EMA(np.arange(5.0, dtype=np.float64), 3)
yields
array([ nan, nan, 1., 2., 3.])
In the case of MACD, TALib computes two exponential moving averages (EMAs), in your case of period-lengths 12 and 26, resulting in 11 and 25 NaN values being generated for those averages.
After computing those values, it then computes a 9 day EMA of the difference between the first two computed arrays. Since NaN + anything = NaN, this results in TALIb computing a 9-period moving average on an array that contains 25 NaNs. Clearly, the first 25 of these averages will also be NaNs. Less obviously, the next 8 results will also be NaNs, since TALib evaluates any average containing a NaN to NaN. Thus, for a call to
talib.MACD(array, 12, 26, 9)
the first 33 values will be NaN. You can check this by trying:
talib.MACD(np.arange(34) * 1.0, 12, 26, 9)
which yields
(array([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
7.]),
array([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
7.]),
array([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
0.]))
Surprising (to me at least) is the fact that the first two arrays (which should just contain the 12 and 26 day EMAs), have the same NaN behavior as the final result. Looking through the TALib C source, however, it turns out that their MACD implementation computes a single lookback window to use for all three averages, which results in more NaNs than expected for the two plain EMAs.