The Portfolio class provides position tracking, weight optimization, risk analytics, and rebalancing for prediction market portfolios. All heavy computation (Monte Carlo, Greeks, Ledoit-Wolf) runs in Rust.
4 Optimizers
Kelly, equal-weight, risk parity, and minimum variance allocation.
Monte Carlo Risk
VaR, CVaR, win probability, and Greeks via Rust simulation engine.
Auto-Rebalance
Generate rebalancing orders to move from current to target weights.
Live Engine Sync
Build a Portfolio from a running Engine with Portfolio.from_engine().
Build a Portfolio from a running Engine’s live positions and feed data.
engine = hz.Engine(risk_config=hz.RiskConfig(max_position_per_market=500))# ... trade for a while ...portfolio = hz.Portfolio.from_engine(engine, capital=10000.0)print(portfolio.summary())
Compute Kelly-optimal weights given your probability estimates.
kelly_weights = portfolio.optimize_kelly( probs={ "trump-win": 0.60, # Your estimated probability "fed-cut": 0.70, "btc-100k": 0.20, # Probability of YES (you hold NO) }, max_total=1.0, # Cap total Kelly allocation (default: 1.0))# {"trump-win": 0.37, "fed-cut": 0.28, "btc-100k": 0.13}
Inverse-volatility weighting. Uses Ledoit-Wolf shrinkage covariance if return history is provided.
# Without history (falls back to equal weight)rp_weights = portfolio.optimize_risk_parity()# With return history (T x N matrix)returns_history = [ [0.01, -0.02, 0.03], # time step 1: 3 assets [0.02, -0.01, 0.04], # time step 2 [-0.01, 0.03, -0.02], # time step 3 # ... more observations]rp_weights = portfolio.optimize_risk_parity(returns_history)
Generate the orders needed to move from current to target weights.
target = portfolio.optimize_equal_weight()orders = portfolio.rebalance_orders(target)for order in orders: print(f"{order['action']} {order['size_delta']:.1f} of {order['market_id']}") print(f" Current weight: {order['current_weight']:.2%}") print(f" Target weight: {order['target_weight']:.2%}")
Compute portfolio risk metrics including Monte Carlo simulation.
m = portfolio.metrics( n_simulations=10000, # Monte Carlo scenarios seed=42, # Reproducible results t_hours=24.0, # Hours to expiry for Greeks vol=0.2, # Implied vol for Greeks)
Returns a PortfolioMetrics object:
Field
Type
Description
num_positions
int
Number of positions
total_value
float
Total portfolio value
total_pnl
float
Total unrealised PnL
total_pnl_pct
float
PnL as percentage
concentration
float
HHI index
var_95
float
95% Value at Risk (Monte Carlo)
cvar_95
float
95% Conditional VaR
win_probability
float
Probability of positive PnL
greeks
dict[str, PredictionGreeks]
Per-position Greeks (delta, gamma, theta, vega)
correlation_matrix
`list[list[float]]
None`
Ledoit-Wolf covariance if available
shrinkage_intensity
float
Shrinkage parameter
m = portfolio.metrics(seed=42)print(f"VaR 95: ${m.var_95:.2f}")print(f"CVaR 95: ${m.cvar_95:.2f}")print(f"Win Prob: {m.win_probability:.1%}")print(f"HHI: {m.concentration:.3f}")# Per-position Greeksfor market_id, g in m.greeks.items(): print(f"{market_id}: delta={g.delta:.2f}, gamma={g.gamma:.2f}")