我正在使用talib库中的MACD函数。 http://www.tadoc.org/indicator/MACD.htm
该函数的输出基于输入值的移动平均值。
让我们创建函数输入:
close = np.array([ 2930.8, 2926.3, 2941.5, 2942.9, 2948.5, 2923.1, 2917.6,
2947.6, 2932.4, 2890.8, 2887.2, 2872.7, 2886.9, 2807.1,
2839.2, 2855. , 2878.6, 2862. , 2843.9, 2866.1, 2857.4,
2819.4, 2831.8, 2805.1, 2780.1, 2790.4, 2752.6, 2749.6,
2804.9, 2827.7, 2845.7, 2874.9, 2889.2, 2886.9, 2881. ,
2898.4, 2894.8, 2896.3, 2926.2, 2933.4, 2960. , 2950.6,
2952.1, 2922.1, 2917.9, 2930.9, 2944.2, 2967.7, 2979.6,
3000.3])
index = pd.date_range('2019-04-24','2019-07-02', freq = 'B')
fast_period, slow_period, signal_period = 3, 10, 1
让我们运行函数:
import talib as ta
macd_histogram = pd.DataFrame({'macd_hist': ta.MACD(close, fast_period, slow_period, signal_period)[2]}, index = index)
让我们检查输出的最后几个值:
print(macd_histogram[-3:])
2019-06-28 -11.932
2019-07-01 90723585644794610874060269361590083028296154282...
2019-07-02 0.000
Freq: B, Name: macd_hist, dtype: float64
问题 :如您所见,后两个值明显失真。
让我们重做完全相同的过程:
macd_histogram = pd.DataFrame({'macd_hist': ta.MACD(close, fast_period, slow_period, signal_period)[2]}, index = index)
print(macd_histogram[-3:])
2019-06-28 -11.932
2019-07-01 24.066
2019-07-02 30.652
Freq: B, Name: macd_hist, dtype: float64
这一次,最后两个值似乎更有可能被正确计算。
让我们再尝试一次。
macd_histogram = pd.DataFrame({'macd_hist': ta.MACD(close, fast_period, slow_period, signal_period)[2]}, index = index)
print(macd_histogram[-3:])
macd_hist
2019-06-28 -11.932
2019-07-01 0.000
2019-07-02 0.000
更加不一致!