### My New Udemy Class: Fundamental Market Analysis with Python

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

### Hot off the Press!

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

### Grow Your Web Brand, Visibility & Traffic Organically

5 Years of amunategui.github.Io and the Lessons I Learned from Growing My Online Community from the Ground Up

### Introduction

Were seeing the lowest unemployment numbers in 49 years!! Were all seeing a shrinking labor pool - what does this all mean?

### Code

ViralML-Hands-On-Unemployment-Labor-Pool-vs-SP500
In [156]:
from IPython.display import Image
Image(filename='../viralml-book.png')

Out[156]:

# Unemployment, Labor Pool and the S&P500¶

Let's Compare overlay the Unemployment Rates on the S&P500 Let's continue exploring the stock market with Python. We've looked at the VIX, we've looked at the long and short-term bonds, Now let's take into the unemployment numbers.

You can find the unemployment rates directly from the Bureau of Labor Statistics: https://data.bls.gov/timeseries/lns14000000

Civilian Labor Force Participation Rate:

https://fred.stlouisfed.org/series/CIVPART

https://fred.stlouisfed.org/categories/12

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

In [157]:
%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

In [158]:
# get the SP500 (^GSPC) from Yahoo Finace
sp500_df['Date'] = pd.to_datetime(sp500_df['Date'])
sp500_df.columns = ['Date', 'SP500_close']
sp500_df['SP500_pct_change'] = sp500_df['SP500_close'].pct_change()

Out[158]:
Date SP500_close SP500_pct_change
0 1950-01-03 16.66 NaN
1 1950-01-04 16.85 0.011405
2 1950-01-05 16.93 0.004748
3 1950-01-06 16.98 0.002953
4 1950-01-09 17.08 0.005889
In [159]:
# loading employment data
# https://data.bls.gov/timeseries/lns14000000

Out[159]:
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
0 1948 3.4 3.8 4.0 3.9 3.5 3.6 3.6 3.9 3.8 3.7 3.8 4.0
1 1949 4.3 4.7 5.0 5.3 6.1 6.2 6.7 6.8 6.6 7.9 6.4 6.6
2 1950 6.5 6.4 6.3 5.8 5.5 5.4 5.0 4.5 4.4 4.2 4.2 4.3
3 1951 3.7 3.4 3.4 3.1 3.0 3.2 3.1 3.1 3.3 3.5 3.5 3.1
4 1952 3.2 3.1 2.9 2.9 3.0 3.0 3.2 3.4 3.1 3.0 2.8 2.7
In [160]:
def monthToNum(shortMonth):
return{'Jan' : 1,
'Feb' : 2,
'Mar' : 3,
'Apr' : 4,
'May' : 5,
'Jun' : 6,
'Jul' : 7,
'Aug' : 8,
'Sep' : 9,
'Oct' : 10,
'Nov' : 11,
'Dec' : 12
}[shortMonth]

monthToNum('Feb')

Out[160]:
2
In [161]:
# stack the data together to have on row per month
unemployment_df = (unemployment_df.set_index(['Year'])
.stack().reset_index(name='Unemployment')
.rename(columns={'level_1':'Month' }))

unemployment_df['Month'] = [monthToNum(m) for m in unemployment_df['Month'].values ]
unemployment_df.tail()

Out[161]:
Year Month Unemployment
851 2018 12 3.9
852 2019 1 4.0
853 2019 2 3.8
854 2019 3 3.8
855 2019 4 3.6
In [162]:
# format the year and the month into an actual date
unemployment_df['Date'] = [str(y) + '-' + str(m) + '-' + '01' for y,m in zip(unemployment_df['Year'].values, unemployment_df['Month'].values)]
unemployment_df = unemployment_df[['Date','Unemployment']]
unemployment_df['Unemployment_pct_change'] = unemployment_df['Unemployment'].pct_change()
unemployment_df['Date'] = pd.to_datetime(unemployment_df['Date'])

Out[162]:
Date Unemployment Unemployment_pct_change
0 1948-01-01 3.4 NaN
1 1948-02-01 3.8 0.117647
2 1948-03-01 4.0 0.052632
3 1948-04-01 3.9 -0.025000
4 1948-05-01 3.5 -0.102564
In [163]:
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(unemployment_df['Date'], unemployment_df['Unemployment'], color='blue')
plt.legend()
plt.grid()
plt.title('Labor Force Statistics from the Current Population Survey and the S&P 500')

