Files
set50-system/backend/app/paper.py
T

42 lines
1.6 KiB
Python

"""Paper portfolio ledger for the first vertical slice."""
from __future__ import annotations
from datetime import datetime, timezone
from math import isfinite
from uuid import uuid4
class PaperLedger:
def __init__(self) -> None:
self._entries: list[dict] = []
def record(self, payload: dict, allowed_symbols: set[str]) -> dict:
symbol = str(payload.get("symbol", "")).strip().upper()
if symbol not in allowed_symbols:
raise ValueError(f"unknown signal symbol: {symbol}")
try:
target_weight = float(payload["target_weight"])
assumed_price = float(payload["assumed_price"])
except (KeyError, TypeError, ValueError) as exc:
raise ValueError("target_weight and assumed_price must be numeric") from exc
if not isfinite(target_weight) or not isfinite(assumed_price):
raise ValueError("target_weight and assumed_price must be finite")
if not -1.0 <= target_weight <= 1.0:
raise ValueError("target_weight must be between -1 and 1")
if assumed_price <= 0:
raise ValueError("assumed_price must be positive")
entry = {
"entry_id": f"paper_{uuid4().hex}",
"created_at": datetime.now(timezone.utc).isoformat(),
"symbol": symbol,
"target_weight": target_weight,
"assumed_price": assumed_price,
"status": "PAPER_RECORDED",
}
self._entries.append(entry)
return entry
def entries(self) -> list[dict]:
return list(self._entries)