DataHandler¶
-
class
qf_lib.backtesting.data_handler.data_handler.
DataHandler
(data_provider: qf_lib.data_providers.data_provider.DataProvider, timer: qf_lib.common.utils.dateutils.timer.Timer)[source]¶ Bases:
qf_lib.data_providers.data_provider.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, …)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, …)Runs DataProvider.get_price(…) but before makes sure that the query doesn’t concern data from the future.
historical_price
(tickers, …)Returns the latest available data samples, which simply correspond to the last available <nr_of_bars> number of bars.
Returns classes of tickers which are supported by this DataProvider.
use_data_bundle
(tickers, …)Optimises running of the backtest.
-
get_futures_chain_tickers
(tickers: Union[qf_lib.containers.futures.future_tickers.future_ticker.FutureTicker, Sequence[qf_lib.containers.futures.future_tickers.future_ticker.FutureTicker]], expiration_date_fields: Union[qf_lib.common.enums.expiration_date_field.ExpirationDateField, Sequence[qf_lib.common.enums.expiration_date_field.ExpirationDateField]]) → Dict[qf_lib.containers.futures.future_tickers.future_ticker.FutureTicker, Union[qf_lib.containers.series.qf_series.QFSeries, qf_lib.containers.dataframe.qf_dataframe.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
tickers (FutureTicker, Sequence[FutureTicker]) – tickers for which should the future chain tickers be retrieved
expiration_date_fields (ExpirationDateField, Sequence[ExpirationDateField]) – field that should be downloaded as the expiration date field, by default last tradeable date
- 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[qf_lib.common.tickers.tickers.Ticker, Sequence[qf_lib.common.tickers.tickers.Ticker]], fields: Union[str, Sequence[str]], start_date: datetime.datetime, end_date: datetime.datetime = None, frequency: qf_lib.common.enums.frequency.Frequency = None, **kwargs) → Union[qf_lib.containers.series.qf_series.QFSeries, qf_lib.containers.dataframe.qf_dataframe.QFDataFrame, qf_lib.containers.qf_data_array.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[qf_lib.common.tickers.tickers.Ticker, Sequence[qf_lib.common.tickers.tickers.Ticker]], frequency: qf_lib.common.enums.frequency.Frequency = None, end_time: Optional[datetime.datetime] = None) → Union[float, qf_lib.containers.series.qf_series.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[qf_lib.common.tickers.tickers.Ticker, Sequence[qf_lib.common.tickers.tickers.Ticker]], fields: Union[qf_lib.common.enums.price_field.PriceField, Sequence[qf_lib.common.enums.price_field.PriceField]], start_date: datetime.datetime, end_date: datetime.datetime = None, frequency: qf_lib.common.enums.frequency.Frequency = None) → Union[qf_lib.containers.series.prices_series.PricesSeries, qf_lib.containers.dataframe.prices_dataframe.PricesDataFrame, qf_lib.containers.qf_data_array.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
- Return type
None, PricesSeries, PricesDataFrame, QFDataArray
-
historical_price
(tickers: Union[qf_lib.common.tickers.tickers.Ticker, Sequence[qf_lib.common.tickers.tickers.Ticker]], fields: Union[qf_lib.common.enums.price_field.PriceField, Sequence[qf_lib.common.enums.price_field.PriceField]], nr_of_bars: int, end_date: Optional[datetime.datetime] = None, frequency: qf_lib.common.enums.frequency.Frequency = None) → Union[qf_lib.containers.series.prices_series.PricesSeries, qf_lib.containers.dataframe.prices_dataframe.PricesDataFrame, qf_lib.containers.qf_data_array.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
- Returns
- Return type
-
supported_ticker_types
()[source]¶ Returns classes of tickers which are supported by this DataProvider.
-
use_data_bundle
(tickers: Union[qf_lib.common.tickers.tickers.Ticker, Sequence[qf_lib.common.tickers.tickers.Ticker]], fields: Union[qf_lib.common.enums.price_field.PriceField, Sequence[qf_lib.common.enums.price_field.PriceField]], start_date: datetime.datetime, end_date: datetime.datetime, frequency: qf_lib.common.enums.frequency.Frequency = <Frequency.DAILY: 252>)[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