Skip to content

feat: Phase 2 — L3 Portfolio Construction (v0.4.0)#14

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/longbridge-fs-iteration-1
Draft

feat: Phase 2 — L3 Portfolio Construction (v0.4.0)#14
Copilot wants to merge 2 commits intomainfrom
copilot/longbridge-fs-iteration-1

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

Implements the L3 Portfolio Construction layer of the Harness five-layer pipeline, enabling signal-driven rebalance from target weights through to beancount ORDER entries — all file-system driven.

New: internal/portfolio/ package

  • target.goParseTarget / ValidateTarget: reads portfolio/target.json, validates weights sum ≈ 1.0 and total_capital_pct ∈ (0, 1]
  • diff.goSyncCurrent: builds portfolio/current.json from account/state.json + quote/hold/*/overview.json; ComputeDiff: produces portfolio/diff.json with per-symbol ADD/REDUCE/HOLD/REMOVE actions and estimated delta quantities
  • rebalance.goExecuteRebalance: converts portfolio/rebalance/pending.json to beancount ORDER entries tagged with source: rebalance + rebalance_id, archives the file to portfolio/history/, then removes it
  • portfolio.goSyncPortfolio orchestrates the three steps above in sequence

Model types (internal/model/types.go)

Added: PortfolioTarget, TargetPosition, PortfolioCurrent, CurrentPosition, PortfolioDiff, PortfolioAdjustment, RebalancePending, RebalanceOrder

Controller integration (cmd/longbridge-fs/main.go)

  • portfolio.SyncPortfolio(root, autoRebalance) injected into polling loop after account refresh
  • New --auto-rebalance flag (default false) — when false, only manually placed pending.json files are executed; the diff is always computed for Agent review
  • fs init now creates a default portfolio/target.json skeleton

Example rebalance flow:

# Agent writes portfolio/target.json with desired weights
# Controller next tick:
#   1. Writes portfolio/current.json  (live snapshot)
#   2. Writes portfolio/diff.json     (AAPL: REDUCE -67, 700.HK: ADD +80)
# Agent reviews diff, writes portfolio/rebalance/pending.json
# Controller next tick:
#   3. Appends to trade/beancount.txt:
#        ; source: rebalance
#        ; rebalance_id: rebal-20260330-001
#   4. Archives pending.json → portfolio/history/

Copilot AI linked an issue Mar 31, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Implement requirements from spec.md for iteration 1 feat: Phase 2 — L3 Portfolio Construction (v0.4.0) Mar 31, 2026
Copilot AI requested a review from JetSquirrel March 31, 2026 12:41
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.

[Auto] Development Iteration 1

2 participants