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.weights returns 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 DatetimeIndex with 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 positions

  • weights (Dict[str, float] | List[float] | ndarray | DataFrame | Series) – target weights. A Dict or pd.DataFrame is safest, since both are aligned to prices.columns by name; a list or array is positional and must match the column count. A fixed weight vector is applied at every date in rebalancing_freq; a pd.DataFrame supplies its own rebalancing dates, which are ordered chronologically before validation and execution, and rebalancing_freq is then ignored. The caller-owned object is not modified

  • rebalancing_freq (str | None) – calendar anchor for rebalancing when weights is a fixed vector, passed to generate_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, as qis.compute_excess_returns charges; a period with no quote known at its start leaves the nav missing, with a warning

  • management_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.columns by name. Dated rows are ordered chronologically on a local copy before alignment to prices

  • rebalancing_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 prices dates 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 costless

  • weight_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 weights is 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 warning

  • constant_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 prices input does not use a DatetimeIndex

  • ValueError – if prices is not a pd.DataFrame, if its nonempty date index contains NaT or 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 a rebalancing_costs DataFrame is missing a price column, or if a rebalancing_costs Series is indexed by dates rather than tickers

  • NotImplementedError – if weights is of an unsupported type

Return type:

PortfolioData