import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader.data as web
%matplotlib inline
pd.set_option('display.max_colwidth', 200)
start = pd.to_datetime('2010-01-01')
end = pd.to_datetime('2017-01-01')
spy = web.get_data_yahoo('SPY', start, end)
spy.head()
Open | High | Low | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2010-01-04 | 112.370003 | 113.389999 | 111.510002 | 113.330002 | 118944600 | 98.214371 |
2010-01-05 | 113.260002 | 113.680000 | 112.849998 | 113.629997 | 111579900 | 98.474354 |
2010-01-06 | 113.519997 | 113.989998 | 113.430000 | 113.709999 | 116074400 | 98.543685 |
2010-01-07 | 113.500000 | 114.330002 | 113.180000 | 114.190002 | 131091100 | 98.959667 |
2010-01-08 | 113.889999 | 114.620003 | 113.660004 | 114.570000 | 126402800 | 99.288981 |
spy_adj_close = spy['Adj Close']
# adj close plotted
spy_adj_close.plot(subplots=True ,figsize=(10, 5), title='SPY', color='k')
array([<matplotlib.axes._subplots.AxesSubplot object at 0x119876390>], dtype=object)
first_open = spy['Open'].iloc[0]
first_open
112.370003
last_close = spy['Close'].iloc[-1]
last_close
223.52999900000003
# Total return between 2010-1-1 <> 2017-1-1
last_close - first_open
111.15999600000004
# Lets look at daily returns
spy['Daily Change'] = pd.Series(spy['Close'] - spy['Open'])
spy['Daily Change'].sum()
73.64011700000037