AlphaModel¶
-
class
qf_lib.backtesting.alpha_model.alpha_model.
AlphaModel
(risk_estimation_factor: float, data_provider: qf_lib.data_providers.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: qf_lib.common.tickers.tickers.Ticker, current_exposure: qf_lib.backtesting.alpha_model.exposure_enum.Exposure, current_time: datetime.datetime, frequency: qf_lib.common.enums.frequency.Frequency) → qf_lib.backtesting.alpha_model.exposure_enum.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: qf_lib.common.tickers.tickers.Ticker, current_time: datetime.datetime, frequency: qf_lib.common.enums.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
- 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: qf_lib.common.tickers.tickers.Ticker, current_exposure: qf_lib.backtesting.alpha_model.exposure_enum.Exposure, current_time: datetime.datetime, frequency: qf_lib.common.enums.frequency.Frequency) → qf_lib.backtesting.signals.signal.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