Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot the rolling mean of stock data?

I was able to plot the data using the below code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

url = "http://real-chart.finance.yahoo.com/table.csv?s=YHOO&a=03&b=12&c=2006&d=01&e=9&f=2016&g=d&ignore=.csv"

df = pd.read_csv(url)
df.index = df["Date"]
df.sort_index(inplace=True)

df['Adj Close'].plot()
plt.show()

But now I want to calculate the rolling mean of the data and plot that. This is what I've tried:

pd.rolling_mean(df.resample("1D", fill_method="ffill"), window=3, min_periods=1)
plt.plot()

But this gives me the error:

Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex

All I want to do is plot the rolling mean of the data. Why is this happening?

like image 830
template boy Avatar asked Sep 26 '22 10:09

template boy


2 Answers

Why don't you just use the datareader?

import pandas.io.data as web

aapl = web.DataReader("aapl", 'yahoo', '2010-1-1')['Adj Close']
aapl.plot(title='AAPL Adj Close');pd.rolling_mean(aapl, 50).plot();pd.rolling_mean(aapl, 200).plot()

enter image description here

To give more control over the plotting:

aapl = web.DataReader("aapl", 'yahoo', '2010-1-1')['Adj Close']
aapl.name = 'Adj Close'
aapl_50ma = pd.rolling_mean(aapl, 50)
aapl_50ma.name = '50 day MA'
aapl_200ma = pd.rolling_mean(aapl, 200)
aapl_200ma.name = '200 day MA'
aapl.plot(title='AAPL', legend=True);aapl_50ma.plot(legend=True);aapl_200ma.plot(legend=True)

enter image description here

like image 179
Alexander Avatar answered Oct 19 '22 20:10

Alexander


Pandas isn't parsing your dates correctly, as it doesn't by default when loading CSVs. parse_dates either needs to be True, to parse the index, or a list of column numbers to parse. It's loading them as strings instead. Also, read_csv allows setting the index automatically. The following will work:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

url = "http://real-chart.finance.yahoo.com/table.csv?s=YHOO&a=03&b=12&c=2006&d=01&e=9&f=2016&g=d&ignore=.csv"

df = pd.read_csv(url, parse_dates=True, index_col=0)
#df.index = df["Date"]
df.sort_index(inplace=True)

df['Adj Close'].plot()
plt.show()

And then

rm = pd.rolling_mean(df.resample("1D", fill_method="ffill"), window=3, min_periods=1)
rm['Adj Close'].plot()

However, this latter bit of code is currently plotting and giving me an odd error that I need to look into. Note that in odd cases, in jupyter/ipython notebooks with inline plotting, this may give an error if you don't use the matplotlib/pylab magic before importing matplotlib.

like image 1
cge Avatar answered Oct 19 '22 18:10

cge