AbstractPriceDataProvider

class qf_lib.data_providers.abstract_price_data_provider.AbstractPriceDataProvider(timer: Optional[Timer] = None)[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:

get_last_available_price(tickers[, ...])

Gets the latest available price for given assets as of end_time.

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

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

historical_price(tickers, fields, nr_of_bars)

Returns the latest available data samples, which simply correspond to the last available <nr_of_bars> number of bars.

price_field_to_str_map(*args)

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

str_to_price_field_map()

Inverse of price_field_to_str_map.

get_last_available_price(tickers: Union[Ticker, Sequence[Ticker]], frequency: Optional[Frequency] = None, end_time: Optional[datetime] = None) Union[float, QFSeries][source]

Gets the latest available price for given assets as of end_time.

Parameters:
  • tickers (Ticker, Sequence[Ticker]) – tickers of the securities which prices should be downloaded

  • frequency (Frequency) – frequency of the data

  • end_time (datetime) – date which should be used as a base to compute the last available price. The parameter is optional and if not provided, the end_date will point to the current user time.

Returns:

last_prices series where: - last_prices.name contains a date of current prices, - last_prices.index contains tickers - last_prices.data contains latest available prices for given tickers

Return type:

float, pandas.Series

get_price(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[PriceField, Sequence[PriceField]], start_date: datetime, end_date: datetime = None, frequency: Frequency = Frequency.DAILY, look_ahead_bias: Optional[bool] = False, **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

  • look_ahead_bias (False) – if set to False, no future data will be ever returned

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

historical_price(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[PriceField, Sequence[PriceField]], nr_of_bars: int, end_date: Optional[datetime] = None, frequency: Frequency = None, **kwargs) Union[PricesSeries, PricesDataFrame, QFDataArray][source]

Returns the latest available data samples, which simply correspond to the last available <nr_of_bars> number of bars.

In case of intraday data and N minutes frequency, the most recent data may not represent exactly N minutes (if the whole bar was not available at this time). The time ranges are always aligned to the market open time. Non-zero seconds and microseconds are in the above case omitted (the output at 11:05:10 will be exactly the same as at 11:05).

Parameters:
  • tickers (Ticker, Sequence[Ticker]) – ticker or sequence of tickers of the securities

  • fields (PriceField, Sequence[PriceField]) – PriceField or sequence of PriceFields of the securities

  • nr_of_bars (int) – number of data samples (bars) to be returned. Note: while requesting more than one ticker, some tickers may have fewer than n_of_bars data points

  • end_date (Optional[datetime]) – last date which should be considered in the query, the nr_of_bars that should be returned will always point to the time before end_date. The parameter is optional and if not provided, the end_date will point to the current user time.

  • frequency – frequency of the data

Return type:

PricesSeries, PricesDataFrame, QFDataArray

abstract price_field_to_str_map(*args) 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_price_field_map() Dict[str, PriceField][source]

Inverse of price_field_to_str_map.