Using pandas rolling function

1. Set up environment

In [2]:
import numpy as np
import pandas as pd
import pandas_datareader as pdr
import matplotlib.pyplot as plt
%matplotlib inline

2. Get data from yahoo.finance

In [3]:
gld = pdr.get_data_yahoo('GLD', '2016-11-08')
gld.head()
Out[3]:
Open High Low Close Adj Close Volume
Date
2016-11-08 122.120003 123.089996 121.360001 121.639999 121.639999 9173700
2016-11-09 124.300003 124.349998 121.110001 121.559998 121.559998 18236500
2016-11-10 121.519997 121.540001 119.379997 119.750000 119.750000 22268000
2016-11-11 119.650002 119.690002 116.230003 117.099998 117.099998 20381800
2016-11-14 116.120003 117.139999 115.489998 116.110001 116.110001 20431800

3. Discard unneeded data

In [4]:
gld_close = pd.DataFrame(gld.Close)

4. Use rolling method to compute moving averages

In [10]:
gld_close['MA_9'] = gld_close.Close.rolling(9).mean().shift()
gld_close['MA_21'] = gld_close.Close.rolling(21).mean()
In [11]:
gld_close['MA_9'].head(12)
Out[11]:
Date
2016-11-08           NaN
2016-11-09           NaN
2016-11-10           NaN
2016-11-11           NaN
2016-11-14           NaN
2016-11-15           NaN
2016-11-16           NaN
2016-11-17           NaN
2016-11-18           NaN
2016-11-21    117.930000
2016-11-22    117.256667
2016-11-23    116.587778
Name: MA_9, dtype: float64

5. Plot data and moving averages

In [8]:
plt.figure(figsize=(15,10))
plt.grid(True)
plt.plot(gld_close['Close'],label='GLD')
plt.plot(gld_close['MA_9'], label='MA 9 day')
plt.plot(gld_close['MA_21'], label='MA 21 day')
plt.legend(loc=2)
Out[8]:
<matplotlib.legend.Legend at 0x11b6ccda0>

6. Compute log change (instantaneous rate of return)

In [12]:
gld_close["change"] = np.log(gld_close["Close"] / gld_close["Close"].shift())

7. Plot reveals noisy data centered around 0

In [13]:
plt.plot(gld_close.change)
Out[13]:
[<matplotlib.lines.Line2D at 0x11ba15748>]

8. Compute rolling historical volatility, offset using .shift() method

In [15]:
gld_close['Volatility'] = gld_close.change.rolling(21).std().shift()
gld_close['Volatility'].plot()
Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x11ba4bac8>

Part 2 some price analysis

In [86]:
#offset expected change 1-day
gld_close['exp_chng'] = gld_close['Volatility'] * gld_close['Close'].shift()
gld_close['actual_chng'] = gld_close['Close'] - gld_close['Close'].shift()
In [97]:
#running more than once will 
gld_close = pd.DataFrame(gld_close.iloc[22:])
In [98]:
gld_close['Magnitude'] = gld_close['actual_chng'] / gld_close['exp_chng'] 
In [102]:
gld_close['abs_magni'] = np.abs(gld_close['Magnitude'])
gld_close.head()
Out[102]:
Close MA_9 MA_21 log_chng Volatility exp_chng actual_chng Magnitude abs_magni
Date
2017-01-12 113.910004 112.055557 110.030000 0.003606 0.007895 0.896120 0.410004 0.457532 0.457532
2017-01-13 114.209999 112.566667 110.209048 0.002630 0.007892 0.899028 0.299995 0.333688 0.333688
2017-01-17 115.849998 113.164445 110.543333 0.014257 0.007824 0.893542 1.639999 1.835392 1.835392
2017-01-18 114.870003 113.610001 110.901905 -0.008495 0.007336 0.849917 -0.979995 -1.153048 1.153048
2017-01-19 114.769997 113.853333 111.221905 -0.000871 0.006803 0.781510 -0.100006 -0.127965 0.127965
In [103]:
plt.scatter(gld_close['actual_chng'], gld_close['abs_magni'])
Out[103]:
<matplotlib.collections.PathCollection at 0x11c663240>
In [ ]: