DataHandler

class qf_lib.backtesting.data_handler.data_handler.DataHandler(data_provider: DataProvider, timer: Timer)[source]

Bases: DataProvider

DataHandler is a wrapper which can be used with any AbstractPriceDataProvider in both live and backtest environment. It makes sure that data “from the future” is not passed into components in the backtest environment.

DataHandler should be used by all the Backtester’s components (even in the live trading setup).

The goal of a DataHandler is to provide backtester’s components with financial data. It makes sure that no data from the future (relative to a “current” time of a backtester) is being accessed, that is: that there is no look-ahead bias.

Parameters:
  • data_provider (DataProvider) – the underlying data provider

  • timer (Timer) – timer used to keep track of the data “from the future”

Methods:

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_history(tickers, fields, start_date[, ...])

Runs DataProvider.get_history(...) but before makes sure that the query doesn't concern data from the future.

get_last_available_price(tickers[, ...])

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

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

Runs DataProvider.get_price(...) but before makes sure that the query doesn't concern data from the future.

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.

supported_ticker_types()

Returns classes of tickers which are supported by this DataProvider.

use_data_bundle(tickers, fields, start_date, ...)

Optimises running of the backtest.

get_futures_chain_tickers(tickers: Union[FutureTicker, Sequence[FutureTicker]], expiration_date_fields: Union[ExpirationDateField, Sequence[ExpirationDateField]]) Dict[FutureTicker, Union[QFSeries, 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_history(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[str, Sequence[str]], start_date: datetime, end_date: Optional[datetime] = None, frequency: Optional[Frequency] = None, **kwargs) Union[QFSeries, QFDataFrame, QFDataArray][source]

Runs DataProvider.get_history(…) but before makes sure that the query doesn’t concern data from the future.

It accesses the latest fully available bar as of “today”, that is: if a bar wasn’t closed for today yet, then all the PriceFields (e.g. OPEN) will concern data from yesterday.

See also

DataProvider.get_history

abstract 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: Optional[datetime] = None, frequency: Optional[Frequency] = None, **kwargs) Union[PricesSeries, PricesDataFrame, QFDataArray][source]

Runs DataProvider.get_price(…) but before makes sure that the query doesn’t concern data from the future. It always returns the fully available bars (e.g. it will return a full bar for a day only after the market close).

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:

  • None, PricesSeries, PricesDataFrame, QFDataArray

  • param **kwargs:

historical_price(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[PriceField, Sequence[PriceField]], nr_of_bars: int, end_date: Optional[datetime] = None, frequency: Optional[Frequency] = None) 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

supported_ticker_types()[source]

Returns classes of tickers which are supported by this DataProvider.

use_data_bundle(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[PriceField, Sequence[PriceField]], start_date: datetime, end_date: datetime, frequency: Frequency = Frequency.DAILY)[source]

Optimises running of the backtest. All the data will be downloaded before the backtest. Note that requesting during the backtest any other ticker or price field than the ones in the params of this function will result in an Exception.

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

  • start_date (datetime) – initial date that should be downloaded

  • end_date (datetime) – last date that should be downloaded

  • frequency – frequency of the data