AbstractPriceDataProvider

class qf_lib.data_providers.abstract_price_data_provider.AbstractPriceDataProvider[source]

Bases: DataProvider

Interface for data providers that supply historical data for various asset classes, including stocks, indices, and futures.

This base class is designed for simple data providers, which are linked to a single data source (e.g., Quandl, Bloomberg, Yahoo). It defines the standard structure and methods that any specific data provider implementation must adhere to in order to access and retrieve historical market data.

Notes

  • When implementing the get_history method (which drivers a large portion of backtesting capabilities) careful consideration must be taken

to ensure the data is returned in the expected format depending on the specific input tickers and fields. For Example:

  • isinstance(tickers, str) and isinstance(fields, str) it is expected to return a PriceSeries object

  • isinstance(tickers, str) and isinstance(fields, list) it is expected to return a PricesDataframe object

  • otherwise it is expected to return a QFDataArray object

Methods:

expiration_date_field_str_map([ticker])

Method has to be implemented in each data provider in order to be able to use get_futures_chain_tickers.

get_futures_chain_tickers(tickers, ...)

Returns tickers of futures contracts, which belong to the same futures contract chain as the provided ticker (tickers), along with their expiration dates in form of a QFSeries or QFDataFrame.

get_price(tickers, fields, start_date[, ...])

Gets adjusted historical Prices (Open, High, Low, Close) and Volume

price_field_to_str_map()

Method has to be implemented in each data provider in order to be able to use get_price.

str_to_expiration_date_field_map([ticker])

Inverse of str_to_expiration_date_field_map.

str_to_price_field_map()

Inverse of price_field_to_str_map.

abstract expiration_date_field_str_map(ticker: Optional[Ticker] = None) Dict[ExpirationDateField, str][source]

Method has to be implemented in each data provider in order to be able to use get_futures_chain_tickers. Returns dictionary containing mapping between ExpirationDateField and corresponding string that has to be used by get_futures_chain_tickers method.

Parameters:

ticker (None, Ticker) – ticker is optional and might be uses by particular data providers to create appropriate dictionary

Returns:

mapping between ExpirationDateField and corresponding strings

Return type:

Dict[ExpirationDateField, str]

get_futures_chain_tickers(tickers: Union[FutureTicker, Sequence[FutureTicker]], expiration_date_fields: Union[ExpirationDateField, Sequence[ExpirationDateField]]) Dict[FutureTicker, QFDataFrame][source]

Returns tickers of futures contracts, which belong to the same futures contract chain as the provided ticker (tickers), along with their expiration dates in form of a QFSeries or QFDataFrame.

Parameters:
Returns:

Returns a dictionary, which maps Tickers to QFSeries, consisting of the expiration dates of Future Contracts: Dict[FutureTicker, Union[QFSeries, QFDataFrame]]]. The QFSeries’ / QFDataFrames contain the specific Tickers, which belong to the corresponding futures family, same as the FutureTicker, and are indexed by the expiration dates of the specific future contracts.

Return type:

Dict[FutureTicker, Union[QFSeries, QFDataFrame]]

get_price(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[PriceField, Sequence[PriceField]], start_date: datetime, end_date: Optional[datetime] = None, frequency: Frequency = Frequency.DAILY, **kwargs) Union[None, PricesSeries, PricesDataFrame, QFDataArray][source]

Gets adjusted historical Prices (Open, High, Low, Close) and Volume

Parameters:
  • tickers (Ticker, Sequence[Ticker]) – tickers for securities which should be retrieved

  • fields (PriceField, Sequence[PriceField]) – fields of securities which should be retrieved

  • start_date (datetime) – date representing the beginning of historical period from which data should be retrieved

  • end_date (datetime) – date representing the end of historical period from which data should be retrieved; if no end_date was provided, by default the current date will be used

  • frequency (Frequency) – frequency of the data

Returns:

If possible the result will be squeezed so that instead of returning QFDataArray (3-D structure), data of lower dimensionality will be returned. The results will be either an QFDataArray (with 3 dimensions: dates, tickers, fields), PricesDataFrame (with 2 dimensions: dates, tickers or fields. It is also possible to get 2 dimensions ticker and field if single date was provided), or PricesSeries with 1 dimension: dates. All the containers will be indexed with PriceField whenever possible (for example: instead of ‘Close’ column in the PricesDataFrame there will be PriceField.Close)

Return type:

None, PricesSeries, PricesDataFrame, QFDataArray

abstract price_field_to_str_map() Dict[PriceField, str][source]

Method has to be implemented in each data provider in order to be able to use get_price. Returns dictionary containing mapping between PriceField and corresponding string that has to be used by get_history method to get appropriate type of price series.

Returns:

mapping between PriceFields and corresponding strings

Return type:

Dict[PriceField, str]

str_to_expiration_date_field_map(ticker: Optional[Ticker] = None) Dict[str, ExpirationDateField][source]

Inverse of str_to_expiration_date_field_map.

str_to_price_field_map() Dict[str, PriceField][source]

Inverse of price_field_to_str_map.