OrderFactory

class qf_lib.backtesting.order.order_factory.OrderFactory(broker: Broker, data_provider: DataProvider)[source]

Bases: object

Creates Orders.

Parameters:
  • broker (Broker) – broker used to access the portfolio

  • data_provider (DataProvider) – data provider used to download prices. In case of backtesting, the DataHandler wrapper should be used.

Methods:

orders(quantities, execution_style, ...)

Creates a list of Orders for given numbers of shares for each given asset.

percent_orders(percentages, execution_style, ...)

Creates a list of Orders by specifying the percentage of the current portfolio value which should be spent on each asset.

target_orders(target_quantities, ...[, ...])

Creates a list of Orders from a dictionary of desired target number of shares (number of shares which should be present in the portfolio after executing the Order).

target_percent_orders(target_percentages, ...)

Creates an Order adjusting a position to a value equal to the given percentage of the portfolio.

target_value_orders(target_values, ...[, ...])

Creates a list of Orders by specifying how much should be allocated in each asset after the Orders have been executed.

value_orders(values, execution_style, ...[, ...])

Creates a list of Orders by specifying the amount of money which should be spent on each asset rather than the number of shares to buy/sell.

orders(quantities: Mapping[Ticker, float], execution_style: ExecutionStyle, time_in_force: TimeInForce) List[Order][source]

Creates a list of Orders for given numbers of shares for each given asset.

Orders requiring 0 shares will be removed from resulting order list

Parameters:
  • quantities (Mapping[Ticker, float]) – mapping of a Ticker to an amount of shares which should be bought/sold. If number is positive then asset will be bought. Otherwise it will be sold.

  • execution_style (ExecutionStyle) – execution style of an order (e.g. MarketOrder, StopOrder, etc.)

  • time_in_force (TimeInForce) – e.g. ‘DAY’ (Order valid for one trading session), ‘GTC’ (good till cancelled)

Returns:

list of generated orders

Return type:

List[Order]

percent_orders(percentages: Mapping[Ticker, float], execution_style: ExecutionStyle, time_in_force: TimeInForce, frequency: Optional[Frequency] = None) List[Order][source]

Creates a list of Orders by specifying the percentage of the current portfolio value which should be spent on each asset.

Parameters:
  • percentages (Mapping[Ticker, int]) – mapping of a Ticker to a percentage value of the current portfolio which should be allocated in the asset. This is specified as a decimal value (e.g. 0.5 means 50%)

  • execution_style (ExecutionStyle) – execution style of an order (e.g. MarketOrder, StopOrder, etc.)

  • time_in_force (TimeInForce) – e.g. ‘DAY’ (Order valid for one trading session), ‘GTC’ (good till cancelled)

  • frequency (Frequency) – frequency for the last available price sampling (daily or minutely)

Returns:

list of generated orders

Return type:

List[Order]

target_orders(target_quantities: Mapping[Ticker, float], execution_style: ExecutionStyle, time_in_force: TimeInForce, tolerance_quantities: Optional[Mapping[Ticker, float]] = None) List[Order][source]

Creates a list of Orders from a dictionary of desired target number of shares (number of shares which should be present in the portfolio after executing the Order).

If the position doesn’t already exist, the new Order is placed for the :target_quantity of shares. If the position does exist the Order for the difference between the target number of shares and the current number of shares is placed.

Parameters:
  • target_quantities (Mapping[Ticker, int]) – mapping of a Ticker to a target number of shares which should be present in the portfolio after the Order is executed. After comparing with tolerance the math.floor of the quantity will be taken for assets other than crypto. The fraction value will be taken for crypto.

  • execution_style (ExecutionStyle) – execution style of an order (e.g. MarketOrder, StopOrder, etc.)

  • time_in_force (TimeInForce) – e.g. ‘DAY’ (Order valid for one trading session), ‘GTC’ (good till cancelled)

  • tolerance_quantities (None, Mapping[Ticker, int]) – tells what is a tolerance for the target_quantities (in both directions) for each Ticker. The tolerance is expressed in shares. For example: assume that currently the portfolio contains 100 shares of asset A. then calling target_orders({A: 101}, …, tolerance_quantities={A: 2}) will not generate any trades as the tolerance of 2 allows the allocation to be 100. while target value is 101. Another example: assume that currently the portfolio contains 100 shares of asset A. then calling target_value_order({A: 103}, …, tolerance_quantities={A: 2}) will generate a BUY order for 3 shares if abs(target - actual) > tolerance buy or sell assets to match the target If tolerance for a specific ticker is not provided it is assumed to be 0

Returns:

list of generated orders

Return type:

List[Order]

target_percent_orders(target_percentages: Mapping[Ticker, float], execution_style: ExecutionStyle, time_in_force: TimeInForce, tolerance_percentage: float = 0.0, frequency: Optional[Frequency] = None) List[Order][source]

Creates an Order adjusting a position to a value equal to the given percentage of the portfolio.

Parameters:
  • target_percentages (Mapping[Ticker, int]) – mapping of a Ticker to a percentage of a current portfolio value which should be allocated in each asset after the Order has been carried out

  • execution_style (ExecutionStyle) – execution style of an order (e.g. MarketOrder, StopOrder, etc.)

  • time_in_force (TimeInForce) – e.g. ‘DAY’ (Order valid for one trading session), ‘GTC’ (good till cancelled)

  • tolerance_percentage (float) – tells the us what is a tolerance to the target_percentages (in both directions). The tolerance is expressed in percentage points (0.02 corresponds to 2pp of the target_value). For more details look at the description of target_value_orders.

  • frequency (Frequency) – frequency for the last available price sampling (daily or minutely)

Returns:

list of generated orders

Return type:

List[Order]

target_value_orders(target_values: Mapping[Ticker, float], execution_style: ExecutionStyle, time_in_force: TimeInForce, tolerance_percentage: float = 0.0, frequency: Optional[Frequency] = None) List[Order][source]

Creates a list of Orders by specifying how much should be allocated in each asset after the Orders have been executed.

For example if we’ve already have 10M invested in ‘SPY US Equity’ and you call this method with target value of 11M then only 1M will be spent on this asset

Parameters:
  • target_values (Mapping[Ticker, float]) – mapping of a Ticker to a value which should be allocated in the asset after the Order has been executed (expressed in the currency in which the asset is traded)

  • execution_style (ExecutionStyle) – execution style of an order (e.g. MarketOrder, StopOrder, etc.)

  • time_in_force (TimeInForce) – e.g. ‘DAY’ (Order valid for one trading session), ‘GTC’ (good till cancelled)

  • tolerance_percentage (float) – tells the us what is a tolerance to the target_values (in both directions). The tolerance is expressed as percentage of target_values. For example: assume that currently the portfolio contains asset A with allocation 10 000$. then calling target_value_order({A: 10 500}, …, tolerance_percentage=0.05) will not generate any trades as the tolerance of 0.05 allows the allocation to be 10 000$, while target value is 10 500$ (tolerance value would be equal to 0.05 * 10 500 = 525 and the difference between current and target value would be < 525$). Another example: For example: assume that currently the portfolio contains asset A with allocation 10 000$. then calling target_value_order({A: 13 000}, …, tolerance_percentage=0.1) will generate a BUY order corresponding to 3000$ of shares. The tolerance of 0.1 does not allow a difference of 3000$ if abs(target - actual) > tolerance_percentage * target value

  • frequency (Frequency) – frequency for the last available price sampling (daily or minutely)

Returns:

list of generated orders

Return type:

List[Order]

value_orders(values: Mapping[Ticker, float], execution_style: ExecutionStyle, time_in_force: TimeInForce, frequency: Optional[Frequency] = None) List[Order][source]

Creates a list of Orders by specifying the amount of money which should be spent on each asset rather than the number of shares to buy/sell.

Parameters:
  • values (Mapping[Ticker, int]) – mapping of a Ticker to the amount of money which should be spent on the asset (expressed in the currency in which the asset is traded)

  • execution_style (ExecutionStyle) – execution style of an order (e.g. MarketOrder, StopOrder, etc.)

  • time_in_force (TimeInForce) – e.g. ‘DAY’ (Order valid for one trading session), ‘GTC’ (good till cancelled)

  • frequency (Frequency) – frequency for the last available price sampling

Returns:

list of generated orders

Return type:

List[Order]