quantlab.execution.approximate#

The approximate backtest’s execution, compiled: a book of weights filled at prices and marked at closes.

The approximate backtest (quantlab.backtest.approximate) gives up most of the Execution rules (quantlab.execution.rules) for speed. What it keeps is here, once, for the backtest’s simulation and for the mean-variance solve that drifts its own holdings forward in time with a turnover penalty (quantlab.portfolio.predefined.approximate._support.mean_variance_kernels.solve_forward):

  • a weight decided at bar t fills at bar t + 1’s fill price, or the previous bar’s valuation price where it has none (entry_prices);

  • between fills a book is its weights at their fill prices, marked at each bar’s valuation price, the rest cash (mark_book); a price missing on either side counts as no change (price_relative);

  • each fill costs one rate times its traded weight, against the previous book marked at the fill’s prices (simulate).

Everything is compiled with numba (cache=True: the compiled code is kept in __pycache__ beside this file, or under NUMBA_CACHE_DIR; an install that cannot write there compiles again in each process). Import this module only where it is used: importing it imports numba.

Functions

entry_prices

[T, S]: the price a weight decided at the previous bar fills at: the fill price, else the previous close.

ffill

Forward-fill NaN down the rows of a [T, S] array.

mark_book

Mark a book at prices: marked its positions' values, returning the book's value, cash included.

price_relative

price / entry, 1.0 where it is not finite (a price missing on either side).

simulate

Simulate the targets decided at decisions in one pass over the bars; (value, traded, fill_value, filled).