Get the "Applied Data Science Edge"!

The ViralML School

Fundamental Market Analysis with Python - Find Your Own Answers On What Is Going on in the Financial Markets

Web Work

Python Web Work - Prototyping Guide for Maker

Use HTML5 Templates, Serve Dynamic Content, Build Machine Learning Web Apps, Grow Audiences & Conquer the World!

Hot off the Press!

The Little Book of Fundamental Market Indicators

My New Book: "The Little Book of Fundamental Analysis: Hands-On Market Analysis with Python" is Out!

Charting Financial Markets Objectively with Python and Matplotlib during Trying Times - Hands-on

Introduction

It is easy to chart the markets with Python and great data sources like Yahoo Finance. Let's look at airline ETFs, the VIX, the S&P500, Johnson and Johnson and more! Get your own answers to your questions without having to rely on the Internet, newspapers and TV. Avoid the emotions and the drama and get your own objective answers.



If you liked it, please share it:

Code

ViralML-Coronavirus-and-Markets
In [2]:
from IPython.display import Image
Image(filename='viralml-book.png')
Out[2]:

Getting Your Market Bearings in Turbulent Times with Python

Companion book: "The Little Book of Fundamental Market Indicators":

https://amzn.to/2DERG3d

More at:

https://www.viralml.com/

The Boeing Company

The Boeing Company (BA)

Airline ETFs

iShares Transportation Average ETF: IYT

U.S. Global Jets ETF: JETS

Healthcare/Chemicals ETFs

iShares U.S. Healthcare Providers ETF: IHF

Johnson & Johnson (owns Purell/Pfizer): JNJ

VIX - The Fear Index

Download the S&P500 index (^GSPC): https://finance.yahoo.com/quote/%5EGSPC/

and the VIX (^VIX): https://finance.yahoo.com/quote/%5EVIX

In [3]:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import datetime
path_to_market_data = '/Users/manuel/Documents/financial-research/market-data/2020-03-11/'

sp500_df = pd.read_csv(path_to_market_data + '^GSPC.csv')
sp500_df['Date'] = pd.to_datetime(sp500_df['Date'])
sp500_df = sp500_df.sort_values('Date', ascending=True)
sp500_df.tail()
Out[3]:
Date Open High Low Close Adj Close Volume
23152 2020-03-05 3075.699951 3083.040039 2999.830078 3023.939941 3023.939941 5575550000
23153 2020-03-06 2954.199951 2985.929932 2901.540039 2972.370117 2972.370117 6552140000
23154 2020-03-09 2863.889893 2863.889893 2734.429932 2746.560059 2746.560059 8423050000
23155 2020-03-10 2813.479980 2882.590088 2734.000000 2882.229980 2882.229980 7635960000
23156 2020-03-11 2825.600098 2825.600098 2707.219971 2741.379883 2741.379883 7374110000

Load The VIX and the S&P 500 Historical Data

In [4]:
vix_df = pd.read_csv(path_to_market_data + '^VIX.csv')
vix_df['Date'] = pd.to_datetime(vix_df['Date'])
vix_df.tail()
Out[4]:
Date Open High Low Close Adj Close Volume
7602 2020-03-05 33.610001 42.840000 33.540001 39.619999 39.619999 0
7603 2020-03-06 48.930000 54.389999 40.840000 41.939999 41.939999 0
7604 2020-03-09 41.939999 62.119999 41.939999 54.459999 54.459999 0
7605 2020-03-10 49.680000 55.660000 43.560001 47.299999 47.299999 0
7606 2020-03-11 52.240002 55.820000 49.980000 53.900002 53.900002 0
In [5]:
# date range available
print('VIX:', np.min(vix_df['Date']), np.max(vix_df['Date']))
# date range available
print('SP500:', np.min(sp500_df['Date']), np.max(sp500_df['Date']))
 
VIX: 1990-01-02 00:00:00 2020-03-11 00:00:00
SP500: 1927-12-30 00:00:00 2020-03-11 00:00:00
In [9]:
# cut off data for visualization 
cut_off_date = '2016-01-01'
sp500_df = sp500_df[sp500_df['Date'] >= cut_off_date]
vix_df = vix_df[vix_df['Date'] >= cut_off_date]

Simple Visualization with the S&P 500 & VIX

In [10]:
fig, ax = plt.subplots(figsize=(16, 8))

plt.plot(vix_df['Date'], vix_df['Adj Close'], color='blue', label='VIX')
ax.legend(loc='upper left', frameon=False)
plt.title('VIX and S&P 500')
plt.grid()

# Get second axis
ax2 = ax.twinx()
plt.plot(sp500_df['Date'], 
         sp500_df['Adj Close'], label='S&P500', color='green')
ax2.legend(loc='lower right', frameon=False)

plt.show()

Let's clean this up with a rolling average

In [12]:
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(vix_df['Date'], vix_df['Adj Close'].rolling(window=20).mean().values, 
         color='blue', label='VIX')
ax.legend(loc='upper left', frameon=False)
plt.title('VIX and S&P 500')
plt.grid()
plt.axhline(15, color='red')


# Get second axis
ax2 = ax.twinx()
plt.plot(sp500_df['Date'], 
         sp500_df['Adj Close'], label='S&P500', color='green', linewidth=5)
ax2.legend(loc='lower right', frameon=False)

plt.show()

fig, ax = plt.subplots(figsize=(16, 4)) 
ax.bar(sp500_df['Date'], sp500_df['Volume'] ,color='black', width=2, label='Hist')
ax.axhline(y=0)
plt.grid()


plt.show()

Transportation

In [13]:
iyt_df = pd.read_csv(path_to_market_data + 'IYT.csv')
iyt_df['Date'] = pd.to_datetime(iyt_df['Date'])
iyt_df.head()
Out[13]:
Date Open High Low Close Adj Close Volume
0 2004-01-02 54.000000 54.279999 54.000000 54.279999 45.627151 2300
1 2004-01-05 54.150002 54.150002 53.889999 54.130001 45.501064 3500
2 2004-01-06 54.040001 54.380001 54.009998 54.299999 45.643967 52800
3 2004-01-07 53.799999 54.139999 53.730000 54.119999 45.492653 7000
4 2004-01-08 54.279999 54.279999 53.990002 53.990002 45.383381 5000
In [14]:
jets_df = pd.read_csv(path_to_market_data + 'JETS.csv')
jets_df['Date'] = pd.to_datetime(jets_df['Date'])
jets_df.head()
Out[14]:
Date Open High Low Close Adj Close Volume
0 2015-04-30 24.493999 24.579000 24.129999 24.188000 23.209225 7300
1 2015-05-01 24.700001 24.969000 24.379999 24.870001 23.863626 182800
2 2015-05-04 25.100000 25.100000 24.600000 24.660000 23.662123 209000
3 2015-05-05 24.480000 24.480000 24.000000 24.000000 23.028830 75400
4 2015-05-06 24.010000 24.110001 23.590000 23.980000 23.009640 36900
In [15]:
# cut off data for visualization 
cut_off_date = '2016-01-01'
jets_df = jets_df[jets_df['Date'] >= cut_off_date]
iyt_df = iyt_df[iyt_df['Date'] >= cut_off_date]
In [16]:
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(iyt_df['Date'], iyt_df['Adj Close'], color='blue', label='IYT')
ax.legend(loc='upper left', frameon=False)
plt.title('IYT and JETS')
plt.grid()

# Get second axis
ax2 = ax.twinx()
plt.plot(jets_df['Date'], 
         jets_df['Adj Close'], label='JETS', color='green')
ax2.legend(loc='lower right', frameon=False)

plt.show()
In [17]:
ba_df = pd.read_csv(path_to_market_data + 'BA.csv')
ba_df['Date'] = pd.to_datetime(ba_df['Date'])
ba_df = ba_df[ba_df['Date'] >= '2010-01-01']
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(ba_df['Date'], ba_df['Adj Close'], color='black', label='BA')
ax.legend(loc='upper left', frameon=False)
plt.title('BA')
plt.grid()


fig, ax = plt.subplots(figsize=(16, 4)) 
ax.bar(ba_df['Date'], ba_df['Volume'] ,color='blue', width=2, label='Hist')
ax.axhline(y=0)
plt.grid()

Healthcare

In [18]:
ihf_df = pd.read_csv(path_to_market_data + 'IHF.csv')
ihf_df['Date'] = pd.to_datetime(ihf_df['Date'])
ihf_df.head()
Out[18]:
Date Open High Low Close Adj Close Volume
0 2006-05-05 48.849998 48.849998 48.849998 48.849998 44.909374 100
1 2006-05-08 49.250000 49.250000 49.070000 49.070000 45.111641 4000
2 2006-05-09 48.900002 48.939999 48.119999 48.209999 44.320995 197700
3 2006-05-10 48.700001 49.139999 48.700001 48.959999 45.010506 5100
4 2006-05-11 48.700001 48.700001 48.459999 48.500000 44.587605 3900
In [19]:
jnj_df = pd.read_csv(path_to_market_data + 'JNJ.csv')
jnj_df['Date'] = pd.to_datetime(jnj_df['Date'])
jnj_df.head()
Out[19]:
Date Open High Low Close Adj Close Volume
0 1962-01-02 0.0 0.223380 0.222222 0.223380 0.000001 0
1 1962-01-03 0.0 0.221065 0.219907 0.219907 0.000001 345600
2 1962-01-04 0.0 0.221065 0.217593 0.217593 0.000001 216000
3 1962-01-05 0.0 0.215856 0.214120 0.214120 0.000001 129600
4 1962-01-08 0.0 0.212384 0.210648 0.210648 0.000001 172800
In [20]:
# cut off data for visualization 
cut_off_date = '2016-01-01'
ihf_df = ihf_df[ihf_df['Date'] >= cut_off_date]
jnj_df = jnj_df[jnj_df['Date'] >= cut_off_date]
In [21]:
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(ihf_df['Date'], ihf_df['Adj Close'], color='blue', label='IHF')
ax.legend(loc='upper left', frameon=False)
plt.title('IHF and JNJ')
plt.grid()

# Get second axis
ax2 = ax.twinx()
plt.plot(jnj_df['Date'], 
         jnj_df['Adj Close'], label='JNJ', color='green')
ax2.legend(loc='lower right', frameon=False)

plt.show()

Show Notes

(pardon typos and formatting -
these are the notes I use to make the videos)

It is easy to chart the markets with Python and great data sources like Yahoo Finance. Let's look at airline ETFs, the VIX, the S&P500, Johnson and Johnson and more! Get your own answers to your questions without having to rely on the Internet, newspapers and TV. Avoid the emotions and the drama and get your own objective answers.