AlphaModel

class qf_lib.backtesting.alpha_model.alpha_model.AlphaModel(risk_estimation_factor: float, data_provider: DataProvider)[source]

Bases: object

Base class for all alpha models.

Parameters:
  • risk_estimation_factor – 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 the backtesting purposes, in order to avoid looking into the future, use DataHandler wrapper.

Methods:

calculate_exposure(ticker, current_exposure, ...)

Returns the expected Exposure, which is the key part of a generated Signal.

calculate_fraction_at_risk(ticker, ...)

Returns the float value which determines the risk factor for an AlphaModel and a specified Ticker, may be used to calculate the position size.

get_signal(ticker, current_exposure, ...)

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

abstract calculate_exposure(ticker: Ticker, current_exposure: Exposure, current_time: datetime, frequency: Frequency) Exposure[source]

Returns the expected Exposure, which is the key part of a generated Signal. Exposure suggests the trend direction for managing the trading position. Uses DataHandler passed when the AlphaModel (child) is initialized - all required data is provided in the child class.

Parameters:
  • ticker (Ticker) – Ticker for which suggested signal exposure is calculated.

  • 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) – The time of the exposure calculation

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

calculate_fraction_at_risk(ticker: Ticker, current_time: datetime, frequency: Frequency) float[source]

Returns the float value which determines the risk factor for an AlphaModel and a specified Ticker, may be used to calculate the position size.

For example: Value of 0.02 means that we should place a Stop Loss 2% below the latest available price of the instrument. This value should always be positive

Parameters:
  • ticker (Ticker) – Ticker for which the calculation should be made

  • current_time (datetime) – The time of the fraction at risk calculation

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

Returns:

percentage_at_risk value for an AlphaModel and a Ticker, calculated as Normalized Average True Range multiplied by the risk_estimation_factor, being a property of each AlphaModel: fraction_at_risk = ATR / last_close * risk_estimation_factor

Return type:

float

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