Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Sector rotation strategy that ranks numerous factors

I'm fairly new to algo trading. Basically, I would like to create a strategy that ranks 11 sectors using ETFs on very simple, fundamental trading strategies (i.e. RSI, moving average, MACD, etc). I'm not fully understanding on how to code multiple rankings. Would the best way to accomplish this be rank out each strategy independently and then combine the ranking? The only problem I have with combining the ranks is that I wouldn't want it to buy or sell unless it met a certain criteria (i.e. the MA(200) has to be less than the MA(50)).

Can anyone point me in the right direction that would help me out or share similar code that I could use as an example? Thanks!

1 response

This may help to start:


# Simple Dynamic Sectors Allocation  
# ------------------------------------------------------------------------------------  
sectors = symbols('XLV', 'XLK', 'XLI', 'XLP', 'XLE', 'XLU', 'XLB', 'XLY', 'XLF', 'IYR')  
bonds, ma_f, ma_s, lev, h = symbols('TLT', 'IEF'), 10, 100, 1.0, 0.1  
# ------------------------------------------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.month_start(), time_rules.market_open(minutes = 65))

def trade(context, data):  
    if get_open_orders(): return  
    bars = max(ma_f, ma_s) 

    P = data.history(sectors, 'price', bars, '1d')  
    mavg_f = P.tail(ma_f).mean()  
    mavg_s = P.tail(ma_s).mean()  
    longs = mavg_f[mavg_f > mavg_s]

    wt_bonds = lev

    for sec in sectors:  
        if data.can_trade(sec):  
            if sec in longs.index:  
                wt_sec = lev*(1.0 - h)/len(sectors)  
                order_target_percent(sec, wt_sec)  
                wt_bonds -= wt_sec  
            else:  
                order_target(sec, 0)  

    for bond in bonds:  
        if data.can_trade(bond):  
            wt_bnd = wt_bonds / len(bonds)  
            order_target_percent(bond, wt_bnd)

    record(leverage = context.account.leverage)