简体   繁体   English

移动平均线计算不正确

[英]Moving average not calculating properly

I am doing a time series analysis of Microsoft stock with data pulled from Quandl.我正在使用从 Quandl 中提取的数据对 Microsoft 股票进行时间序列分析。 I want to plot closing prices against moving averages.我想根据移动平均线绘制收盘价。 When I plot the moving averages they do not go all the way to right side of the graph.当我绘制移动平均线时,它们不会一直走到图的右侧。

I believe having a gap makes sense (for instance the 200 day moving average can't start until day 200), but am concerned that the gap is on the right.我相信有一个缺口是有道理的(例如,200 天移动平均线要到 200 天才能开始),但我担心缺口在右边。 This implies that it is starting on the most recent date (which makes some sense as most recent date is first in the time series), which is obviously not the correct way to do it.这意味着它从最近的日期开始(这是有道理的,因为最近的日期是时间序列中的第一个),这显然不是正确的方法。

I could just reverse the series (new data frame ascending instead of descending), but I believe it would then also plot the graph starting at the most recent date, which is obviously not acceptable.我可以反转系列(新数据框升序而不是降序),但我相信它也会从最近的日期开始绘制图形,这显然是不可接受的。

#calculate moving averages for Microsoft

roll100 = MSFT_data['Adj. Close'].rolling(100).mean()
roll200 = MSFT_data['Adj. Close'].rolling(200).mean()
roll50 = MSFT_data['Adj. Close'].rolling(50).mean()
roll10 = MSFT_data['Adj. Close'].rolling(10).mean()


roll200.plot(label = '200 Day Moving Average')
roll50.plot(label = '50 Day Moving Average')
roll100.plot(label = '100 Day Moving Average')
MSFT_data['Adj. Close'].plot(label = 'MSFT Closing Price', color = 'blue')
plt.legend(loc = 'upper left')

Since moving averages should always be calculated starting with the earliest date, I'm assuming I'm missing a simple command to do so.由于移动平均线应该总是从最早的日期开始计算,我假设我缺少一个简单的命令来这​​样做。

Update: it was suggested that I make the moving averages part of the original data frame (I assume so they were associated with the date index) but I had the same result:更新:有人建议我将移动平均线作为原始数据框的一部分(我假设它们与日期索引相关联)但我得到了相同的结果:


MSFT_data['roll100'] = MSFT_data['Adj. Close'].rolling(100).mean()
MSFT_data['roll200'] = MSFT_data['Adj. Close'].rolling(200).mean()
MSFT_data['roll50'] = MSFT_data['Adj. Close'].rolling(50).mean()
MSFT_data['roll10'] = MSFT_data['Adj. Close'].rolling(10).mean()

#plot Microsoft Price along with moving averages

fig = plt.figure()
fig.set_figheight(8)
fig.set_figwidth(10)

MSFT_data['roll200'].plot(label = '200 Day Moving Average')
MSFT_data['roll50'].plot(label = '50 Day Moving Average')
MSFT_data['roll100'].plot(label = '100 Day Moving Average')
MSFT_data['Adj. Close'].plot(label = 'MSFT Closing Price', color = 'blue')
plt.legend(loc = 'upper left')

图形

Also if I examine the column for instance with:另外,如果我检查列,例如:

MSFT_data['roll10'].head(12)

Date
2017-12-29       NaN
2017-12-28       NaN
2017-12-27       NaN
2017-12-26       NaN
2017-12-22       NaN
2017-12-21       NaN
2017-12-20       NaN
2017-12-19       NaN
2017-12-18       NaN
2017-12-15    85.796
2017-12-14    85.711
2017-12-13    85.674
Name: roll10, dtype: float64

I can see that the 9 most recent values are NaN.我可以看到 9 个最近的值是 NaN。 Conversely if I create a new data frame and try to reindex ascending, my last 9 values in the tail are NaN.相反,如果我创建一个新的数据框并尝试重新索引升序,则尾部的最后 9 个值是 NaN。

I'm not sure if this can help you but if I try to reproduce this I don't have any gap to the right.我不确定这是否可以帮助您,但是如果我尝试重现此内容,则右侧没有任何差距。 I think that your problem is that you are not adding MAs to the original df .我认为您的问题是您没有将 MA 添加到原始df Here's my code这是我的代码

%matplotlib inline
import pandas as pd
import numpy as np
N = 400
dates = pd.date_range(start='2018-01-01', periods=N, freq="T")
df = pd.DataFrame({"Date":dates, "Adj. Close":np.random.rand(N)})
for ma in [10,50, 100,200]:
    df["MA{}".format(ma)] = df["Adj. Close"].rolling(ma).mean()

df.plot(x="Date").legend(bbox_to_anchor=(1, 1));

I'm not sure this is 100% an answer but it had something to do with the data frame being created from most recent to least and then indexed.我不确定这是 100% 的答案,但它与从最近到最少创建然后索引的数据框有关。 By resetting the index, sorting in ascending order, and re-indexing back to date I got it to work.通过重置索引,按升序排序,并重新索引回到日期,我让它工作了。

MSFT1 = MSFT_data.sort_index(ascending = True)
MSFT1 = MSFT1.reset_index()
MSFT1 = MSFT1.set_index('Date')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM