In [1]:
%run ../../code/version_check.py
In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import quandl
In [3]:
df = quandl.get("CFTC/FV_FO_ALL")
In [4]:
cols = ['Open Interest','Dealer Longs','Dealer Shorts',]
com = df[cols]
com.head()
Out[4]:
In [5]:
com.plot()
Out[5]:
In [6]:
# adj = com[['Dealer Longs', 'Dealer Shorts']] / com['Open Interest']
adj = com.div(com['Open Interest'], axis='index').drop('Open Interest', 1)
adj.plot(title='Adjusted Commerical Longs and Shorts')
Out[6]:
In [7]:
adj_net = (com['Dealer Longs'] - com['Dealer Shorts']) / com['Open Interest']
print(adj_net.head(1))
adj_net.plot()
Out[7]:
In [8]:
def cot(df):
max = df.max()
min = df.min()
current = df[len(df) -1]
return (current - min) / (max - min)
In [9]:
com_index = adj_net.rolling(window=26, min_periods=26).apply(func=cot)
In [10]:
producer_index = adj['Dealer Shorts'].rolling(window=26, min_periods=26).apply(func=cot)
In [11]:
consumer_index = adj['Dealer Longs'].rolling(window=26, min_periods=26).apply(func=cot)
In [12]:
mu = adj_net.mean()
mu
Out[12]:
In [13]:
sigma = adj_net.std()
sigma
Out[13]:
In [14]:
u_limit = mu + (2 * sigma)
u_limit
Out[14]:
In [15]:
l_limit = mu - (2 * sigma)
l_limit
Out[15]:
In [16]:
fig, ax = plt.subplots(nrows=4, ncols=1, figsize=(12,8), sharex=True)
adj_net.plot(color='blue',
grid=True,
linewidth=1,
yticks=[u_limit, mu, l_limit],
title='Commercial Control Chart (2 s.d.)',
ax=ax[0])
com_index.plot(color='blue',
grid=True,
linewidth=1,
yticks=[0.2, 0.8],
title='Commercial Index (Net Pressure)',
ax=ax[1])
producer_index.plot(color='blue',
grid=True,
linewidth=1,
yticks=[0.2, 0.8],
title='Producer Index (Selling Pressure)',
ax=ax[2])
consumer_index.plot(color='blue',
grid=True,
linewidth=1,
yticks=[0.2, 0.8],
title='Consumer Index (Buying Pressure)',
ax=ax[3])
for i in range(len(ax)):
ax[i].spines['top'].set_visible(False)
ax[i].spines['right'].set_visible(False)
ax[i].legend().set_visible(False)
In [17]:
diff = adj_net.diff()
diff.plot(figsize=(12,2))
Out[17]:
In [18]:
from pymc3 import Exponential, StudentT, Deterministic, Model
from pymc3.math import exp
from pymc3.distributions.timeseries import GaussianRandomWalk
with Model() as sp500_model:
nu = Exponential('nu', 1./10, testval=5.)
sigma = Exponential('sigma', 1./.02, testval=.1)
s = GaussianRandomWalk('s', sigma**-2, shape=len(diff))
volatility_process = Deterministic('volatility_process', exp(-2*s))
r = StudentT('r', nu, lam=1/volatility_process, observed=diff)
In [ ]:
from pymc3 import variational
import scipy
with sp500_model:
mu, sds, elbo = variational.advi(n=100000)
step = NUTS(scaling=sp500_model.dict_to_array(sds)**2, is_cov=True)
trace = sample(2000, step, start=mu, progressbar=True)
In [ ]:
traceplot(trace[200:], [nu, sigma]);
In [ ]:
import quandl.version as v
In [ ]:
quandl.version.VERSION