Scheduled events allow you to trigger code blocks for execution at specific times according to rules you set. This feature helps coordinate your algorithm activities and perform analysis at regular intervals; while letting the trading engine take care of market holidays.

The scheduling is set with two rules: the DateRules and TimeRules classes. The schedule manager takes one of each and executes an action code block.
In C# this looks like:

Schedule.On(DateRules.On(2013, 10, 7), TimeRules.At(13, 0), () =>
{
         Debug("Specific Time: Fired at : " + Time);
});

Or in Python:

self.Schedule.On(self.DateRules.On(2013, 10, 7), self.TimeRules.At(13, 0), Action(self.SpecificTime))
def SpecificTime(self):
    self.Log("SpecificTime: Fired at : {0}".format(self.Time))

The date rules can be specified as below:

Scheduled Events Date Rules
DateRules.On(2013, 10, 7) On a specific date
DateRules.EveryDay("SPY") Every trading day for SPY
DateRules.Every(DayOfWeek.Monday, ...) On specific days of week
DateRules.EveryDay() Every day
DateRules.MonthStart("SPY") SPY trading start of each month

The time rules trigger specify when on the day the event should be triggered. They can be specified as below:

Scheduled Events Time Rules
TimeRules.At(13, 0) At a specific time, 1:00pm
TimeRules.AfterMarketOpen("SPY", 10) Run 10 minutes after market open for SPY
TimeRules.BeforeMarketClose("SPY", 10) Run 10 minutes before market close for SPY
TimeRules.Every(TimeSpan.FromMinutes(10)) Every 10 minutes during day specified

For a demonstration see the tutorial video below:

Contributors

jingwu74 gaviles

Contribute

Comments


  1. Thomas Chang says:

    Hi,

    I see here many of the functions, especially the TimeRules use the parameter of “SPY”. Why?

  2. Roven Roche says:

    This doesn’t seem to work in Python. I haven’t tried C#
    I keep getting errors on the Callable Action, even when following the documentation.

    as an example :

    self.Schedule.On(self.DateRules.EveryDay(self.spy), self.TimeRules.AfterMarketOpen(self.spy,30), Action(self.EveryDayAfterMarketOpen))

    Gives me an error saying:
    Initialize(): Python.Runtime.PythonException: AttributeError : ‘XXXAlgorithm’ object has no attribute ‘EveryDayAfterMarketOpen’

    The documentation also seems to be lacking in defining all the actions allowed. The DateRules and TimeRules are clear, but the Action not so much

    1. Jing Wu Jing Wu says:

      Hi Roven, you need to define Action function yourself. Check this basic example on Github https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/ScheduledEventsAlgorithm.py

  3. Thomas Chang says:

    I see from your document there is another TimeRules as bellow:
    self.TimeRules.Every(timedelta(minutes=10))

    What is the difference to
    self.TimeRules.Every(TimeSpan.FromMinutes(10)) ?

    1. Jing Wu Jing Wu says:

      They both have the same function.

  4. Brett C. says:

    Is there a way to repeat the event scheduler every 10 minutes, but not start until, say, 10am?

    1. Jing Wu Jing Wu says:

      Hi Brett, you can change the time rules: self.TimeRules.Every(timedelta(minutes=10))

Sign In to comment in this post.