Skip to content

feat(portfolio): value-history tracking, returns analysis, and portfolio statistics - #114

Merged
belarusian merged 1 commit into
mainfrom
build36/portfolio-value-history
Aug 16, 2026
Merged

feat(portfolio): value-history tracking, returns analysis, and portfolio statistics#114
belarusian merged 1 commit into
mainfrom
build36/portfolio-value-history

Conversation

@belarusian

Copy link
Copy Markdown
Owner

Summary

Closes a parity gap in the portfolio engine. Portfolio now tracks a
portfolio_values history (seeded with initial cash) and exposes two new
capabilities that were previously computed ad-hoc (or not at all) in core.py:

  • record_value(prices) — append a per-step valuation to the history.
  • calculate_returns(lookback=None) — daily / cumulative / annualized
    returns, Sharpe ratio (risk-free 2%), and max drawdown.
  • calculate_portfolio_statistics(prices) — per-position
    shares/price/value/allocation plus a Herfindahl-Hirschman concentration
    index (raw + normalized) and the number of assets held.

HHI normalization

HHI is computed on renormalized non-cash weights so the index stays in
[0, 1] regardless of the cash position. The raw-fraction form (squaring
allocation fractions that sum to < 1 when cash is held) can produce a
normalized HHI below zero in cash-heavy portfolios; renormalizing the
non-cash weights to sum to 1 keeps the index well-defined.

Tests

  • New TestValueHistory, TestCalculateReturns, TestPortfolioStatistics
    classes in tests/test_portfolio.py covering seeding, recording,
    cumulative/daily returns, max drawdown, lookback windowing, Sharpe sign,
    per-position reporting, and HHI for single/equal/cash-heavy portfolios.
  • New TestMainEntryPoint class covering the alloc.__main__ entry point
    (delegation identity + exit-code propagation).

Full suite: 571 passed (up from 556). ruff check alloc/ and
mypy alloc/ --ignore-missing-imports both clean.

Follow-ups

…rtfolio statistics

Close a seed-parity gap in the portfolio engine. Portfolio now tracks a
portfolio_values history (seeded with initial cash) and exposes:

- record_value(prices): append a per-step valuation to the history
- calculate_returns(lookback): daily/cumulative/annualized returns,
  Sharpe ratio (rf=2%), and max drawdown
- calculate_portfolio_statistics(prices): per-position shares/price/value/
  allocation plus a Herfindahl-Hirschman concentration index (raw and
  normalized) and the number of assets held

HHI is computed on renormalized non-cash weights so the index stays in
[0, 1] regardless of the cash position (the seed's raw-fraction form can
go negative in cash-heavy portfolios). Adds tests for the new methods and
for the alloc.__main__ entry point.
@belarusian
belarusian merged commit 7002b04 into main Aug 16, 2026
1 check passed
@belarusian
belarusian deleted the build36/portfolio-value-history branch August 16, 2026 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant