Let's plot the VIX, The Fear Index, From Scratch!
Introduction
It's sitting right at the teens - that usually means the markets are calm and orderly. Let's roll up our sleeves, download some data and visualize this using Python. I'll briefly discuss what the Vix is after we get our plot up and running for those that are in a hurry.
Code
from IPython.display import Image
Image(filename='viralml-book.png')
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import io, base64, os, json, re
import pandas as pd
import numpy as np
import datetime
import warnings
warnings.filterwarnings('ignore')
path_to_market_data = '/Users/manuel/Documents/financial-research/market-report/2019-07-06/'
sp500_df = pd.read_csv(path_to_market_data + '^GSPC.csv')
sp500_df['Date'] = pd.to_datetime(sp500_df['Date'])
sp500_df = sp500_df[['Date', 'Adj Close']]
sp500_df.columns = ['Date', 'SP500_Close']
print(np.min(sp500_df['Date'] ),np.max(sp500_df['Date'] ))
sp500_df = sp500_df.sort_values('Date', ascending=True)
sp500_df.tail()
vix_df = pd.read_csv(path_to_market_data + '^VIX.csv')
vix_df['Date'] = pd.to_datetime(vix_df['Date'])
vix_df = vix_df[['Date', 'Adj Close']]
vix_df.columns = ['Date', 'VIX_Close']
print(np.min(vix_df['Date'] ),np.max(vix_df['Date'] ))
vix_df = vix_df.sort_values('Date', ascending=True)
vix_df.tail()
vix_df_tmp = vix_df.copy()
sp500_df_tmp = sp500_df.copy()
cut_off_date = '1990-01-01'
vix_df_tmp = vix_df_tmp[vix_df_tmp['Date'] > cut_off_date]
sp500_df_tmp = sp500_df_tmp[sp500_df_tmp['Date'] > cut_off_date]
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(vix_df_tmp['Date'],vix_df_tmp['VIX_Close'], color='red', label='VIX')
plt.legend(loc='lower right')
plt.axhline(13, color='blue', linewidth=3, linestyle='-.' )
plt.title('VIX & SP500 - ' + str(np.max(vix_df_tmp['Date']))[0:10])
plt.grid()
ax2 = ax.twinx()
plt.plot(sp500_df_tmp['Date'],sp500_df_tmp['SP500_Close'], color='black',
label='SP500_Close')
plt.legend(loc='lower left')
plt.show()
Show Notes
(pardon typos and formatting -these are the notes I use to make the videos)
It's sitting right at the teens - that usually means the markets are calm and orderly. Let's roll up our sleeves, download some data and visualize this using Python. I'll briefly discuss what the Vix is after we get our plot up and running for those that are in a hurry.