"""
Rotate between S&P 500, mid-cap value, small cap international, emerging markets, and intermediate treasuries
Use momentum (short term moving average ratio to long term)
$10K initial, with IB fees
"""
bt = get_backtest('58a10bac98a6065defa8d7b0')
bt.create_full_tear_sheet()
import time
from datetime import datetime, timedelta
date = time.strftime("%Y-%m-%d")
#date = '2010-01-05'
import pandas as pd
import math as math
import numpy as np
def asset_momentum(assets, short_t, long_t):
n = len(assets)
i = -1
best = 0
for x in range(n):
prices = get_pricing(symbols(assets[x]), start_date=(datetime.strptime(date, '%Y-%m-%d') - timedelta(days=300)), end_date=date, frequency = 'daily')
st = prices['close_price'][-(short_t+1):-1].mean()
lt = prices['close_price'][-(long_t+1):-1].mean()
ratio = st/lt
print assets[x] + '\n {:03.0f} Day: '.format(short_t) + '{:01.2f}'.format(st) + '\n {:03.0f} Day: '.format(long_t) + '{:01.2f}'.format(lt) + '\n Ratio: {:01.3f} '.format(ratio)
if ratio > best:
i = x
best = ratio
print '\nBest: ' + assets[i] + '\n'
tsp = ['IVV', #C Fund Equivalent - IVV (ISHARES CORE S&P 500 ETF)
'IJR', #S Fund Equivalent - IJR (ISHARES CORE S&P SMALL-CAP ETF)
'EFA', #I Fund Equivalent - EFA (ISHARES MSCI EAFE ETF)
'AGG', #F Fund Equivalent - AGG (ISHARES CORE U.S. AGGREGATE BOND)
'SHY'] #G Fund Equivalent - SHY (ISHARES 1-3 YEAR TREASURY BOND)
asset_momentum(tsp,20,180)
best = [ 'IEF', #IEF (ISHARES 7-10 YEAR TREASURY BOND)
'IVV', #IVV (ISHARES CORE S&P 500)
'IJJ', #IJJ (ISHARES S&P MID-CAP 400 VALUE)
'SCZ', #SCZ (ISHARES MSCI EAFE SMALL-CAP)
'EEM'] #EEM (ISHARES MSCI EMERGING MARKETS)
asset_momentum(best,20,120)