ax2 = ax.twinx()
plt.plot(sp500_df['Date'],
sp500_df['SP500_close'], color='green', label='SP500_close', linestyle= 'dotted')

ax2.legend(loc='lower right', frameon=False)
plt.show()

In [164]:
# cut off data for further analysis
cut_off_date = '1980-01-01'
unemployment_df = unemployment_df[unemployment_df['Date'] >= cut_off_date]
sp500_df = sp500_df[sp500_df['Date'] >= cut_off_date]

fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(unemployment_df['Date'], unemployment_df['Unemployment'], color='blue')
plt.legend()
plt.grid()
plt.title('Labor Force Statistics from the Current Population Survey and the S&P 500')

ax2 = ax.twinx()
plt.plot(sp500_df['Date'],
sp500_df['SP500_close'], color='green', label='SP500_close', linestyle= 'dotted')

ax2.legend(loc='lower right', frameon=False)
plt.show()

In [149]:
unemployment_df['Diff'] = unemployment_df['Unemployment_pct_change']
unemployment_df['Diff'] = unemployment_df['Diff'].rolling(window=50).mean().values

fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(unemployment_df['Date'],
unemployment_df['Diff'],
color='black', label='Unemployment_pct_change')
plt.axhline(0)
plt.title('Unemployment_pct_change vs SP500')
plt.legend()
plt.grid()

# background colors
unemployment_df['i'] = range(1,len(unemployment_df)+1)

# track whenever the percentage change is above or b
pos_1 = unemployment_df[unemployment_df['Diff'] >= 0]['i']
neg_1 = unemployment_df[unemployment_df['Diff'] < 0]['i']

for x in pos_1:
ax.axvline(unemployment_df[unemployment_df['i']==x]['Date'].values, color='blue',linewidth=1,alpha=0.3)
for x in neg_1:
ax.axvline(unemployment_df[unemployment_df['i']==x]['Date'].values, color='red',linewidth=1,alpha=0.3)

ax2 = ax.twinx()
plt.plot(sp500_df['Date'],
sp500_df['SP500_close'], color='green', label='SP500_close', linestyle= 'dotted')

ax2.legend(loc='lower right', frameon=False)
plt.show()


# Comparing Unemployment VS Labor Force¶

In [165]:
# https://fred.stlouisfed.org/series/CIVPART
labor_df.columns = ['Date', 'Labor_Participation']
labor_df['Date'] = pd.to_datetime(labor_df['Date'])
labor_df['Labor_Participation_pct_change'] = labor_df['Labor_Participation'].pct_change()

Out[165]:
Date Labor_Participation Labor_Participation_pct_change
0 1948-01-01 58.6 NaN
1 1948-02-01 58.9 0.005119
2 1948-03-01 58.5 -0.006791
3 1948-04-01 59.0 0.008547
4 1948-05-01 58.3 -0.011864
In [166]:
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(labor_df['Date'], labor_df['Labor_Participation'], color='blue')
plt.legend()
plt.grid()
plt.title('Labor Participation and the S&P 500')

ax2 = ax.twinx()
plt.plot(sp500_df['Date'],
sp500_df['SP500_close'], color='green', label='SP500_close', linestyle= 'dotted')

ax2.legend(loc='lower right', frameon=False)
plt.show()


# All Together Now!¶

In [167]:
cut_off_date = '1980-01-01'
labor_df = labor_df[labor_df['Date'] >= cut_off_date]

fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(labor_df['Date'], labor_df['Labor_Participation'], color='red',
label='Labor_Participation')
plt.legend()
plt.grid()
plt.title('Labor Participation 24-54 and the S&P 500')

ax2 = ax.twinx()
plt.plot(sp500_df['Date'],
sp500_df['SP500_close'], color='green', label='SP500_close', linestyle= 'dotted')

ax2.legend(loc='lower right', frameon=False)

ax3 = ax.twinx()
plt.plot(unemployment_df['Date'],
unemployment_df['Unemployment'], color='blue', label='unemployment')

ax3.legend(loc='upper left', frameon=False)
plt.show()


### Show Notes

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