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!

Business Sentiment Indicators - Hands-On With Market Analysis with Python

Introduction

Let's get a handle on business sentiment by looking at the PMI, BCI, CPPI and, of course, the SPY, DOW, and Russel 2000. I'll show you where to get the data and run some simple charts to get a feel of what is going on.



If you liked it, please share it:

Code

ViralML-Measuring-Business-Sentiment-Hands-On-Market-Analysis-With-Python
In [1]:
from IPython.display import Image
Image(filename='viralml-book.png')
Out[1]:
In [2]:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.dates as mdates
from mpl_finance import candlestick_ohlc
mpl.style.use('default')
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-data/2019-09-16/'

Data Required

S&P 500 (^GSPC)
https://finance.yahoo.com/quote/%5EGSPC/history?p=%5EGSPC


Russell 2000 (^RUT)
https://finance.yahoo.com/quote/%5ERUT/history?p=^RUT


Dow Jones Industrial Average (^DJI)
https://finance.yahoo.com/quote/%5EDJI


BCI - Business confidence index

This business confidence indicator provides information on future developments, based upon opinion surveys on developments in production, orders and stocks of finished goods in the industry sector. It can be used to monitor output growth and to anticipate turning points in economic activity. Numbers above 100 suggest an increased confidence in near future business performance, and numbers below 100 indicate pessimism towards future performance.

https://data.oecd.org/leadind/business-confidence-index-bci.htm


PMI Composite Index

Purchasing Managers' Indexes (PMI) are economic indicators derived from monthly surveys of private sector companies.

https://www.quandl.com/data/ISM/MAN_PMI-PMI-Composite-Index


Green Street Commercial Property Price Index
https://www.greenstreetadvisors.com/insights/CPPI

In [3]:
 
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) # sort in ascending date order
sp500_df.tail()
1950-01-03 00:00:00 2019-09-16 00:00:00
Out[3]:
Date SP500_Close
17534 2019-09-10 2979.389893
17535 2019-09-11 3000.929932
17536 2019-09-12 3009.570068
17537 2019-09-13 3007.389893
17538 2019-09-16 2997.959961
In [4]:
ru2000_df = pd.read_csv(path_to_market_data + '^RUT.csv')
ru2000_df['Date'] = pd.to_datetime(ru2000_df['Date'])
ru2000_df = ru2000_df[['Date','Adj Close']]
ru2000_df.columns = ['Date', 'RUS_Close']
print(np.min(ru2000_df['Date'] ),np.max(ru2000_df['Date'] ))
ru2000_df = ru2000_df.sort_values('Date', ascending=True) # sort in ascending date order
ru2000_df.tail()
1987-09-10 00:00:00 2019-09-16 00:00:00
Out[4]:
Date RUS_Close
8064 2019-09-10 1541.609985
8065 2019-09-11 1575.709961
8066 2019-09-12 1575.069946
8067 2019-09-13 1578.140015
8068 2019-09-16 1584.599976

Dow Jones Industrial Average (^DJI)

https://finance.yahoo.com/quote/%5EDJI

In [5]:
dji_df = pd.read_csv(path_to_market_data + '^DJI.csv')
dji_df['Date'] = pd.to_datetime(dji_df['Date'])
dji_df = dji_df[['Date','Adj Close']]
dji_df.columns = ['Date', 'DOW_Close']
print(np.min(dji_df['Date'] ),np.max(dji_df['Date'] ))
dji_df = dji_df.sort_values('Date', ascending=True) # sort in ascending date order
dji_df.tail()
1985-01-29 00:00:00 2019-09-16 00:00:00
Out[5]:
Date DOW_Close
8724 2019-09-10 26909.429688
8725 2019-09-11 27137.039063
8726 2019-09-12 27182.449219
8727 2019-09-13 27219.519531
8728 2019-09-16 27076.820313
In [6]:
ru2000_df_tmp = ru2000_df.copy()
sp500_df_tmp = sp500_df.copy()
dji_df_tmp = dji_df.copy()

cut_off_date = '2015-01-01'
ru2000_df_tmp = ru2000_df_tmp[ru2000_df_tmp['Date'] >= cut_off_date]
sp500_df_tmp = sp500_df_tmp[sp500_df_tmp['Date'] >= cut_off_date]
dji_df_tmp = dji_df_tmp[dji_df_tmp['Date'] >= cut_off_date]

fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(ru2000_df_tmp['Date'], ru2000_df_tmp['RUS_Close'], label='RUS_Close', color='blue')
plt.legend(loc='lower right')
plt.title('RUS_Close & SP500 - ' + str(np.max(ru2000_df_tmp['Date']))[0:10])
plt.grid()

# Get second axis
ax2 = ax.twinx()
plt.plot(sp500_df_tmp['Date'],  sp500_df_tmp['SP500_Close'], label='SP500_Close',color='green')
plt.legend(loc='upper left')

ax3 = ax.twinx()
plt.plot(dji_df_tmp['Date'],  dji_df_tmp['DOW_Close'], label='DOW_Close',color='red')
plt.legend(loc='lower left')
plt.show()
In [7]:
bci_df = pd.read_csv(path_to_market_data + 'DP_LIVE_17092019082247898.csv')
bci_df = bci_df[bci_df['LOCATION'] == 'USA']
bci_df['Date'] = bci_df['TIME'] + '-01'
bci_df['Date'] = pd.to_datetime(bci_df['Date'])
bci_df = bci_df.sort_values('Date', ascending=True) # sort in ascending date order
bci_df = bci_df[['Date','Value']]
bci_df.columns = ['Date', 'BCI']
print(np.min(bci_df['Date'] ),np.max(bci_df['Date'] ))
bci_df.tail()
1950-01-01 00:00:00 2019-08-01 00:00:00
Out[7]:
Date BCI
8559 2019-04-01 100.00640
8560 2019-05-01 99.82034
8561 2019-06-01 99.65862
8562 2019-07-01 99.46520
8563 2019-08-01 99.20241
In [8]:
bci_df_tmp = bci_df.copy()
sp500_df_tmp = sp500_df.copy()

cut_off_date = '2007-01-01'
bci_df_tmp = bci_df_tmp[bci_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(bci_df_tmp['Date'], bci_df_tmp['BCI'], label='BCI', color='blue')
plt.legend(loc='lower right')
plt.title('BCI & SP500 - ' + str(np.max(bci_df_tmp['Date']))[0:10])
plt.grid()

plt.axhline(100, color='gray', linewidth=3, linestyle='-.' )

# Get second axis
ax2 = ax.twinx()
plt.plot(sp500_df_tmp['Date'],  sp500_df_tmp['SP500_Close'], label='SP500_Close',color='black')
plt.legend(loc='upper left')
plt.show()

Purchasing Managers' Index - PMI Composite Index

https://www.quandl.com/data/ISM/MAN_PMI-PMI-Composite-Index

In [9]:
pmi_df = pd.read_csv(path_to_market_data + 'ISM-MAN_PMI.csv')
pmi_df['Date'] = pd.to_datetime(pmi_df['Date'])
print(np.min(pmi_df['Date'] ),np.max(pmi_df['Date'] ))
pmi_df = pmi_df.sort_values('Date', ascending=True) # sort in ascending date order
pmi_df.tail()
1948-01-01 00:00:00 2019-08-01 00:00:00
Out[9]:
Date PMI
4 2019-04-01 52.8
3 2019-05-01 52.1
2 2019-06-01 51.7
1 2019-07-01 51.2
0 2019-08-01 49.1
In [10]:
pmi_df_tmp = pmi_df.copy()
sp500_df_tmp = sp500_df.copy()

cut_off_date = '2007-01-01'
pmi_df_tmp = pmi_df_tmp[pmi_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(pmi_df_tmp['Date'], pmi_df_tmp['PMI'], label='PMI', color='blue')
plt.legend(loc='lower right')
plt.title('PMI & SP500 - ' + str(np.max(pmi_df_tmp['Date']))[0:10])
plt.grid()

plt.axhline(50, color='gray', linewidth=3, linestyle='-.' )

# Get second axis
ax2 = ax.twinx()
plt.plot(sp500_df_tmp['Date'],  sp500_df_tmp['SP500_Close'], label='SP500_Close',color='black')
plt.legend(loc='upper left')
plt.show()

Real Estate

Green Street Commercial Property Price Index

https://www.greenstreetadvisors.com/insights/CPPI

In [11]:
cppi_df = pd.read_csv(path_to_market_data + 'CPPI.csv') #, sep='\t')
cppi_df['Date'] = pd.to_datetime(cppi_df['Date'])
cppi_df.columns = ['Date', 'CPPI_All_Property' , 'CPPI_Core_Sector']
print('cppi_df:', np.min(cppi_df['Date']), np.max(cppi_df['Date']))
cppi_df.tail()
cppi_df: 1997-12-01 00:00:00 2019-08-01 00:00:00
Out[11]:
Date CPPI_All_Property CPPI_Core_Sector
256 2019-04-01 131.9 129.9
257 2019-05-01 132.9 131.1
258 2019-06-01 133.4 132.3
259 2019-07-01 133.4 132.4
260 2019-08-01 133.8 132.7
In [12]:
cppi_df_tmp = cppi_df.copy()
sp500_df_tmp = sp500_df.copy()

cut_off_date = '2007-01-01'
cppi_df_tmp = cppi_df_tmp[cppi_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(cppi_df_tmp['Date'], cppi_df_tmp['CPPI_All_Property'], label='CPPI_All_Property', color='blue')
plt.legend(loc='lower right')
plt.title('CPPI_All_Property & SP500 - ' + str(np.max(cppi_df_tmp['Date']))[0:10])
plt.grid()

# Get second axis
ax2 = ax.twinx()
plt.plot(sp500_df_tmp['Date'],  sp500_df_tmp['SP500_Close'], label='SP500_Close',color='black')
plt.legend(loc='upper left')
plt.show()

Show Notes

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

Let's get a handle on business sentiment by looking at the PMI, BCI, CPPI and, of course, the SPY, DOW, and Russel 2000. I'll show you where to get the data and run some simple charts to get a feel of what is going on.