Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Re-balancing portfolio every year - please see code

I'm trying to set my re-balance function so it will be triggered once every year and am getting errors.
I've read other posts in the forum about this and haven't managed to get it to work.

Any help could be much appreciated, see code:

def initialize(context):  
    algo.attach_pipeline(risk_loading_pipeline(), 'risk_factors')

    # Schedule our rebalance function  
    algo.schedule_function(func=rebalance,  
                           date_rule=stock_selection_by_calendar(),  
                           time_rule=algo.time_rules.market_open(hours=0, minutes=30),  
                           half_days=True)

    # Record our portfolio variables at the end of day  
    algo.schedule_function(func=record_vars,  
                           date_rule=algo.date_rules.every_day(),  
                           time_rule=algo.time_rules.market_close(),  
                           half_days=True)  
def stock_selection_by_calendar(context,data):  
    today = get_datetime()  
    return today.month == 1  and today.day == 1  
2 responses

Hope this will help

# ----------------------------------------------------  
assets, wt, lev = symbols('QQQ', 'TLT'), [.5, .5], 1.0  
# ----------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.month_start(), time_rules.market_open(minutes = 65))

def trade(context, data):  
    if get_datetime().month not in [1] or get_open_orders(): return  
    for i, asset in enumerate (assets):  
        if data.can_trade(asset):  
            order_target_percent(asset, lev*wt[i])  

Thanks, works perfect!