Notebook

Risk Free Rate: FRED and Quandl

Karl 8 December 2017

In [1]:
# import quandl # Not supported in Quantopian Research
import numpy as np
import pandas as pd
from datetime import date, datetime, timedelta
import matplotlib.pyplot as plt

# fred = quandl.get("FRED/DGS3MO", start_date="2012-08-01") # Need API Key if quandl.get() is >50/day

# fred = quandl.get_table("FRED/DGS3MO", start_date="2012-08-01") # Not supported in Quantopian Research
In [2]:
# FRED: https://fred.stlouisfed.org/series/DGS3MO

# Quandl download FRED-DGS3MO.csv: www.quandl.com/data/FRED/DGS3MO-3-Month-Treasury-Constant-Maturity-Rate
# Then upload FRED-DGS3MO.csv to Research for use as local_csv()

fredData = local_csv('FRED-DGS3MO.csv', date_column='Date')
fredData['Rf_21'] = fredData[::-1].rolling(window=21).mean() # Approx Rf
fredData.head()
Out[2]:
Value Rf_21
Date
2017-12-07 00:00:00+00:00 1.29 1.274762
2017-12-06 00:00:00+00:00 1.30 1.271429
2017-12-05 00:00:00+00:00 1.30 1.266190
2017-12-04 00:00:00+00:00 1.29 1.260476
2017-12-01 00:00:00+00:00 1.27 1.254762
In [3]:
fig = plt.figure(figsize=(16,9))
ax1 = fig.add_subplot(1, 1, 1)

ax1.plot(fredData['Value'], color='blue', alpha=0.6, linewidth=0.6);
ax1.plot(fredData['Rf_21'], color='black', linewidth=1.2);

# fredData.plot(figsize=(16,9), linewidth=0.8);

Treasury Bond: ETF

In [4]:
# ETF Prices
etfBond = symbols(['TLH', 'TLT', 'SHY', 'VGLT', 'TLO', 'ITE', 'GOVT', 'SIPE'])
etfPrices = get_pricing(etfBond, start_date='2015-01-01', end_date='2017-12-07', fields="close_price")
etfPrices.tail()
Out[4]:
Equity(33147 [TLH]) Equity(23921 [TLT]) Equity(23911 [SHY]) Equity(38988 [VGLT]) Equity(33946 [SPTL]) Equity(33947 [ITE]) Equity(42524 [GOVT]) Equity(46446 [SIPE])
2017-12-01 00:00:00+00:00 136.40 126.56 83.990 77.88 36.46 59.640 25.15 NaN
2017-12-04 00:00:00+00:00 136.31 126.65 83.930 77.85 36.46 59.580 25.13 NaN
2017-12-05 00:00:00+00:00 136.67 127.24 83.930 78.26 36.65 59.581 25.14 NaN
2017-12-06 00:00:00+00:00 136.98 127.69 83.955 78.49 36.81 59.620 25.19 NaN
2017-12-07 00:00:00+00:00 136.49 126.73 83.960 77.99 36.50 59.580 25.14 NaN
In [5]:
etfPrices.plot(figsize=(16,9), linewidth=1.2);