FuturesModel

class qf_lib.backtesting.alpha_model.futures_model.FuturesModel(num_of_bars_needed: int, risk_estimation_factor: float, data_provider: DataProvider, cache_path: Optional[str] = None)[source]

Bases: AlphaModel

Type of alpha model, which can be easily integrated with any type of Tickers (plain Ticker objects, as well as FutureTickers). It provides the functionality which allows to download the daily OHLCV prices data frame, regardless of ticker type (the get_data function).

Parameters:
  • num_of_bars_needed (int) – number of bars necessary to compute the exposure

  • risk_estimation_factor (float) – float value which estimates the risk level of the specific AlphaModel. Corresponds to the level at which the stop-loss should be placed.

  • data_provider (DataProvider) – DataProvider which provides data for the ticker. For backtesting purposes the Data Handler should be used.

  • cache_path (Optional[str]) – path to a directory, which could be used by the model for caching purposes. If provided, the model will cache the outputs of get_data function.

Methods:

get_data(ticker_str, end_date, frequency[, ...])

Downloads the OHCLV Prices data frame for the given ticker.

get_signal(ticker, current_exposure, ...)

Returns the Signal calculated for a specific AlphaModel and a set of data for a specified Ticker

get_data(ticker_str: str, end_date: datetime, frequency: Frequency, aggregate_volume: bool = False)[source]

Downloads the OHCLV Prices data frame for the given ticker. In case of a FutureTicker, the function downloads the Futures Chain and applies backward adjustment to the prices.

Parameters:
  • ticker_str (str) – string representing the ticker for which the data should be downloaded

  • end_date (datetime) – last date for the data to be fetched

  • frequency (Frequency) – frequency of the data

  • aggregate_volume (bool) – used only in case of future tickers - if set to True, the volume data would not be the volume for the given contract, but the volume aggregated across all contracts (for each day the volume will be simply the sum of all volumes of the existing contracts of the given future asset)

get_signal(ticker: Ticker, current_exposure: Exposure, current_time: datetime, frequency: Frequency) Signal[source]

Returns the Signal calculated for a specific AlphaModel and a set of data for a specified Ticker

Parameters:
  • ticker (Ticker) – A ticker of an asset for which the Signal should be generated

  • current_exposure (Exposure) – The actual exposure, based on which the AlphaModel should return its Signal. Can be different from previous Signal suggestions, but it should correspond with the current trading position

  • current_time (datetime) – current time, which is afterwards recorded inside each of the Signals. The parameter is optional and if not provided, defaults to the current user time.

  • frequency (Frequency) – frequency of data obtained by the data provider for signal calculation

Returns:

Signal being the suggestion for the next trading period

Return type:

Signal