import numpy as np
import pandas as pd
import pandas_datareader as pdr
import matplotlib.pyplot as plt
%matplotlib inline
gld = pdr.get_data_yahoo('GLD', '2016-11-08')
gld.head()
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 |
gld_close = pd.DataFrame(gld.Close)
gld_close['MA_9'] = gld_close.Close.rolling(9).mean().shift()
gld_close['MA_21'] = gld_close.Close.rolling(21).mean()
gld_close['MA_9'].head(12)
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
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)
<matplotlib.legend.Legend at 0x11b6ccda0>
gld_close["change"] = np.log(gld_close["Close"] / gld_close["Close"].shift())
plt.plot(gld_close.change)
[<matplotlib.lines.Line2D at 0x11ba15748>]
gld_close['Volatility'] = gld_close.change.rolling(21).std().shift()
gld_close['Volatility'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x11ba4bac8>
#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()
#running more than once will
gld_close = pd.DataFrame(gld_close.iloc[22:])
gld_close['Magnitude'] = gld_close['actual_chng'] / gld_close['exp_chng']
gld_close['abs_magni'] = np.abs(gld_close['Magnitude'])
gld_close.head()
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 |
plt.scatter(gld_close['actual_chng'], gld_close['abs_magni'])
<matplotlib.collections.PathCollection at 0x11c663240>