Skip to content

TimeRules

QuantConnect.Scheduling.TimeRules

TimeRules(
    algorithm: IAlgorithm,
    securities: SecurityManager,
    time_zone: Any,
    market_hours_database: MarketHoursDatabase,
)

Bases: BaseScheduleRules

Helper class used to provide better syntax when defining time rules

Initializes a new instance of the TimeRules helper class

Parameters:

Name Type Description Default
algorithm IAlgorithm

The algorithm instance

required
securities SecurityManager

The security manager

required
time_zone Any

The algorithm's default time zone

required
market_hours_database MarketHoursDatabase

The market hours database instance to use

required

now

now: ITimeRule

Specifies an event should fire at the current time

midnight

midnight: ITimeRule

Convenience property for running a scheduled event at midnight in the algorithm time zone

noon

noon: ITimeRule

Convenience property for running a scheduled event at noon in the algorithm time zone

time_zone

time_zone: Any

The algorithm's default time zone

This Property is protected.

securities

securities: SecurityManager

The security manager

This Property is protected.

market_hours_database

market_hours_database: MarketHoursDatabase

The market hours database instance to use

This Property is protected.

after_market_close

after_market_close(
    symbol: Union[Symbol, str, BaseContract, Security],
    minutes_after_close: float = 0,
    extended_market_close: bool = False,
) -> ITimeRule
after_market_close(
    minutes_after_close: float = 0,
    extended_market_close: bool = False,
) -> ITimeRule
after_market_close(
    symbol: str,
    minutes_after_close: float = 0,
    extended_market_close: bool = False,
) -> ITimeRule

Signature descriptions:

  • Specifies an event should fire at the market close +- minutes_after_close

  • Specifies an event should fire at the market close +- minutes_after_close. Picks, per date, the latest market close across the algorithm's securities, ignoring always-open exchanges. Defaults to US equities (SPY) when no eligible security is subscribed.

Parameters:

Name Type Description Default
symbol Optional[Union[Symbol, str, BaseContract, Security] | str]

The symbol whose market close we want an event for

None
minutes_after_close float

The time after market close that the event should fire

0
extended_market_close bool

True to use extended market close, false to use regular market close

False

Returns:

Type Description
ITimeRule

