fix(accounting): use size_usdc (not net_cost) for positions in initialize()
CI/CD / build-and-push (push) Successful in 1m48s

Eliminates the phantom 0.4% exposure overage after pod restarts.

During live trading execute() stores size_usdc in portfolio.positions and
deducts net_cost from cash — so total_value = bankroll − fees and
exposure_pct = sum(size_usdc) / (bankroll − fees).

Old initialize() stored net_cost in positions, making total_value = bankroll
and inflating exposure_pct (observed: 30.085% vs runtime 29.670%).

Fix: new get_open_position_data() returns both {market_id: size_usdc} and
total_net_cost in one query; initialize() uses size_usdc for positions and
total_net_cost for cash — identical model to execute().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
chemavx
2026-04-22 11:05:47 +00:00
parent 8479a63174
commit 8a56bf77d1
2 changed files with 43 additions and 8 deletions
+22 -8
View File
@@ -82,20 +82,34 @@ class PaperExecutor:
Called once after __init__ so the executor reflects any trades that
survived a pod restart. After a TRUNCATE the DB is empty and the
portfolio resets to a full bankroll automatically.
Accounting model (must match execute() exactly):
positions[market_id] = size_usdc (fee excluded — same as runtime)
cash = bankroll - sum(net_cost) (fees already spent)
total_value = cash + sum(size_usdc) = bankroll - sum(fees)
exposure_pct = sum(size_usdc) / total_value
"""
positions = await self._db.get_open_positions()
if not positions:
positions_size, total_net_cost = await self._db.get_open_position_data()
if not positions_size:
log.info("No open positions in DB — starting with full bankroll")
return
total_deployed = sum(positions.values())
self._portfolio.positions = positions
self._portfolio.cash = max(0.0, self._portfolio.cash - total_deployed)
positions_value = sum(positions_size.values())
self._portfolio.positions = positions_size
self._portfolio.cash = max(0.0, self._portfolio.cash - total_net_cost)
total_value = self._portfolio.cash + positions_value
exposure_pct = positions_value / total_value if total_value > 0 else 0.0
log.info(
"Restored %d open position(s) from DB — deployed $%.2f, cash $%.2f",
len(positions),
total_deployed,
"Restored %d open position(s) from DB — "
"positions_value=$%.2f net_cost_spent=$%.2f cash=$%.2f "
"total_value=$%.2f exposure=%.2f%%",
len(positions_size),
positions_value,
total_net_cost,
self._portfolio.cash,
total_value,
exposure_pct * 100,
)
def get_portfolio(self) -> Portfolio: