In [14]:
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader.data as web
In [15]:
%matplotlib inline
pd.set_option('display.max_colwidth', 200)
In [16]:
start = pd.to_datetime('2010-01-01')
end = pd.to_datetime('2017-01-01')
In [17]:
spy = web.get_data_yahoo('SPY', start, end)
In [18]:
spy.head()
Out[18]:
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
In [30]:
spy_adj_close = spy['Adj Close']
In [31]:
# adj close plotted
spy_adj_close.plot(subplots=True ,figsize=(10, 5), title='SPY', color='k')
Out[31]:
array([<matplotlib.axes._subplots.AxesSubplot object at 0x119876390>], dtype=object)
In [33]:
first_open = spy['Open'].iloc[0]
first_open
Out[33]:
112.370003
In [34]:
last_close = spy['Close'].iloc[-1]
last_close
Out[34]:
223.52999900000003
In [35]:
# Total return between 2010-1-1 <> 2017-1-1
last_close - first_open
Out[35]:
111.15999600000004
In [36]:
# Lets look at daily returns
spy['Daily Change'] = pd.Series(spy['Close'] - spy['Open'])
In [39]:
spy['Daily Change'].sum()
Out[39]:
73.64011700000037
In [ ]: