quantlab.execution.rules.ExecutionBook#

class quantlab.execution.rules.ExecutionBook(fill, valuation, delisted, settings=ExecutionSettings(sizing_basis='fill', fees=0.0, slippage=0.0), init_cash=1.0)[source]#

Bases: object

Shares and cash, traded one bar at a time under the Execution rules.

A driver submit``s the weights it decides on a bar and ``trade``s the bars in time order, reading ``shares and cash as it goes. Stepping every bar holds exactly what replay returns for the same weights.

Parameters:
fill, valuation, delistedarray-like

As for plan_orders, on [T, S].

settingsExecutionSettings

The sizing basis, fees and slippage.

init_cashfloat

The starting cash; the book starts with no shares.

Attributes:
settingsExecutionSettings

As given.

sharesnp.ndarray

Signed shares of each symbol after the last bar traded.

cashfloat

Cash after the last bar traded.

Examples

>>> import numpy as np
>>> prices = np.array([[10.0, 20.0], [10.0, 20.0], [12.0, 18.0]])
>>> book = ExecutionBook(prices, prices, np.zeros((3, 2), dtype=bool))
>>> book.submit(0, [0.5, -0.5])
>>> book.trade(1).rejected.tolist()
[False, False]
>>> book.shares.tolist(), book.cash
([0.05, -0.025], 1.0)
submit(row, weights)[source]#

Queue the weights decided at row; they fill FILL_DELAY_BARS later.

Weights submitted again for the same row replace the earlier ones. Weights whose fill bar is past the panel never fill.

Parameters:
rowint

The bar the weights are decided on.

weightsarray-like

Target weights on [S]; NaN keeps a holding.

Examples

>>> import numpy as np
>>> prices = np.full((2, 1), 10.0)
>>> book = ExecutionBook(prices, prices, np.zeros((2, 1), dtype=bool))
>>> book.submit(0, [1.0])
>>> book.trade(0).settled.tolist(), book.shares.tolist()
([False], [0.0])
>>> book.trade(1).settled.tolist(), book.shares.tolist()
([False], [0.1])
trade(bar)[source]#

Run bar’s orders: the weights that fill there and the settlements.

Bars must be traded in time order, each once.

Parameters:
barint

The bar traded.

Returns:
BarExecution

The orders rejected and the holdings settled on the bar.

Raises:
ValueError

If an order meets a price of 0 or less on a symbol that is not being settled, as vectorbt raises.

Examples

Bar 1 fills 0.1 shares of AAA at 10. AAA delists on bar 1 at a valuation of 8, so bar 2 settles them at 8:

>>> import numpy as np
>>> fill = np.array([[10.0], [10.0], [np.nan]])
>>> valuation = np.array([[10.0], [8.0], [np.nan]])
>>> book = ExecutionBook(fill, valuation, np.array([[False], [True], [False]]))
>>> book.submit(0, [1.0])
>>> _ = book.trade(1)
>>> book.trade(2).settled.tolist(), book.shares.tolist(), book.cash
([True], [0.0], 0.8)