Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/valuecell/agents/strategy_agent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ class PositionSnapshot(BaseModel):
entry_ts: Optional[int] = Field(
default=None, description="Entry timestamp (ms) for the current position"
)
closed_ts: Optional[int] = Field(
default=None, description="Close timestamp (ms) for recently closed positions"
)
pnl_pct: Optional[float] = Field(
default=None, description="Unrealized P&L as a percent of position value"
)
Expand Down
12 changes: 10 additions & 2 deletions python/valuecell/agents/strategy_agent/portfolio/in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,16 @@ def apply_trades(

# Handle position quantity transitions and avg price
if new_qty == 0.0:
# Fully closed
self._view.positions.pop(symbol, None)
# Fully closed — do NOT remove the position immediately.
# Keep a tombstone snapshot so downstream callers (UI / API)
# that poll holdings immediately after execution can still see
# the just-closed position. Mark it closed with a timestamp.
position.quantity = 0.0
position.mark_price = price
# preserve avg_price and entry_ts for auditing; record closed_ts
position.closed_ts = int(datetime.now(timezone.utc).timestamp() * 1000)
position.unrealized_pnl = 0.0
position.unrealized_pnl_pct = None
elif current_qty == 0.0:
# Opening new position
position.quantity = new_qty
Expand Down
5 changes: 4 additions & 1 deletion python/valuecell/server/services/strategy_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ async def get_strategy_holding(strategy_id: str) -> Optional[StrategyHoldingData
for h in holdings:
try:
t = h.type
qty = float(h.quantity) if h.quantity is not None else 0.0
if h.quantity is None or h.quantity == 0.0:
# Skip fully closed positions
continue
qty = float(h.quantity)
positions.append(
PositionHoldingItem(
symbol=h.symbol,
Expand Down