Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Rebalancing less frequently than 1 month (HELP)

Hi,

how can I do my rebalancing less frequently than 1 month (e.g. yearly)? For now I've tried to do it like this in before_trading_start:

def before_trading_start(context, data):  
    now = get_datetime('US/Eastern')  
    if now.month==rebalancing_month:  
        pass  
    else:  
        return  

Thanks for help.

5 responses

maybe it's not an elegant solution, but i usually do it with a simple counter:, for example to trade every 3 months:

def initialize(context):  
    context.counter = 0  
    schedule_function(  
    trade,  
    date_rules.month_end(),  
    time_rules.market_open(minutes=15))  
def trade(context, data):  
    context.counter += 1  
    if context.counter == 3:  
        trade()  
    cotext.counter = 0  

Could you send some specific working example?

shure, as you can see in the logs the trade function runs every 3 months

Sanning

Try this example.

Add the following to 'my_rebalance' function:

def my_rebalance(context,data):
# Schedule our rebalance function to run at the start of each quarter.
today = get_datetime('US/Eastern')
if today.month in [1,4,7,10]: #rebalance quarterly
your_rebalance_method

You can customize the frequency in 'today.month in [frequency]'.