Notebook

Risk Free Rate: FRED and Quandl

Karl 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 [ ]:
# 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()
In [ ]:
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 [2]:
# ETF Prices
etfBond = symbols(['TLH', 'TLT', 'SHY', 'VGLT', 'TLO', 'ITE', 'GOVT', 'SIPE'])
etfPrices = get_pricing(etfBond, start_date='2015-01-01', end_date='2017-01-01', fields="close_price")
etfPrices.head()
Out[2]:
Equity(33147 [TLH]) Equity(23921 [TLT]) Equity(23911 [SHY]) Equity(38988 [VGLT]) Equity(33946 [SPTL]) Equity(33947 [ITE]) Equity(42524 [GOVT]) Equity(46446 [SIPE])
2015-01-02 00:00:00+00:00 131.515 121.980 83.530 74.167 70.161 58.653 24.690 NaN
2015-01-05 00:00:00+00:00 132.683 123.906 83.533 75.340 71.278 58.780 24.729 NaN
2015-01-06 00:00:00+00:00 133.870 126.148 83.582 76.529 72.467 58.887 24.846 NaN
2015-01-07 00:00:00+00:00 133.693 125.888 83.602 76.317 72.333 58.849 24.846 NaN
2015-01-08 00:00:00+00:00 132.799 124.241 83.612 75.510 71.501 58.836 24.788 NaN
In [3]:
etfPrices.plot(figsize=(16,9), linewidth=1.2);