FastDataHandler

class qf_lib.backtesting.fast_alpha_model_tester.fast_data_handler.FastDataHandler(data_provider: qf_lib.data_providers.data_provider.DataProvider, timer: qf_lib.common.utils.dateutils.timer.Timer, default_frequency: qf_lib.common.enums.frequency.Frequency = <Frequency.DAILY: 252>)[source]

Bases: qf_lib.backtesting.data_handler.data_handler.DataHandler

Class pretending to be a DataHandler but being much faster and not protecting from the look-ahead bias (one needs to be careful).

Methods

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(ickers, …)

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

historical_price(ticker, fields, …)

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

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)[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. This behaviour is different than the behaviour of get_price function of DataHandler. The reason for that is, that it is impossible to infer which of the fields are available before the market closes (in case of get_price, it is well known that PriceField.Open is available after market opens, but the DataHandler does not have a valid mapping between PriceField.Open and the string pointing to the open price field).

See also

DataProvider.get_history()

get_last_available_price(ickers: 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)[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

historical_price(ticker, fields, num_of_bars_needed, end_date: Optional[datetime.datetime] = None, frequency: qf_lib.common.enums.frequency.Frequency = <Frequency.DAILY: 252>)[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

PricesSeries, PricesDataFrame, QFDataArray