Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
SCHEDULE FUNCTIONS WITH MORE THAN A MONTH

Hi,

For one of my algorithms, I would really need to schedule one function with an interval bigger than a month. I've checked the API Reference and the schedule function only has got three possible date rules: daily, weekly and monthly. Any ideas or work around to execute the function with a bigger timespan, le's say 3 months or half-year?

Thank you all!

Axel

5 responses

Schedule the function to run monthly. Inside that function check if it's a month you want to run, otherwise, return. Something like this.

def my_rebalance(context, data):  
    """  
    Rebalance quarterly (ie months 3, 6, 9, and 12)  
    """  
    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("executing this month")

    # put the code you want executed quarterly below

Thank you so much Dan! You're the boss!!!

Hi Dan,

I am trying to have my algo rebalance every monday and friday within the same week. However, when I use the below code I can see from my logs that this does not happen.

For example if I begin on 6/27/2016 (monday) i would expect my next rebalance to be Friday 7/1/2016 but my logs show 6/27/2016 & 7/05/2016.

MINUTES_AFTER_OPEN_TO_TRADE = 300 #2:30pm Eastern
MINUTES_AFTER_OPEN_TO_TRADE_2 = 400 #3:30pm Eastern

# Schedule a function, 'do_portfolio_construction'.  
algo.schedule_function(  
    do_portfolio_construction,  
    date_rule=algo.date_rules.week_start(days_offset= 0),  
    time_rule=algo.time_rules.market_open(minutes=MINUTES_AFTER_OPEN_TO_TRADE),  
    half_days=True,  
)  

algo.schedule_function(  
    do_portfolio_construction,  
    date_rule=algo.date_rules.week_end(days_offset= 0),  
    time_rule=algo.time_rules.market_open(minutes=MINUTES_AFTER_OPEN_TO_TRADE_2),  
    half_days=True,  
)  

Hi Daniel,

Your second scheduled function need to say: days_offset=4

Currently both are scheduled for Monday.

0=Monday
4=Friday

Thank you Dan