我正在尝试使用已保存到文件中的数据对库存MACD计算器进行编码。但是,我需要使用的公式需要引用尚不存在的先前值。
我已经尝试过研究和参考,但结果令人吃惊,因为它们都引用了第一个数字为put的数字。
import pandas as pd
df = pd.read_csv(r"C:\Users\drnov\Desktop\Stocks\TNA (3).csv")
del df['High']
del df['Low']
del df['Open']
del df['Volume']
del df['Adj Close']
df['Close'] = round(df['Close'] * 100) / 100
#Original formula (Unsimplified) =(AS4*(2/(12+1))+AT5*(1-(2/(12+1))))
a = df.loc[1, 'EMA12'] = 61.45
b = df.loc[1, 'EMA26'] = 63.90
EMA12 = (df['Close'] * (2/13) + a * (1 - (2/13)))
df['EMA12'] = EMA12
df['EMA12'] = round(df['EMA12'] * 100) / 100
EMA26 = (df['Close'] * (2/27) + b * (1 - (2/27)))
df['EMA26'] = EMA26
df['EMA26'] = round(df['EMA26'] * 100) / 100
MACD = EMA12 - EMA26
df['MACD'] = MACD
print(df)
预期:
Date Close 12 Day EMA 26 Day EMA MACD Signal
0 2019-02-15 65.46 60.06 54.42 5.64 5.47
1 2019-02-14 62.54 59.08 53.77 5.31 5.54
2 2019-02-13 62.12 58.45 53.11 5.35 5.66
3 2019-02-12 61.57 57.79 52.43 5.36 5.77
4 2019-02-11 59.39 57.10 51.87 5.23 5.89
5 2019-02-08 57.95 56.68 51.39 5.30 6.04
6 2019-02-07 57.82 56.45 50.87 5.58 6.18
7 2019-02-06 59.25 56.21 50.20 6.01 6.34
8 2019-02-05 59.49 55.65 49.46 6.20 6.50
9 2019-02-04 59.30 54.95 48.67 6.29 6.63
10 2019-02-01 57.35 54.16 47.97 6.19 6.79
11 2019-01-31 57.11 53.59 47.24 6.34 7.03
12 2019-01-30 55.76 52.95 46.56 6.38 7.32
13 2019-01-29 54.04 52.43 45.96 6.47 7.60
14 2019-01-28 54.39 52.14 45.29 6.85 7.89
15 2019-01-25 55.34 51.73 44.49 7.25 8.15
16 2019-01-24 53.29 51.08 43.78 7.29 8.38
17 2019-01-23 52.35 50.67 43.10 7.58 8.67
18 2019-01-22 52.69 50.37 42.33 8.04 8.95
19 2019-01-18 55.49 49.95 41.28 8.67 9.17
20 2019-01-17 53.80 48.94 40.27 8.66 9.31
21 2019-01-16 52.39 48.06 39.31 8.75 9.44
22 2019-01-15 51.39 47.27 38.34 8.93 9.55
23 2019-01-14 50.05 46.52 37.40 9.12 9.62
24 2019-01-11 51.59 45.88 36.27 9.61 9.76
25 2019-01-10 51.49 44.84 35.05 9.79 9.89
26 2019-01-09 50.75 43.63 33.79 9.83 10.04
27 2019-01-08 49.49 42.33 32.54 9.79 10.24
28 2019-01-07 47.43 41.03 31.34 9.68 10.49
29 2019-01-04 45.02 39.87 30.25 9.62 10.77
30 2019-01-03 40.46 38.93 29.43 9.49 11.19
31 2019-01-02 42.82 38.65 28.36 10.29 11.75
32 2018-12-31 42.09 37.89 27.27 10.63 12.28
33 2018-12-28 41.20 37.13 26.15 10.98 12.82
34 2018-12-27 40.72 36.39 24.98 11.40 13.27
35 2018-12-26 40.46 35.60 23.75 11.86 13.63
36 2018-12-24 35.43 34.72 22.81 11.91 13.79
37 2018-12-21 37.52 34.59 21.64 12.95 13.71
38 2018-12-20 40.80 34.06 20.10 13.95 13.10
39 2018-12-19 42.88 32.83 18.28 14.55 NaN
40 2018-12-18 45.73 31.00 16.08 14.92 NaN
41 2018-12-17 45.94 28.33 13.70 14.63 NaN
42 2018-12-14 49.27 25.12 10.85 14.27 NaN
43 2018-12-13 51.64 20.73 7.59 13.15 NaN
44 2018-12-12 53.93 15.11 3.88 11.24 NaN
45 2018-12-11 52.37 8.06 0.00 8.06 NaN
现实:
Date Close EMA12 EMA26 MACD
0 11/27/2018 59.04 61.08 63.54 -2.460769
1 11/28/2018 63.37 61.75 63.86 -2.115356
2 11/29/2018 62.83 61.66 63.82 -2.158433
3 11/30/2018 63.70 61.80 63.89 -2.089031
4 12/3/2018 65.61 62.09 64.03 -1.936667
5 12/4/2018 57.21 60.80 63.40 -2.606752
6 12/6/2018 56.68 60.72 63.37 -2.649031
7 12/7/2018 53.14 60.17 63.10 -2.931425
8 12/10/2018 52.63 60.09 63.07 -2.972108
9 12/11/2018 52.37 60.05 63.05 -2.992849
10 12/12/2018 53.93 60.29 63.16 -2.868405
11 12/13/2018 51.64 59.94 62.99 -3.051083
12 12/14/2018 49.27 59.58 62.82 -3.240142
13 12/17/2018 45.94 59.06 62.57 -3.505783
14 12/18/2018 45.73 59.03 62.55 -3.522536
15 12/19/2018 42.88 58.59 62.34 -3.749886
16 12/20/2018 40.80 58.27 62.19 -3.915812
17 12/21/2018 37.52 57.77 61.95 -4.177464
18 12/24/2018 35.43 57.45 61.79 -4.344188
19 12/26/2018 40.46 58.22 62.16 -3.942934
20 12/27/2018 40.72 58.26 62.18 -3.922194
21 12/28/2018 41.20 58.33 62.22 -3.883903
22 12/31/2018 42.09 58.47 62.28 -3.812906
23 1/2/2019 42.82 58.58 62.34 -3.754672
24 1/3/2019 40.46 58.22 62.16 -3.942934
25 1/4/2019 45.05 58.93 62.50 -3.576781
26 1/7/2019 47.43 59.29 62.68 -3.386923
27 1/8/2019 49.49 59.61 62.83 -3.222593
28 1/9/2019 50.75 59.80 62.93 -3.122080
29 1/10/2019 51.49 59.92 62.98 -3.063048
.. ... ... ... ... ...
32 1/15/2019 51.39 59.90 62.97 -3.071026
33 1/16/2019 52.39 60.06 63.05 -2.991254
34 1/17/2019 53.80 60.27 63.15 -2.878775
35 1/18/2019 55.49 60.53 63.28 -2.743960
36 1/22/2019 52.69 60.10 63.07 -2.967322
37 1/23/2019 52.35 60.05 63.04 -2.994444
38 1/24/2019 53.30 60.20 63.11 -2.918661
39 1/25/2019 55.34 60.51 63.27 -2.755926
40 1/28/2019 54.39 60.36 63.20 -2.831709
41 1/29/2019 54.04 60.31 63.17 -2.859630
42 1/30/2019 55.76 60.57 63.30 -2.722422
43 1/31/2019 57.11 60.78 63.40 -2.614729
44 2/1/2019 57.35 60.82 63.41 -2.595584
45 2/4/2019 59.30 61.12 63.56 -2.440028
46 2/5/2019 59.49 61.15 63.57 -2.424872
47 2/6/2019 59.25 61.11 63.56 -2.444017
48 2/7/2019 57.82 60.89 63.45 -2.558091
49 2/8/2019 57.95 60.91 63.46 -2.547721
50 2/11/2019 59.39 61.13 63.57 -2.432849
51 2/12/2019 61.57 61.47 63.73 -2.258946
52 2/13/2019 62.12 61.55 63.77 -2.215071
53 2/14/2019 62.54 61.62 63.80 -2.181567
54 2/15/2019 65.46 62.07 64.02 -1.948632
55 2/19/2019 66.01 62.15 64.06 -1.904758
56 2/20/2019 66.93 62.29 64.12 -1.831368
57 2/21/2019 66.16 62.17 64.07 -1.892792
58 2/22/2019 67.89 62.44 64.20 -1.754786
59 2/25/2019 67.92 62.45 64.20 -1.752393
60 2/26/2019 66.42 62.21 64.09 -1.872051
-1 NaN NaN NaN NaN NaN