Skip to content

MarketHoursDatabase

QuantConnect.Securities.MarketHoursDatabase

MarketHoursDatabase(
    exchange_hours: Dictionary[SecurityDatabaseKey, Entry],
)

Bases: BaseSecurityDatabase[QuantConnect_Securities_MarketHoursDatabase, QuantConnect_Securities_MarketHoursDatabase_Entry]

Provides access to exchange hours and raw data times zones in various markets

Initializes a new instance of the MarketHoursDatabase class

Parameters:

Name Type Description Default
exchange_hours Dictionary[SecurityDatabaseKey, Entry]

The full listing of exchange hours by key

required

exchange_hours_listing

exchange_hours_listing: List[
    KeyValuePair[SecurityDatabaseKey, Entry]
]

Gets all the exchange hours held by this provider

ALWAYS_OPEN

ALWAYS_OPEN: MarketHoursDatabase

Gets a MarketHoursDatabase that always returns SecurityExchangeHours.always_open

data_folder_database

data_folder_database: (
    QuantConnect_Securities_BaseSecurityDatabase_T
)

The database instance loaded from the data folder

This codeEntityType is protected.

DATA_FOLDER_DATABASE_LOCK

DATA_FOLDER_DATABASE_LOCK: Object = ...

Lock object for the data folder database

This codeEntityType is protected.

entries

entries: Dictionary[
    SecurityDatabaseKey,
    QuantConnect_Securities_BaseSecurityDatabase_TEntry,
]

The database entries

This codeEntityType is protected.

custom_entries

custom_entries: HashSet[SecurityDatabaseKey]

Custom entries set by the user.

This codeEntityType is protected.

Entry

Entry(
    data_time_zone: Any,
    exchange_hours: SecurityExchangeHours,
)

Bases: Object

Represents a single entry in the MarketHoursDatabase

Initializes a new instance of the Entry class

Parameters:

Name Type Description Default
data_time_zone Any

The raw data time zone

required
exchange_hours SecurityExchangeHours

The security exchange hours for this entry

required

data_time_zone

data_time_zone: Any

Gets the raw data time zone for this entry

exchange_hours

exchange_hours: SecurityExchangeHours

Gets the exchange hours for this entry

get_entry

get_entry(
    market: str, symbol: str, security_type: SecurityType
) -> Entry
get_entry(
    market: str,
    symbol: Union[Symbol, str, BaseContract],
    security_type: SecurityType,
) -> Entry

Gets the entry for the specified market/symbol/security-type

Parameters:

Name Type Description Default
market str

The market the exchange resides in, i.e, 'usa', 'fxcm', ect...

required
symbol str | Union[Symbol, str, BaseContract]

The particular symbol being traded

required
security_type SecurityType

The security type of the symbol

required

Returns:

Type Description
Entry

The entry matching the specified market/symbol/security-type.

get_exchange_hours

get_exchange_hours(
    configuration: SubscriptionDataConfig,
) -> SecurityExchangeHours
get_exchange_hours(
    market: str,
    symbol: Union[Symbol, str, BaseContract],
    security_type: SecurityType,
) -> SecurityExchangeHours

Convenience method for retrieving exchange hours from market hours database using a subscription config

Parameters:

Name Type Description Default
configuration Optional[SubscriptionDataConfig]

The subscription data config to get exchange hours for

None
market Optional[str]

The market the exchange resides in, i.e, 'usa', 'fxcm', ect...

None
symbol Optional[Union[Symbol, str, BaseContract]]

The particular symbol being traded

None
security_type Optional[SecurityType]

The security type of the symbol

None

Returns:

Type Description
SecurityExchangeHours

Depends on the signature used. Case 1: [The configure exchange hours for the specified configuration.]; Case 2: [The exchange hours for the specified security.]

try_get_entry

try_get_entry(
    market: str,
    symbol: Union[Symbol, str, BaseContract],
    security_type: SecurityType,
    entry: Optional[Entry],
) -> Tuple[bool, Entry]
try_get_entry(
    market: str,
    symbol: str,
    security_type: SecurityType,
    entry: Optional[Entry],
) -> Tuple[bool, Entry]

Tries to get the entry for the specified market/symbol/security-type

Parameters:

Name Type Description Default
market str

The market the exchange resides in, i.e, 'usa', 'fxcm', ect...

required
symbol Union[Symbol, str, BaseContract] | str

The particular symbol being traded

required
security_type SecurityType

The security type of the symbol

required
entry Optional[Entry]

The entry found if any

required

Returns:

Type Description
Tuple[bool, Entry]

True if the entry was present, else false.

from_data_folder

from_data_folder() -> MarketHoursDatabase

Gets the instance of the MarketHoursDatabase class produced by reading in the market hours data found in /Data/market-hours/

Returns:

Type Description
MarketHoursDatabase

A MarketHoursDatabase class that represents the data in the market-hours folder.

from_file

from_file(path: str) -> MarketHoursDatabase

Reads the specified file as a market hours database instance

Parameters:

Name Type Description Default
path str

The market hours database file path

required

Returns:

Type Description
MarketHoursDatabase

A new instance of the MarketHoursDatabase class.

get_data_time_zone

get_data_time_zone(
    market: str,
    symbol: Union[Symbol, str, BaseContract],
    security_type: SecurityType,
) -> Any

Performs a lookup using the specified information and returns the data's time zone if found, if an entry is not found, an exception is thrown

Parameters:

Name Type Description Default
market str

The market the exchange resides in, i.e, 'usa', 'fxcm', ect...

required
symbol Union[Symbol, str, BaseContract]

The particular symbol being traded

required
security_type SecurityType

The security type of the symbol

required

Returns:

Type Description
Any

The raw data time zone for the specified security.

set_entry

set_entry(
    market: str,
    symbol: str,
    security_type: SecurityType,
    exchange_hours: SecurityExchangeHours,
    data_time_zone: Any = None,
) -> Entry

Sets the entry for the specified market/symbol/security-type. This is intended to be used by custom data and other data sources that don't have explicit entries in market-hours-database.csv. At run time, the algorithm can update the market hours database via calls to AddData.

Parameters:

Name Type Description Default
market str

The market the exchange resides in, i.e, 'usa', 'fxcm', ect...

required
symbol str

The particular symbol being traded

required
security_type SecurityType

The security type of the symbol

required
exchange_hours SecurityExchangeHours

The exchange hours for the specified symbol

required
data_time_zone Any

The time zone of the symbol's raw data. Optional, defaults to the exchange time zone

None

Returns:

Type Description
Entry

The entry matching the specified market/symbol/security-type.

set_entry_always_open

set_entry_always_open(
    market: str,
    symbol: str,
    security_type: SecurityType,
    time_zone: Any,
) -> Entry

Convenience method for the common custom data case. Sets the entry for the specified symbol using SecurityExchangeHours.AlwaysOpen(time_zone) This sets the data time zone equal to the exchange time zone as well.

Parameters:

Name Type Description Default
market str

The market the exchange resides in, i.e, 'usa', 'fxcm', ect...

required
symbol str

The particular symbol being traded

required
security_type SecurityType

The security type of the symbol

required
time_zone Any

The time zone of the symbol's exchange and raw data

required

Returns:

Type Description
Entry

The entry matching the specified market/symbol/security-type.

contains_key

contains_key(key: SecurityDatabaseKey) -> bool
contains_key(
    market: str, symbol: str, security_type: SecurityType
) -> bool
contains_key(
    market: str,
    symbol: Union[Symbol, str, BaseContract],
    security_type: SecurityType,
) -> bool

Signature descriptions:

  • Determines if the database contains the specified key

  • Check whether an entry exists for the specified market/symbol/security-type

Parameters:

Name Type Description Default
key Optional[SecurityDatabaseKey]

The key to search for

None
market Optional[str]

The market the exchange resides in, i.e, 'usa', 'fxcm', ect...

None
symbol Optional[str | Union[Symbol, str, BaseContract]]

The particular symbol being traded

None
security_type Optional[SecurityType]

The security type of the symbol

None

Returns:

Type Description
bool

True if an entry is found, otherwise false.

get_database_symbol_key

get_database_symbol_key(
    symbol: Union[Symbol, str, BaseContract],
) -> str

Gets the correct string symbol to use as a database key

Parameters:

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

The symbol

required

Returns:

Type Description
str

The symbol string used in the database ke.

reset

reset() -> None

Resets the database, forcing a reload when reused. Called in tests where multiple algorithms are run sequentially, and we need to guarantee that every test starts with the same environment.