Skip to content

Scheduling regular actions on Blueshift®

Scheduling functions

On Blueshift®, we can schedule a particular function to be called at a particular time interval. This is useful if our strategy (or some part of it) is executed at pre-determined time interval. A typical example will be weekly or monthly portfolio rebalance or a daily end-of-day square-off for an intraday strategy. For such cases, we can use the schedule_function API function, instead of checking for the time condition in handle_data ourselves.

Scheduling function

Call to schedule_function must be done at the initialize function.

Examples below:

from zipline.api import schedule_function, date_rules, time_rules

def initialize(context):
    schedule_function(my_func, date_rules.month_start(), 
                        time_rules.market_open(hours=1, minutes=5)))

def my_func(context, data):

Here we schedule a function called my_func in the initialize function. The periodicity is specified by specifying the date periodicity and the time periodicity, date_rules and time_rules respectively. Here we use the month_start method of date_rule. The offset can be specified too. For example to run a function 5th of every month, we can specify date_rules.month_start with a offset of 5. Similarly, the time rules (via time_rules) specifies the the function to be called after 1 hour and 5 minutes after the market_open. Note hours and minutes denote after for market_open and before for market_close. Other possiblities are:

Date rules

  • date_rules.every_day(): call every day
  • date_rules.month_start(offset): Call offset days after start of the month.
  • date_rules.month_end(offset): Call offset days before end of the month.
  • date_rules.week_start(offset): Call offset days before start of the week.
  • date_rules.week_end(offset): Call offset days before end of the week.

Time rules

  • time_rules.every_minute(): call every minute on the scheduled date
  • time_rules.market_open(hours, minutes): call hours and minutes after the market opens on scheduled date
  • time_rules.market_close(hours, minutes): call hours and minutes before the market cloes on scheduled date

On the live platform you also can use time_rules.every_nth_minute(n) for a call every n-th minute and time_rules.every_nth_hour(n) for calls every n-th hour.

Scheduling in live runs

Call time on live trading

In live trading you should not make any assumption on the call timing of any functions - either scheduled or the main entry-point functions. However, the call orders will be maintained.

The concept of time is different between backtest and a live run. In a backtest, we control the time, as we feed the time-stamps to the simulation engine. On a live run, we have no control. A function scheduled to be called at exactly 18:00 hours can be called later, if other routines part of the algorithm were busy executing as it waits in the line. This is particularly important if you are using the schedule_function API function to carry out end-of-day squaring off of positions in an intraday strategy. Make sure the timing of the scheduling is comfortably before the market close, so that it has a chance to run while the market is still trading.