Last active
February 8, 2017 09:28
-
-
Save fanannan/d546d1ebd82b9e356900bb07e1770f08 to your computer and use it in GitHub Desktop.
Draw 'possible range' of VIX movement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import numpy as np | |
import pandas as pd | |
import datetime as dt | |
from pandas.io.data import DataReader | |
from joblib import Memory | |
import matplotlib as mpl | |
import matplotlib.pyplot as plt | |
# | |
memory = Memory('/tmp/') | |
TICKER_LIST = ['^VIX'] | |
@memory.cache() | |
def read_yahoo_data(tickers): | |
lis = list() | |
for ticker in tickers: | |
df = DataReader(ticker, 'yahoo') | |
adjustment = df['Adj Close']/df['Close'] | |
df['Open'] *= adjustment | |
df['High'] *= adjustment | |
df['Low'] *= adjustment | |
df['Close'] = df['Adj Close'] | |
del df['Adj Close'] | |
for c in df.columns: | |
df = df.rename(columns={c: ticker.replace('^', '').lower()+'_'+c.replace(' ', '').lower()}) | |
lis.append(df) | |
df_aggregated = pd.concat(lis, axis=1) | |
return df_aggregated | |
def get_vix_bands(df): | |
df_vix_high = df['vix_high'] | |
df_vix_low = df['vix_low'] | |
df_vix_close = df['vix_close'] | |
df_top = df_vix_high.rolling(window=250, min_periods=3).quantile(0.9) | |
df_higher = df_vix_high.rolling(window=60, min_periods=3).quantile(0.9) | |
df_high = df_vix_high.rolling(window=30, min_periods=3).quantile(0.9) | |
df_low = df_vix_low.rolling(window=30, min_periods=3).quantile(0.1) | |
df_lower = df_vix_low.rolling(window=60, min_periods=3).quantile(0.1) | |
df_bottom = df_vix_low.rolling(window=250, min_periods=3).quantile(0.1) | |
return df_vix_high, df_vix_low, df_vix_close, df_top, df_higher, df_high, df_low, df_lower, df_bottom | |
def draw_vix_bands(df, todays_high=None, todays_low=None, todays_close=None): | |
df = df.copy() | |
df_latest = pd.DataFrame([], index=[], columns=['vix_high', 'vix_low', 'vix_close']) | |
if todays_high is not None or todays_low is not None or todays_close is not None: | |
today = dt.date.today() | |
df_latest = pd.DataFrame([[todays_high, todays_low, todays_close], | |
[np.nan, np.nan, np.nan], | |
[np.nan, np.nan, np.nan], | |
[np.nan, np.nan, np.nan], | |
[np.nan, np.nan, np.nan], | |
], | |
index=[today+dt.timedelta(days=n) for n in range(0, 5)], | |
columns=['vix_high', 'vix_low', 'vix_close']) | |
df = df.append(df_latest) | |
df_vix_high, df_vix_low, df_vix_close, df_top, df_higher, df_high, df_low, df_lower, df_bottom = get_vix_bands(df) | |
x = df.index | |
fig = plt.figure(figsize=(20,6)) | |
ax = fig.add_subplot(1,1,1) | |
ax2 = ax.twinx() | |
ax.set_title('vix range') | |
ax.set_ylim((10, 50)) | |
ax2.set_ylim((10, 50)) | |
ax.set_xlim((x[0], x[-1])) | |
ax2.set_xlim((x[0], x[-1])) | |
ax2.fill_between(x, df_top.shift(1).values, df_bottom.shift(1).values, color='#888888', alpha=.1) | |
ax2.fill_between(x, df_higher.shift(1).values, df_lower.shift(1).values, color='#888888', alpha=.2) | |
ax2.fill_between(x, df_high.shift(1).values, df_low.shift(1).values, color='#888888', alpha=.4) | |
ax.plot(x, df_top.shift(1).values, color='#ff0000', alpha=0.4) | |
ax.plot(x, df_higher.shift(1).values, color='#dd0000', alpha=0.3) | |
ax.plot(x, df_high.shift(1).values, color='#880000', alpha=0.3) | |
ax.plot(x, df_low.shift(1).values, color='#008800', alpha=0.3) | |
ax.plot(x, df_lower.shift(1).values, color='#00dd00', alpha=0.3) | |
ax.plot(x, df_bottom.shift(1).values, color='#00ff00', alpha=0.4) | |
ax.plot(x, df_vix_close.values, linewidth=1.7, color='#0000ff', alpha=0.9) | |
ax.tick_params(axis='both', direction='out', width=1, length=5, labelsize=14, pad=8) | |
plt.yticks([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30, 35, 40, 45, 50]) | |
ax.grid() | |
# ax.text(0, 0, 'top {}'.format(df_top.shift(1).values[-15]), | |
# horizontalalignment='center', | |
# verticalalignment='bottom') | |
#ax.annotate('figure pixels', xy=(x[-1], close.values[-1]), xycoords='figure pixels') | |
#print(df.tail(12)) | |
offset = -len(df_latest) | |
p = df_vix_close.values[offset] | |
print('top : {0:2.2f}({1:02.2f}%)'.format(df_top.shift(1).values[offset], | |
(df_top.shift(1).values[offset]/p-1)*100)) | |
print('upper : {0:2.2f}({1:02.2f}%)'.format(df_higher.shift(1).values[offset], | |
(df_higher.shift(1).values[offset]/p-1)*100)) | |
print('up : {0:2.2f}({1:02.2f}%)'.format(df_high.shift(1).values[offset], | |
(df_high.shift(1).values[offset]/p-1)*100)) | |
print('low : {0:2.2f}({1:02.2f}%)'.format(df_low.shift(1).values[offset], | |
(df_low.shift(1).values[offset]/p-1)*100)) | |
print('lower : {0:2.2f}({1:02.2f}%)'.format(df_lower.shift(1).values[offset], | |
(df_lower.shift(1).values[offset]/p-1)*100)) | |
print('bottom: {0:2.2f}({1:02.2f}%)'.format(df_bottom.shift(1).values[offset], | |
(df_bottom.shift(1).values[offset]/p-1)*100)) | |
print('last : {0:2.2f}'.format(p)) | |
fig.savefig('/tmp/vix_range_band.png') | |
plt.show() | |
if True: | |
df = read_yahoo_data(TICKER_LIST) | |
print(df) | |
draw_vix_bands(df, todays_high=13.0, todays_low=12.0, todays_close=12.15) | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment