qis.backtest_model_portfolio¶
- qis.backtest_model_portfolio(prices, weights, rebalancing_freq='QE', initial_nav=100, funding_rate=None, management_fee=None, instruments_carry=None, rebalancing_costs=None, weight_implementation_lag=None, constant_trade_level=None, is_rebalanced_at_first_date=False, ticker='Portfolio')[source]¶
simulate a rebalanced portfolio from prices and target weights.
The simulation holds units, not weights: target weights are converted to units at each rebalancing and held until the next one, so the realised weights drift with prices in between and reported turnover is the turnover actually traded.
PortfolioData.weightsreturns those realised weights, not the targets.- Parameters:
prices (DataFrame) – instrument prices, columns are tickers. Costs, carry and fees are applied on this grid. A nonempty input must use a
DatetimeIndexwith unique, non-missing timestamps. Rows are ordered chronologically on a local copy before any portfolio state is constructed; the caller-owned object is not modified. Missing quotes use the last observed price for valuation and carry only; existing units cannot trade until a quoted rebalance. Leading missing prices never create positionsweights (Dict[str, float] | List[float] | ndarray | DataFrame | Series) – target weights. A Dict or pd.DataFrame is safest, since both are aligned to
prices.columnsby name; a list or array is positional and must match the column count. A fixed weight vector is applied at every date inrebalancing_freq; a pd.DataFrame supplies its own rebalancing dates, which are ordered chronologically before validation and execution, andrebalancing_freqis then ignored. The caller-owned object is not modifiedrebalancing_freq (str | None) – calendar anchor for rebalancing when
weightsis a fixed vector, passed togenerate_rebalancing_indicators()initial_nav (float) – starting nav
funding_rate (Series) – annualised rate applied to positive and negative cash balances. Dated rows are ordered chronologically on a local copy before alignment to
prices. The cash balance held over (t-1, t] earns the latest quote on or before the price date t-1 times the ACT/365 fraction of the period, asqis.compute_excess_returnscharges; a period with no quote known at its start leaves the nav missing, with a warningmanagement_fee (float) – annualised fee accrued on nav
instruments_carry (DataFrame) – per-instrument carry, expressed on nav. Complete panels with unique ticker columns are aligned to
prices.columnsby name. Dated rows are ordered chronologically on a local copy before alignment topricesrebalancing_costs (float | Series | DataFrame) – proportional cost on traded notional, fractional units (0.0010 is 10 bp). A float applies to every instrument and date; a Series indexed by ticker is per-instrument and constant in time; a DataFrame of dates x tickers is reindexed to
pricesdates taking the last schedule row at or before each date, so a cost schedule stated on era boundaries applies from each boundary onward. Numeric pandas extension dtypes are accepted. Costs are read at every trade date, including an opening trade on the first price date; dates before the first schedule row are costlessweight_implementation_lag (int | None) – non-negative integer observations of the price index between a weight being observed and traded, so 1 on a business-day panel trades the next business day. None means zero. It selects the entry price for the units only and leaves prices and instrument returns untouched. Applies only when
weightsis a pd.DataFrame, since a fixed vector has no signal date. A weight whose traded date would fall past the end of the price history is dropped with a warningconstant_trade_level (float) – size each rebalancing off this notional rather than off current nav, so trade size does not compound with performance
is_rebalanced_at_first_date (bool) – rebalance on the first price date as well as on the schedule
ticker (str) – name carried on the resulting portfolio
- Returns:
PortfolioData holding the nav, realised weights, units, instrument pnl and realised costs, including costs charged on an opening trade
- Raises:
TypeError – if a nonempty
pricesinput does not use aDatetimeIndexValueError – if
pricesis not a pd.DataFrame, if its nonempty date index containsNaTor duplicate timestamps, if the dated-weight implementation lag is neither None nor a non-negative integer, if a weight vector does not match the number of price columns, if the price history starts after the weights do, if two weight dates resolve to the same traded date on the price index, if no weight date is traded at all, if arebalancing_costsDataFrame is missing a price column, or if arebalancing_costsSeries is indexed by dates rather than tickersNotImplementedError – if
weightsis of an unsupported type
- Return type: