Find ETF Bond funds that move counter to SPY
In [359]:
import pandas as pd
import matplotlib.pyplot as pyplot
import seaborn as sns
In [360]:
df_ETFs = local_csv('ETFs.csv')
df_ETFs = df_ETFs.set_index('Symbol')
df_ETFs[:2]
Out[360]:
In [361]:
df_ETFs.Category.unique()
Out[361]:
In [362]:
df_ETFs.Sector.unique()
Out[362]:
In [363]:
df_ETFs['Asset Class'].unique()
Out[363]:
In [364]:
bonds = df_ETFs[df_ETFs['Asset Class'] == 'Bond']
bonds[:2]
Out[364]:
In [365]:
# find ETFs' with Bond in their Category
bond_cat = list(df_ETFs[df_ETFs.Category.str.contains('Bond').fillna(False)].index)
bond_cat[:2]
Out[365]:
In [366]:
# ETFs with Bond in title that are not a Bond in Asset Class
set(bond_cat) - set(bonds.index)
Out[366]:
In [367]:
# apparently these are all equity
df_ETFs.ix['PGX']
Out[367]:
In [368]:
bonds = bonds.drop(bonds[bonds.Leveraged.notnull()].index) # 5 are leveraged
bonds = bonds.drop(bonds[bonds.Inverse.notnull()].index)
len(bonds)
Out[368]:
In [369]:
bond_prices = pd.DataFrame(index=get_pricing('AGG', fields='close_price', start_date=start, end_date=end, frequency='daily').index)
non_bonds = []
for b in bonds.index:
try:
bond_prices[b] = get_pricing(b, fields='close_price', start_date=start, end_date=end, frequency='daily')
except:
non_bonds += [b]
non_bonds
Out[369]:
In [370]:
bonds = bonds.drop(non_bonds)
len(bonds)
Out[370]:
In [371]:
start = pd.Timestamp('2007-03-02 00:00:00+0000', tz='UTC')
end = pd.Timestamp('2015-02-06 23:59:00+0000', tz='UTC')
In [398]:
# add spy to the price df
spy = get_pricing('SPY', fields='close_price', start_date=start, end_date=end, frequency='daily')
bond_prices['SPY'] = spy
In [442]:
# plot the Bond ETF's that have data for the 2008 - 2009 stock market drop
bond_prices.ix['2008-09-01':'2009-06-01'].dropna(axis=1,how='any').plot(colormap='rainbow')
pyplot.title("Bond Prices")
#pyplot.legend(loc='best')
pyplot.ylabel("Bond Price")
pyplot.xlabel("Time")
Out[442]:
In [406]:
# plot the Bond ETF's that have a price > 62 , those below don't seem to move
bond_prices.ix['2008-09-01':'2009-06-01'][(bond_prices.ix['2008-09-01':'2009-06-01'].dropna(axis=1,how='any') > 62)].plot(colormap='rainbow')
pyplot.title("Bond Prices")
pyplot.ylabel("Bond Price")
pyplot.xlabel("Time")
Out[406]:
In [441]:
# plot the Bond ETF's that have a price > 62 and max > 100, they broke to the upside
bonds2 = bond_prices.ix['2008-09-01':'2009-06-01'].dropna(axis=1,how='any')
bonds2[bonds2.columns[bonds2.max() > 100]].plot(colormap='rainbow')
Out[441]:
In [ ]: