Created
July 12, 2021 03:03
-
-
Save geekypy/3918b4a362898c4b7648b40773668d57 to your computer and use it in GitHub Desktop.
Generates 1yr treasury note yield using FRED data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python3 | |
import sys ,os , requests | |
import time, json | |
import matplotlib.pyplot as plt | |
import Money_Supply_FRED as msf | |
import numpy as np | |
# This library retrieves and relies on data coming from FRED API | |
# Attributes | |
# List containing the three datasets to plot/analyze | |
#data_s_s =["DGS1","MANMM101USA657S","MABMM301USA657S"] | |
data_s_s =["DGS1","M1","M2"] | |
#DGS1 represents the 1-yr-Treasury Note | |
#M1 is the M1 money stock while M2 is M2 money stock | |
delta = 0 | |
def growth_rate(id,delta): | |
#Put all data together in lists and dictionaries | |
ob,ds,vs = msf.obtain_observations(id) | |
# Cleaning up the "." | |
#vs = [item.replace('.', '0') for item in vs] | |
pointer = 0 | |
#Initialize end lists | |
final_values = list() | |
final_date = list() | |
while pointer <= len(ds): | |
if (pointer+delta) <= len(ds): | |
v_0 = float(vs[pointer]) | |
v_1 = float(vs[pointer+delta]) | |
if v_0 != 0: | |
g = (v_1-v_0)/(v_0) | |
else: | |
g = v_1 | |
pointer = pointer + delta | |
final_values.append(g) | |
final_date.append(ds[pointer]) | |
print(g) | |
else: | |
end = len(ds)-1 | |
v_0 = float(vs[pointer]) | |
v_1 = float(vs[end]) | |
if v_0 != 0: | |
g = (v_1-v_0)/(v_0) | |
else: | |
g = v_1 | |
g = (v_1 - v_0) / (v_0) | |
final_values.append(g) | |
final_date.append(ds[end]) | |
break | |
return final_date, final_values | |
def constant_maturity(id, range,delta): | |
ob, ds, vs = msf.obtain_observations(id) | |
max = len(ds)-1 | |
samples_total = delta*int(range) | |
min = max - samples_total | |
ds = ds[min:max] | |
vs= vs[min:max] | |
pointer = 0 | |
vx=list() | |
for index in vs: | |
if index == ".": | |
# FRED Data introduces noise to the sample. | |
# We eliminate here those entries that are represented with a dot "." | |
vx.append(0.0) | |
else: | |
vx.append(float(index)) | |
pointer = pointer + 1 | |
return ds,vx | |
def growth_rate_with_cleanup(id,range,delta): | |
#Calculate growth rate by cleaning up data previously | |
ds,vs = constant_maturity(id,range,delta) | |
print(ds) | |
time.sleep(3) | |
pointer = 0 | |
#Initialize end lists | |
final_values = list() | |
final_date = list() | |
while pointer <= len(ds): | |
if (pointer+delta) < len(ds): | |
v_0 = float(vs[pointer]) | |
v_1 = float(vs[pointer+delta]) | |
if v_0 != 0: | |
g = ((v_1-v_0)/(v_0))*100 | |
else: | |
g = v_1*100 | |
pointer = pointer + delta | |
final_values.append(g) | |
final_date.append(ds[pointer]) | |
print(g) | |
else: | |
end = len(ds)-1 | |
v_0 = float(vs[pointer]) | |
v_1 = float(vs[end]) | |
if v_0 != 0: | |
g = ((v_1-v_0)/(v_0))*100 | |
else: | |
g = v_1*100 | |
g = ((v_1 - v_0) / (v_0))*100 | |
final_values.append(g) | |
final_date.append(ds[end]) | |
break | |
return final_date, final_values | |
def main(): | |
#fs,fv=growth_rate(data_s_s[0]) | |
global delta | |
delta = 200 | |
fs,fv=constant_maturity(data_s_s[0],4,delta) | |
# IMPORTANT | |
# Please commented out these line to depict the 1yr treasury date | |
#fs1, fv1 = constant_maturity(data_s_s[1], 3,delta) | |
#fs2, fv2 = constant_maturity(data_s_s[2], 3,delta) | |
fig, ax = plt.subplots() | |
ax.set_xticklabels(fs, rotation=60, fontsize=7) | |
plt.xlabel("Date Range",labelpad=delta) | |
plt.ylabel("% Growth") | |
plt.title("1yr Treasury Note") | |
msf.plot_single(fs, fv, "date range (2018/07/06 --> 2021/06/03)", "%", "1yr treasury note") | |
#fs0,fv0=constant_maturity(data_s_s[1],2,delta) | |
#fs1,fv1=constant_maturity(data_s_s[2],2,delta) | |
#fs0,fv0 = growth_rate_with_cleanup(data_s_s[1],100,delta) | |
#fs1,fv1 = growth_rate_with_cleanup(data_s_s[2],100,delta) | |
#fig, ax = plt.subplots() | |
#ax.set_xticklabels(fs0, rotation=60, fontsize=6) | |
#plt.plot(fs0,fv0,fs1,fv1) | |
#plt.xlabel("Date Range",labelpad=200) | |
#plt.ylabel("% Growth") | |
#plt.title("M1 vs. M2 Growth Rate") | |
plt.show() | |
#msf.plot_multiple(fs0, fv0, fs1, fv1, "M1 vs. M2 Growth Rate (Previous Period, Not Seasonally Adjusted)", "Date Range","% Growth Rate") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please note that this code can be used to generate the 1yr treasure note yield using FRED api data as well as M1 vs M2 supply growth over time