Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Is there any way to re-balance quarterly?

Using the schedule function, is the least frequent you can rebalance is monthly? E.g., ...

schedule_function(my_rebalance, date_rules.month_end(), time_rules.market_open())

Is there no way - using schedule_function or otherwise - to rebalance on a quarterly basis?

2 responses

Sloan,

Try this:

# Rebalance quarterly 1  

def initialize(context):  
    context.counter = 0  
    schedule_function(rebalance, date_rules.month_end(),time_rules.market_close(minutes = 120))

def rebalance(context, data):  
    freq_month = 3  
    context.counter += 1  
    if context.counter == freq_month:  
        trade(context, data)  
        context.counter = 0 

def trade(context, data):  
    for asset in context.assets:  
        if get_open_orders(asset): return  
        order_target_percent(asset, context.weights[asset])  

Or this:

# Rebalance quarterly 2

def initialize(context):  
    schedule_function(rebalance, date_rules.month_end(),time_rules.market_close(minutes = 120))

def rebalance(context, data):  
    this_month = get_datetime('US/Eastern').month  
    if this_month not in [3, 6, 9, 12]:  
        log.info("skipping this month")  
        return  
    else:  
        log.info("trading this month")  
        trade(context, data)  

Thank you, Vladimir.

For second option, I keep getting this error:

NameError: global name 'trading' is not defined
... USER ALGORITHM:46, in my_rebalance
trading(context, data)