🚀 AI One-Click Generation of quantconnect Strategy Code

How to Use `time_rules.after_market_open` to Schedule Events in QuantConnect

quantconnect | Published on: 2025/12/9
The following content is generated by EasyQuant .

Problem Description

How can I run a function exactly 5 minutes after the stock market opens each day?

Solution

To run a function exactly 5 minutes after the stock market opens, you should use the self.schedule.on method within the initialize function.

You will need to combine self.date_rules.every_day(symbol) with self.time_rules.after_market_open(symbol, 5).

Here is the complete, runnable algorithm demonstrating this implementation:

# region imports
from AlgorithmImports import *
# endregion

class ScheduledMarketOpenAlgorithm(QCAlgorithm):

    def initialize(self):
        # 1. Set basic algorithm parameters
        self.set_start_date(2023, 1, 1)
        self.set_end_date(2023, 12, 31)
        self.set_cash(100000)

        # 2. Add an asset to establish the market hours (e.g., SPY for US Equity)
        self.spy = self.add_equity("SPY", Resolution.MINUTE).symbol

        # 3. Create the Scheduled Event
        # This tells Lean to run 'self.my_daily_function' every trading day,
        # 5 minutes after SPY's market open (usually 9:35 AM ET).
        self.schedule.on(
            self.date_rules.every_day(self.spy),
            self.time_rules.after_market_open(self.spy, 5),
            self.my_daily_function
        )

    def my_daily_function(self):
        """
        This function runs exactly 5 minutes after market open.
        """
        self.log(f"5 Minutes after open! Current Time: {self.time}")
        
        # Example logic: Check if price is above yesterday's close
        if self.securities[self.spy].price > 0:
            self.plot("Trade Plot", "Price", self.securities[self.spy].price)

    def on_data(self, data: Slice):
        # Standard data event handler (required for the algorithm to run properly)
        pass

Key Components Explained

  1. self.add_equity("SPY"): You must add a security to the algorithm. The schedule requires a specific symbol to determine which exchange's hours to use (e.g., NYSE/Nasdaq hours).
  2. self.date_rules.every_day(self.spy): This rule ensures the event triggers on every day that SPY is trading. It automatically skips weekends and market holidays.
  3. self.time_rules.after_market_open(self.spy, 5): This calculates the time 5 minutes after the regular market open. For US Equities, the open is 9:30 AM ET, so this triggers at 9:35 AM ET.
  4. self.my_daily_function: Pass the method name without parentheses. This is the callback function that executes when the time rule is met.

Q&A

Q: Does after_market_open account for extended market hours?
A: No. after_market_open is calculated based on the regular trading hours (typically 9:30 AM ET for US equities), even if you have enabled extended market hours for the security.

Q: What happens on half-days (e.g., day before Christmas)?
A: The after_market_open rule still works correctly. Since the market still opens at 9:30 AM ET on half-days, the function will still run at 9:35 AM ET.

Q: Can I use a negative number for the minutes?
A: Yes. If you use self.time_rules.after_market_open(self.spy, -5), the function will run 5 minutes before the market opens (9:25 AM ET).

Q: Will this run on weekends?
A: No. By passing the symbol object (self.spy) into self.date_rules.every_day(self.spy), the engine checks the exchange calendar for that specific asset and only schedules the event on valid trading days.