Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Rebalance quarterly, monthly

I'm attempting to run an algorithm to take 11 stocks and rebalance them. I'm looking to do a comparison of a weekly, monthly, quarterly, and annual rebalance. It looks like the only dropdown option is daily on the backtest. Is there a way to do this programatically. Here is the code I'm using from one of the sample algorithms. I'm pretty new to coding so I'm not sure where to start.

def initialize(context):  
    set_symbol_lookup_date('2005-01-01')  
    context.stocks = symbols('VHT', 'VDE', 'VDC', 'VOX', 'VAW', 'VNQ', 'VGT', 'VIS', 'VCR', 'VPU', 'VFH') 

def handle_data(context, data):  
    # This will order as many shares as needed to  
    # achieve the desired portfolio allocation.  
    # In our case, we end up with 20% allocation for  
    # one stock and 80% allocation for the other stock.  
    order_target_percent(context.stocks[0], .11)  
    order_target_percent(context.stocks[1], .11)  
    order_target_percent(context.stocks[2], .11)  
    order_target_percent(context.stocks[3], .11)  
    order_target_percent(context.stocks[4], .11)  
    order_target_percent(context.stocks[5], .11)  
    order_target_percent(context.stocks[6], .11)  
    order_target_percent(context.stocks[7], .11)  
    order_target_percent(context.stocks[8], .11)  
    order_target_percent(context.stocks[9], .11)  
    order_target_percent(context.stocks[10], .11)  
    # Plot portfolio allocations  
    pv = float(context.portfolio.portfolio_value)  
    portfolio_allocations = []  
    for stock in context.stocks:  
        pos = context.portfolio.positions[stock]  
        portfolio_allocations.append(  
            pos.last_sale_price * pos.amount / pv * 100  
        )  
5 responses

Nevermind, I found the schedule_function. Here it is if anyone else is looking: https://www.quantopian.com/help#ide-schedulefunction

Did you figure out how to do it quarterly? It looks like the highest date rule is monthly

Yeah, I got some help here https://www.quantopian.com/posts/scheduler-overide-beyond-1-month#570735ae06b3b90b58000137

Here's an example on how to rebalance the first day of each quarter:

    for sec in context.secs:  
        today = get_datetime()  
        if today.month == 1 or today.month == 4 or today.month == 7 or today.month == 10:  
            order_target_percent(sec, context.weights, limit_price=None, stop_price=None)  
            # Get the current exchange time, in the exchange timezone  
            if writelog == False:  
                exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
                log.info("Rebalanced to target portfolio weights at %s" % str(exchange_time))  
                writelog = True  

def initialize(context):

context.spy = sid(8554)  

schedule_function(open_positions, date_rules.month_start(0), time_rules.market_open(minutes=30))  

schedule_function(close_positions, date_rules.month_end(1), time_rules.market_close(minutes=30)) 

def open_positions(context, data):

today = get_datetime('US/Eastern')  

if today.month in [1, 4, 7, 10] :  
    order_target_percent(context.spy, 1)

def close_positions(context, data):

today = get_datetime('US/Eastern')  

if  today.month in [3, 6, 9, 12]:  
    order_target_percent(context.spy, 0)  

I'm using a modulo to check months

def rebalance(context, data):  
    today = get_datetime()  
    if not((today.month%3) == 0):  
        pass