ScenariosGenerator

class qf_lib.backtesting.fast_alpha_model_tester.scenarios_generator.ScenariosGenerator[source]

Bases: object

Class used for generating different scenarios for Trades.

Methods:

make_exposure_scenarios(start_date, ...[, ...])

Creates a random series, which contains information about the exposure of a certain asset for the given dates range.

make_scenarios(trade_rets[, ...])

Utility function to generate different trades scenarios, where each scenario is a series of returns for a given investment strategy.

make_exposure_scenarios(start_date: datetime, end_date: datetime, number_of_trades: int, time_in_the_market: float, exposure: Exposure = Exposure.LONG, frequency: Frequency = Frequency.DAILY, seed: Optional[int] = None) QFSeries[source]

Creates a random series, which contains information about the exposure of a certain asset for the given dates range. Based on a.o. the total desired number of trades and average holding time of the trades, the function generates random trades and fills the rows for corresponding dates with the desired exposure. In case if the number of trades provided is too high to create non-adjacent trades, which will together occupy <time_in_the_market>% percentage of time, the time span between some of the consecutive trades may be set to 0. In that case it may seem as if the returned number of trades was smaller than the expected number of trades.

Exemplary output for daily trading, 2 trades, time in the market = 60% and desired exposure = LONG:

2021-10-01 Exposure.OUT 2021-10-02 Exposure.LONG 2021-10-03 Exposure.LONG 2021-10-04 Exposure.LONG 2021-10-05 Exposure.LONG 2021-10-06 Exposure.OUT 2021-10-07 Exposure.OUT 2021-10-08 Exposure.LONG 2021-10-09 Exposure.LONG 2021-10-10 Exposure.OUT

Parameters:
  • start_date (datetime) – first date considered in the returned series

  • end_date (datetime) – last date considered in the returned series

  • number_of_trades (int) – total number of trades, which should be generated

  • time_in_the_market (float) – total time of the ticker in the market (should be a percentage value, between 0.0 and 1.0)

  • exposure (Exposure) – the desired exposure (either short or long)

  • frequency (Frequency) – frequency of the trading

  • seed (int) – seed used to make the scenarios deterministic

Returns:

Series indexed by dates between start_date and end_date with the given frequency

Return type:

QFSeries

make_scenarios(trade_rets: Sequence[float], scenarios_length: int = 100, num_of_scenarios: int = 10000) SimpleReturnsDataFrame[source]

Utility function to generate different trades scenarios, where each scenario is a series of returns for a given investment strategy. The scenarios of a given length are created by randomly choosing (with replacement) returns from the original sequence of a Trade’s returns. The result is the SimpleReturnsDataFrame which is indexed by the Trade’s ordinal number and has a scenario in each column.

Parameters:
  • trade_rets (Sequence[float]) – sequence of floats which represent the returns on Trades performed by some investment strategy

  • scenarios_length (int) – number of Trades which should simulated for each scenario

  • num_of_scenarios (int) – number of scenarios which should be generated

Returns:

data frame of size scenarios_length (rows) by num_of_scenarios (columns). It contains float numbers.

Return type:

SimpleReturnsDataFrame