Scheduling regular actions on Blueshift®¶
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
schedule_function API function, instead of checking for the
time condition in
schedule_function must be done at the
function. Also, make sure they are scheduled before at least 2
minutes from the close of the markets, else they might get
triggered next day (to avoid potential forward-looking bias). We
recommend scheduling time at least 5 minutes before day close.
from blueshift.api import schedule_function, date_rules, time_rules
def my_func(context, data):
Here we schedule a function called
my_func in the
function. The periodicity is specified by specifying the date periodicity
and the time periodicity,
Here we use the
month_start method of
be specified too. For example to run a function 5th of every month, we
date_rules.month_start with a offset of
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
market_close. Other possiblities are:
date_rules.every_day(): call every day
offsetdays after start of the month.
offsetdays before end of the month.
offsetdays after start of the week.
offsetdays before end of the week.
time_rules.every_minute(): call every minute on the scheduled date
time_rules.market_open(hours, minutes): call
minutesafter the market opens on scheduled date
time_rules.market_close(hours, minutes): call
minutesbefore the market cloes on scheduled date
On the live platform you also can use
for a call every n-th minute and
calls every n-th hour.
For more details on the scheduling APIs, see the API doc.
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
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.