sim_platform.research.portfolio

Portfolio construction: from signals or returns to weights, share targets and orders.

Pure functions over plain lists and dictionaries, so they work in a notebook, in a model file’s own environment (copy this module) and in an Agent’s local tool.

  • Weights: equal_weights, inverse_volatility, risk_parity, minimum_variance (long only) and signal_weights (scale signals to a gross).

  • Risk: volatility_target scales weights to a target annual volatility, and apply_limits caps each weight, the gross and the net exposure.

  • Execution: target_shares turns weights into whole shares at given prices, and rebalance_orders lists the buys and sells that move holdings to the targets.

Money and prices are integers in ten-thousandths of a dollar, as everywhere in Market Lab; returns are decimals per period.

sim_platform.research.portfolio.equal_weights(symbols)[source]

1/n in each symbol.

Parameters:

symbols (Sequence[str])

Return type:

Dict[str, float]

sim_platform.research.portfolio.inverse_volatility(returns)[source]

Weights proportional to 1/volatility, summing to one.

Parameters:

returns (Mapping[str, Sequence[float]])

Return type:

Dict[str, float]

sim_platform.research.portfolio.risk_parity(returns, *, iterations=500, tolerance=1e-10)[source]

Long-only weights where each asset contributes equally to portfolio variance.

Solved by the multiplicative fixed point w_i <- w_i * (1/n) / (w_i (Σw)_i / w’Σw), started from inverse volatility.

Parameters:
  • returns (Mapping[str, Sequence[float]])

  • iterations (int)

  • tolerance (float)

Return type:

Dict[str, float]

sim_platform.research.portfolio.minimum_variance(returns, *, iterations=2000, step=None)[source]

Long-only, fully invested minimum-variance weights by projected gradient descent.

Parameters:
  • returns (Mapping[str, Sequence[float]])

  • iterations (int)

  • step (float | None)

Return type:

Dict[str, float]

sim_platform.research.portfolio.signal_weights(signals, *, gross=1.0)[source]

Scale signals (any sign) so absolute weights sum to gross; zeros stay zero.

Parameters:
  • signals (Mapping[str, float])

  • gross (float)

Return type:

Dict[str, float]

sim_platform.research.portfolio.portfolio_volatility(weights, returns, periods_per_year)[source]

Annualised volatility of a weighted portfolio from per-period returns.

Parameters:
  • weights (Mapping[str, float])

  • returns (Mapping[str, Sequence[float]])

  • periods_per_year (float)

Return type:

float

sim_platform.research.portfolio.volatility_target(weights, returns, *, target, periods_per_year=252, max_leverage=1.0)[source]

Scale weights so the portfolio’s annual volatility is target (capped leverage).

Parameters:
  • weights (Mapping[str, float])

  • returns (Mapping[str, Sequence[float]])

  • target (float)

  • periods_per_year (float)

  • max_leverage (float)

Return type:

Dict[str, float]

sim_platform.research.portfolio.apply_limits(weights, *, max_weight=None, max_gross=None, max_net=None)[source]

Cap each absolute weight, then scale down to the gross and net limits.

Parameters:
  • weights (Mapping[str, float])

  • max_weight (float | None)

  • max_gross (float | None)

  • max_net (float | None)

Return type:

Dict[str, float]

sim_platform.research.portfolio.target_shares(weights, prices, equity, *, lot=1)[source]

Whole shares (in multiples of lot) closest below each weight of equity.

Parameters:
  • weights (Mapping[str, float])

  • prices (Mapping[str, int])

  • equity (int)

  • lot (int)

Return type:

Dict[str, int]

sim_platform.research.portfolio.rebalance_orders(current, target, *, min_shares=1)[source]

Buys and sells that move current holdings to target, sells first.

Each order is {'symbol', 'side', 'quantity'}; changes smaller than min_shares are skipped to avoid paying costs for noise.

Parameters:
  • current (Mapping[str, int])

  • target (Mapping[str, int])

  • min_shares (int)

Return type:

List[Dict[str, object]]