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!

Highest & Lowest Ever 30-Year Interest Rates in our Lifetime: Hands-On Market Analysis with Python

Introduction

If you are alive today, you have just experienced the lowest 30-year interest rates ever. If you were born before 1981, you also experienced the highest 30-year interest rates ever. Let's explore this weird phenomenon with Python and market data.



If you liked it, please share it:

Code

ViralML-Hands-On-30-Year-Highest-and-Lowest-Ever
In [32]:
from IPython.display import Image
Image(filename='viralml-book.png')
Out[32]:

Fundamental and Technical Indicators - Hands-On Market Analysis

Get the book "The Little Book of Fundamental Market Indicators":

https://amzn.to/2DERG3d

More at:

https://www.viralml.com/

Data from the Federal Reserve Bank of St. Louis

Treasury 30-Year Treasury Constant Maturity Rate (DGS30)

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

3-Month Treasury Constant Maturity Rate (DGS3MO)

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

In [42]:
%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-08-30/'

Load 30-Year Treasury Constant Maturity Rate (DGS30)

In a recession, short-term interest rates can rise above long-term ones, which is the opposite of the more conventional pattern of longer-term interest rates being higher than shorter-term interest rates in normal economic times.

In [34]:
# Get the rates 30 year
# Treasury Yield Curve Rates
rates_30_df = pd.read_csv(path_to_market_data + 'DGS30.csv')
rates_30_df.columns = ['Date', '30yearT']
rates_30_df['Date'] = pd.to_datetime(rates_30_df['Date']) 
print(np.min(rates_30_df['Date'] ),np.max(rates_30_df['Date'] ))
rates_30_df = rates_30_df.sort_values('Date', ascending=True) 
rates_30_df.tail()
1977-02-15 00:00:00 2019-08-29 00:00:00
Out[34]:
Date 30yearT
11093 2019-08-23 2.02
11094 2019-08-26 2.04
11095 2019-08-27 1.97
11096 2019-08-28 1.94
11097 2019-08-29 1.97
In [35]:
rates_30_df.describe()
Out[35]:
Date 30yearT
count 11098 11098
unique 11098 1223
top 1989-12-27 00:00:00 .
freq 1 1461
first 1977-02-15 00:00:00 NaN
last 2019-08-29 00:00:00 NaN
In [36]:
# remove NaN and text characters
rates_30_df = rates_30_df[rates_30_df['30yearT'] != '.']
rates_30_df['30yearT'] = rates_30_df['30yearT'].astype(float)
rates_30_df.head()
Out[36]:
Date 30yearT
0 1977-02-15 7.70
1 1977-02-16 7.67
2 1977-02-17 7.67
3 1977-02-18 7.76
5 1977-02-22 7.77
In [43]:
cut_off_date = '1977-01-01'
rates_30_df_tmp = rates_30_df.copy()

fig, ax = plt.subplots(figsize=(16, 10))
plt.plot(rates_30_df_tmp['Date'], rates_30_df_tmp['30yearT'], color='black', label='Rates')

plt.grid()
plt.title("30 Year Rates")
plt.legend(loc='upper left')
plt.axhline(2)
Out[43]:
<matplotlib.lines.Line2D at 0x1218d4470>
In [38]:
# Get the 3 months rates
# Treasury Yield Curve Rates
rates_3MO_df = pd.read_csv(path_to_market_data + 'DGS3MO.csv')
rates_3MO_df.columns = ['Date', '3MOT']
rates_3MO_df['Date'] = pd.to_datetime(rates_3MO_df['Date']) 
print(np.min(rates_3MO_df['Date'] ),np.max(rates_3MO_df['Date'] ))
rates_3MO_df = rates_3MO_df.sort_values('Date', ascending=True) 

rates_3MO_df = rates_3MO_df[rates_3MO_df['3MOT'] != '.']
rates_3MO_df['3MOT'] = rates_3MO_df['3MOT'].astype(float)

rates_3MO_df.tail()
1982-01-04 00:00:00 2019-08-29 00:00:00
Out[38]:
Date 3MOT
9819 2019-08-23 1.97
9820 2019-08-26 2.01
9821 2019-08-27 1.98
9822 2019-08-28 1.99
9823 2019-08-29 1.99
In [41]:
cut_off_date = '2008-01-01 00:00:00'
rates_30_df_tmp = rates_30_df.copy()
rates_30_df_tmp = rates_30_df_tmp[rates_30_df_tmp['Date'] >= cut_off_date]
rates_3MO_df_tmp = rates_3MO_df.copy()
rates_3MO_df_tmp = rates_3MO_df_tmp[rates_3MO_df_tmp['Date'] >= cut_off_date]

fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(rates_30_df_tmp['Date'], rates_30_df_tmp['30yearT'], color='black', label='30 Year Rates')
plt.plot(rates_3MO_df_tmp['Date'], rates_3MO_df_tmp['3MOT'], color='blue', label='3MO Rates')

plt.grid()
plt.title("30 Year Rates")
plt.legend(loc='upper left')
plt.axhline(2)
Out[41]:
<matplotlib.lines.Line2D at 0x11e1d32b0>

Show Notes

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

If you are alive today, you have just experienced the lowest 30-year interest rates ever. If you were born before 1981, you also experienced the highest 30-year interest rates ever. Let's explore this weird phenomenon with Python and market data.