Depends on the signature used. Case 1: [A time rule that fires the specified number of minutes after the symbol's market close.]; Case 2: [A time rule that fires the specified number of minutes after the latest market close.]

after_market_open

after_market_open(
    symbol: Union[Symbol, str, BaseContract, Security],
    minutes_after_open: float = 0,
    extended_market_open: bool = False,
) -> ITimeRule
after_market_open(
    minutes_after_open: float = 0,
    extended_market_open: bool = False,
) -> ITimeRule
after_market_open(
    symbol: str,
    minutes_after_open: float = 0,
    extended_market_open: bool = False,
) -> ITimeRule

Signature descriptions:

  • Specifies an event should fire at market open +- minutes_after_open

  • Specifies an event should fire at market open +- minutes_after_open. Picks, per date, the earliest market open across the algorithm's securities, ignoring always-open exchanges. Defaults to US equities (SPY) when no eligible security is subscribed.

Parameters:

Name Type Description Default
symbol Optional[Union[Symbol, str, BaseContract, Security] | str]

The symbol whose market open we want an event for

None
minutes_after_open float

The minutes after market open that the event should fire

0
extended_market_open bool

True to use extended market open, false to use regular market open

False

Returns:

Type Description
ITimeRule

Depends on the signature used. Case 1: [A time rule that fires the specified number of minutes after the symbol's market open.]; Case 2: [A time rule that fires the specified number of minutes after the earliest market open.]

at

at(time_of_day: timedelta) -> ITimeRule
at(hour: int, minute: int, second: int = 0) -> ITimeRule
at(hour: int, minute: int, time_zone: Any) -> ITimeRule
at(
    hour: int, minute: int, second: int, time_zone: Any
) -> ITimeRule
at(time_of_day: timedelta, time_zone: Any) -> ITimeRule

Signature descriptions:

  • Specifies an event should fire at the specified time of day in the algorithm's time zone

  • Specifies an event should fire at the specified time of day in the specified time zone

Parameters:

Name Type Description Default
time_of_day Optional[timedelta]

The time of day in the algorithm's time zone the event should fire

None
hour Optional[int]

The hour

None
minute Optional[int]

The minute

None
second Optional[int]

The second

0
time_zone Optional[Any]

The time zone the event time is represented in

None

Returns:

Type Description
ITimeRule

A time rule that fires at the specified time in the algorithm's time zone.

before_market_close

before_market_close(
    symbol: Union[Symbol, str, BaseContract, Security],
    minutes_before_close: float = 0,
    extended_market_close: bool = False,
) -> ITimeRule
before_market_close(
    minutes_before_close: float = 0,
    extended_market_close: bool = False,
) -> ITimeRule
before_market_close(
    symbol: str,
    minutes_before_close: float = 0,
    extended_market_close: bool = False,
) -> ITimeRule

Signature descriptions:

  • Specifies an event should fire at the market close +- minutes_before_close

  • Specifies an event should fire at the market close +- minutes_before_close. Picks, per date, the latest market close across the algorithm's securities, ignoring always-open exchanges. Defaults to US equities (SPY) when no eligible security is subscribed.

Parameters:

Name Type Description Default
symbol Optional[Union[Symbol, str, BaseContract, Security] | str]

The symbol whose market close we want an event for

None
minutes_before_close float

The time before market close that the event should fire

0
extended_market_close bool

True to use extended market close, false to use regular market close

False

Returns:

Type Description
ITimeRule

Depends on the signature used. Case 1: [A time rule that fires the specified number of minutes before the symbol's market close.]; Case 2: [A time rule that fires the specified number of minutes before the latest market close.]

before_market_open

before_market_open(
    symbol: Union[Symbol, str, BaseContract, Security],
    minutes_before_open: float = 0,
    extended_market_open: bool = False,
) -> ITimeRule
before_market_open(
    minutes_before_open: float = 0,
    extended_market_open: bool = False,
) -> ITimeRule
before_market_open(
    symbol: str,
    minutes_before_open: float = 0,
    extended_market_open: bool = False,
) -> ITimeRule

Signature descriptions:

  • Specifies an event should fire at market open +- minutes_before_open

  • Specifies an event should fire at market open +- minutes_before_open. Picks, per date, the earliest market open across the algorithm's securities, ignoring always-open exchanges. Defaults to US equities (SPY) when no eligible security is subscribed.

Parameters:

Name Type Description Default
symbol Optional[Union[Symbol, str, BaseContract, Security] | str]

The symbol whose market open we want an event for

None
minutes_before_open float

The minutes before market open that the event should fire

0
extended_market_open bool

True to use extended market open, false to use regular market open

False

Returns:

Type Description
ITimeRule

Depends on the signature used. Case 1: [A time rule that fires the specified number of minutes before the symbol's market open.]; Case 2: [A time rule that fires the specified number of minutes before the earliest market open.]

every

every(interval: timedelta) -> ITimeRule

Specifies an event should fire periodically on the requested interval

Parameters:

Name Type Description Default
interval timedelta

The frequency with which the event should fire, can not be zero or less

required

Returns:

Type Description
ITimeRule

A time rule that fires after each interval passes.

set_default_time_zone

set_default_time_zone(time_zone: Any) -> None

Sets the default time zone

Parameters:

Name Type Description Default
time_zone Any

The time zone to use for helper methods that can't resolve a time zone

required

get_market_open_close_exchange_hours

get_market_open_close_exchange_hours() -> (
    Sequence[SecurityExchangeHours]
)

Helper method to fetch the exchange hours of the securities currently in securities whose markets are not always open. If no such securities are present, falls back to US equities (SPY).

This Class is protected.

get_security_exchange_hours

get_security_exchange_hours(
    symbol: Union[Symbol, str, BaseContract, Security],
) -> SecurityExchangeHours

Helper method to fetch the security exchange hours

This Class is protected.

get_symbol

get_symbol(ticker: str) -> Symbol

This Class is protected.