Back to Community
Faber's Sector Rotation

Faber's Sector Rotation

Clone Algorithm
43
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""

from dateutil.relativedelta import relativedelta
import datetime
import pandas as pd
import numpy as np
 
def initialize(context):
    """
    Called once at the start of the algorithm.
    """   
    set_commission(commission.PerShare(cost=0, min_trade_cost=0))
    
    context.TRADING_DAYS_IN_MONTH = 20
    
    context.spy = sid(8554)
    context.spy_sma_month_count = 10
    context.spy_sma_day_count = context.TRADING_DAYS_IN_MONTH * context.spy_sma_month_count
    
    context.max_active = 3

    context.sector_etfs = symbols("XLI", "XLB", "XLE", "XLV", "XLP", "XLU", "XLF", "XLY", "XLK")
    context.sector_perf = pd.Series()
    
    context.sector_perf_month_count = 3
    context.sector_perf_day_count = context.TRADING_DAYS_IN_MONTH * context.sector_perf_month_count
    
    # Rebalance every day, 1 hour after market open.
    schedule_function(my_rebalance, date_rules.month_start(), time_rules.market_open())
     
    # Record tracking variables at the end of each day.
    schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())
         
 
def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    
    
     
def my_assign_weights(etfs):
    """
    Assign weights to securities that we want to order.
    """
    if len(etfs) == 0:
        return 0 
    else:
        weight = 1.0 / len(etfs)
        return weight
 
def my_rebalance(context, data):
    """
    Execute orders according to our schedule_function() timing. 
    """
    #log.info(str(get_datetime()))
    spy_history = data.history(context.spy, 'price', context.spy_sma_day_count + 2, '1d')
    spy_sma = spy_history[:-2].mean()
    spy_price = spy_history[-2]
               
    #log.info("%.2f, %.2f, %r" % (spy_sma, spy_price, spy_price > spy_sma))
    
    if spy_price > spy_sma:
        hist = data.history(context.sector_etfs, 'price', context.sector_perf_day_count+1, '1d')
        context.sector_perf = pd.Series(index=context.sector_etfs)

        #price_history10_weekly = hist.resample('W', how='last')
        #log.info(price_history10_weekly)
        
        for etf in context.sector_etfs:
            etfPrices = hist.loc[:,etf]
            context.sector_perf[etf] = ((etfPrices[-2] - etfPrices[0])/etfPrices[0])

        context.sector_perf.sort_values(inplace=True, ascending=False)
        context.sector_perf = context.sector_perf[0:context.max_active]  

        # sum = np.sum(context.sector_perf);

        weight = my_assign_weights(context.sector_perf)

        for etf in context.portfolio.positions:
            if etf not in context.sector_perf and data.can_trade(etf):
                order_target_percent(etf, 0)

        for etf, value in context.sector_perf.iteritems():
            if data.can_trade(etf):
              #if weight != 0:
              #    log.info("Ordering %0.0f%% percent of %s" % (weight * 100, etf.symbol))
              order_target_percent(etf, weight)
    else:
        log.info("out")
        for etf in context.portfolio.positions:
            order_target_percent(etf, 0)
        
def my_record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    record(leverage=context.account.leverage, num_positions = len(context.portfolio.positions))
 
def handle_data(context,data):
    """
    Called every minute.
    """
    pass
There was a runtime error.