Back to Community
Timing of functions

Hi,

I wish to time a rebalance function to run once an hour while trade is on, considering half days and such. How do i get the scheduling done for that?

4 responses

The straightforward approach

# Rebalance every day, every hour.  
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=0, minutes=1))  
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=1, minutes=1))  
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=2, minutes=1))  
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=3, minutes=1))  
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=4, minutes=1))  
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=5, minutes=1))  
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=6, minutes=1))

A few less lines of code

for hours_offset in range(7):  
    schedule_function(  
        my_rebalance,  
        date_rules.every_day(),  
        time_rules.market_open(hours=hours_offset, minutes=10),  
        half_days = True)

See the documentation on the schedule function at https://www.quantopian.com/help#api-schedulefunction.

Also see the attached algorithm for the two codes in action. Notice that it is run over the 2016 Thanksgiving week. Nov 25, 2016 was a half trading day. Both approaches handle half days fine. They simply don't execute the scheduled function whenever the markets are not open. You can see this by looking at the resulting logs.

Clone Algorithm
7
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 589882d773a762623b1b1e7a
There was a runtime error.

Many thanks!
My main concern was the unexpected behavior through when you'd order while the market is closed.

No problem. As you can see with the test above, The Quantopian platform takes care of limiting the code to only run during times when markets are open. You can put a value between 0 and 12 for 'hours_offset' and the platform handles it.

One subtle note... If you always want to do some special code (eg close all orders or record some variables) EVERY day before the market closes, even on half days, then you may want to use something like 'time_rules.market_close(hours=1)' method. The 'market_close' method will offset from the close of the markets whether that is a full or half day.

Hey! just checking those two codes (the multiple lines and the for loop). You only need one of those right?

Using the for loop, mine only seems to be triggered for the last hour in hours_offset, any tips?