fix(ledger): ribilancio conserva l'equity (no doppio conteggio in-position)

Bug: i flat si dividevano l'INTERO total includendo il capitale dei worker in
posizione, che lo trattenevano in piu' -> equity gonfiata di Sigma(capital-alloc)
sugli in-pos. Caso MR02_BTC 15m seedato (181.19) e in posizione al ribilancio
00:01: +4.77 fantasma. allocate(weights, reserved={sid:cap}): gli in-pos
trattengono il loro, i flat si dividono total-Sigma(reserved) per peso
rinormalizzato -> Sigma alloc == total sempre. Parita' runner intatta
(5.8e-08). Test conservazione (ledger + runner). Suite verde.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Adriano Dal Pastro
2026-06-13 10:09:36 +00:00
parent 85043bf2d3
commit 432c1ad790
5 changed files with 114 additions and 9 deletions
+17
View File
@@ -8,6 +8,23 @@ def test_alloc_split_by_weights(tmp_path):
assert alloc == {"a": 600.0, "b": 400.0}
def test_alloc_conserves_total_with_reserved(tmp_path):
# un worker in posizione (reserved) trattiene capitale != alloc: i flat si
# dividono il RESTO per peso rinormalizzato e Σalloc == total (no doppio conteggio).
# Regressione del bug 2026-06-13 (MR02_BTC 15m in-pos al ribilancio: +4.77 fantasma).
L = PortfolioLedger("PORTX", total_capital=1000.0, data_dir=tmp_path)
alloc = L.allocate({"a": 0.5, "b": 0.25, "c": 0.25}, reserved={"a": 181.0})
assert abs(sum(alloc.values()) - 1000.0) < 1e-6 # equity conservata
assert alloc["a"] == 181.0 # in-pos tiene il suo
# b e c si dividono 819 per peso 0.25/0.25 -> 409.5 ciascuno
assert abs(alloc["b"] - 409.5) < 1e-6 and abs(alloc["c"] - 409.5) < 1e-6
def test_alloc_no_reserved_is_legacy(tmp_path):
L = PortfolioLedger("PORTX", total_capital=1000.0, data_dir=tmp_path)
assert L.allocate({"a": 0.6, "b": 0.4}) == {"a": 600.0, "b": 400.0}
def test_update_tracks_equity_and_dd(tmp_path):
L = PortfolioLedger("PORTX", total_capital=1000.0, data_dir=tmp_path)
L.update_equity({"a": 700.0, "b": 500.0})
+13 -5
View File
@@ -16,10 +16,18 @@ def test_rebalance_resizes_flat_workers(tmp_path):
assert workers["a"].capital == 600.0 and workers["b"].capital == 600.0
def test_rebalance_skips_in_position_worker(tmp_path):
def test_rebalance_in_position_conserves_equity(tmp_path):
# FIX 2026-06-13: il worker in posizione trattiene il suo capitale (deployato);
# i flat si dividono il RESTO per peso rinormalizzato -> Σcapital CONSERVATO.
# Prima (bug): i flat prendevano peso×total includendo il capitale dell'in-pos
# -> somma gonfiata (qui sarebbe stata 1300, +100 fantasma).
L = PortfolioLedger("PX", total_capital=1000.0, data_dir=tmp_path)
workers = {"a": FakeWorker(700.0, in_position=True), "b": FakeWorker(500.0)}
rebalance_allocations(L, workers, {"a": 0.5, "b": 0.5})
workers = {"a": FakeWorker(700.0, in_position=True),
"b": FakeWorker(300.0), "c": FakeWorker(200.0)} # equity 1200
rebalance_allocations(L, workers, {"a": 0.5, "b": 0.25, "c": 0.25})
assert L.total_capital == 1200.0
assert workers["a"].capital == 700.0 # invariato: posizione aperta
assert workers["b"].capital == 600.0 # flat: riallineato
assert workers["a"].capital == 700.0 # invariato: posizione aperta (reserved)
# b e c si dividono 1200-700=500 per peso 0.25/0.25 rinormalizzato -> 250 ciascuno
assert workers["b"].capital == 250.0 and workers["c"].capital == 250.0
total = sum(w.capital for w in workers.values())
assert total == 1200.0 # equity CONSERVATA dal ribilancio