Dow Jones Transportation Average Diverting, Trouble Ahead? Hands-On Market Analysis with Python
Introduction
Dow Jones Transportation Average Diverting, Trouble Ahead? What about the rumblings in the market that transportation companies are slowing down? It's all related to the Dow theory that states that both industrials and transportation need to be moving upwards for a healthy market. Let's find out for ourselves with raw data and Python!
Code
from IPython.display import Image
Image(filename='viralml-book.png')
Fundamental and Technical Indicators - Hands-On Market Analysis¶
Companion book: "The Little Book of Fundamental Market Indicators":
More at:
Charles Dow and the Dow Jones Transportation and Industrial Indexes¶
https://www.investopedia.com/terms/d/dowtheory.asp
The Dow theory is a theory that says the market is in an upward trend if one of its averages (industrial or transportation) advances above a previous important h igh and is accompanied or followed by a similar advance in the other average.
Data¶
Dow Jones Industrial Average (^DJI) https://finance.yahoo.com/quote/%5EDJI
Dow Jones Transportation Average (DJTA) https://fred.stlouisfed.org/series/DJTA
CSX Corporation (CSX)¶
CSX Corporation is an American holding company focused on rail transportation and real estate in North America, among other industries
%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-data/2019-07-20/'
# create an MACD indicators
def MACD(df, feature, fast=9, medium=12, long=26):
# WARNING - Feed data in ascending order only (i.e. first row should be your oldest print)
tmp_df = df.copy()
tmp_df['EXP1'] = tmp_df[feature].ewm(span=medium, adjust=False).mean()
tmp_df['EXP2'] = tmp_df[feature].ewm(span=long, adjust=False).mean()
tmp_df['MACD'] = tmp_df['EXP1']-tmp_df['EXP2']
tmp_df['EXP3'] = tmp_df['MACD'].ewm(span=fast, adjust=False).mean()
tmp_df['MACD_Hist'] = tmp_df['MACD'] - tmp_df['EXP3']
return tmp_df
Data¶
# DJI
dow_df = pd.read_csv(path_to_market_data + '^DJI.csv')
dow_df['Date'] = pd.to_datetime(dow_df['Date'])
dow_df = dow_df[['Date','Adj Close']]
dow_df.columns = ['Date', 'DOW_Close']
print(np.min(dow_df['Date'] ),np.max(dow_df['Date'] ))
dow_df = dow_df.sort_values('Date', ascending=True)
dow_df['DOW_pct_change'] = dow_df['DOW_Close'].pct_change()
dow_df.tail()
# DJTA
djt_df = pd.read_csv(path_to_market_data + 'DJTA.csv')
djt_df['Date'] = pd.to_datetime(djt_df['DATE'])
djt_df = djt_df[['Date','DJTA']]
djt_df.columns = ['Date', 'DJTA']
# remove invalid data and cast to float
djt_df = djt_df[djt_df['DJTA'] != '.']
djt_df['DJTA'] = djt_df['DJTA'].astype('float64')
print(np.min(djt_df['Date'] ),np.max(djt_df['Date'] ))
djt_df = djt_df.sort_values('Date', ascending=True)
djt_df['DJTA_pct_change'] = djt_df['DJTA'].pct_change()
djt_df.tail()
# CSX
csx_df = pd.read_csv(path_to_market_data + 'CSX.csv')
csx_df['Date'] = pd.to_datetime(csx_df['Date'])
csx_df = csx_df[['Date','Adj Close']]
csx_df.columns = ['Date', 'CSX_Close']
print(np.min(csx_df['Date'] ),np.max(csx_df['Date'] ))
csx_df = csx_df.sort_values('Date', ascending=True)
csx_df['CSX_pct_change'] = csx_df['CSX_Close'].pct_change()
csx_df.tail()
Let's Plot it All¶
cut_off_date = '2019-01-01'
dow_df_tmp = dow_df.copy()
djt_df_tmp = djt_df.copy()
csx_df_tmp = csx_df.copy()
dow_df_tmp = dow_df_tmp[dow_df_tmp['Date'] >= cut_off_date]
djt_df_tmp = djt_df_tmp[djt_df_tmp['Date'] >= cut_off_date]
csx_df_tmp = csx_df_tmp[csx_df_tmp['Date'] >= cut_off_date]
# join both datasets together
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(dow_df_tmp['Date'], dow_df_tmp['DOW_Close'] , color='blue', label='DOW_Close')
plt.title('DOW_Close, DJTA, CSX_Close')
plt.legend(loc='lower right')
plt.grid()
# Get new axis
ax2 = ax.twinx()
plt.plot(djt_df_tmp['Date'], djt_df_tmp['DJTA'] , color='red', label='DJTA')
plt.legend(loc='lower left')
# Get new axis
ax3 = ax.twinx()
plt.plot(csx_df_tmp['Date'], csx_df_tmp['CSX_Close'] , color='green', label='CSX_Close')
plt.legend(loc='lower left')
plt.show()
As Percentage Change¶
# join both datasets together
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(dow_df_tmp['Date'], dow_df_tmp['DOW_pct_change'] , color='blue', label='DOW_pct_change')
plt.title('DOW_pct_change, DJTA_pct_change')
plt.plot(djt_df_tmp['Date'], djt_df_tmp['DJTA_pct_change'] , color='green', label='DJTA_pct_change')
plt.axhline(0, color='gray', linewidth=3, linestyle='-.' )
plt.legend()
plt.grid()
plt.show()
# join both datasets together
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(dow_df_tmp['Date'], dow_df_tmp['DOW_pct_change'] , color='blue', label='DOW_pct_change')
plt.title('DOW_pct_change, CSX_pct_Change')
plt.plot(csx_df_tmp['Date'], csx_df_tmp['CSX_pct_change'] , color='green', label='CSX_pct_change')
plt.axhline(0, color='gray', linewidth=3, linestyle='-.' )
plt.legend()
plt.grid()
plt.show()
Show Notes
(pardon typos and formatting -these are the notes I use to make the videos)
Dow Jones Transportation Average Diverting, Trouble Ahead? What about the rumblings in the market that transportation companies are slowing down? It's all related to the Dow theory that states that both industrials and transportation need to be moving upwards for a healthy market. Let's find out for ourselves with raw data and Python!