27 lines
959 B
Python
27 lines
959 B
Python
from __future__ import annotations
|
|
from typing import List
|
|
from .dsl import MarketSignal, PlanDelta
|
|
|
|
class Backtester:
|
|
"""
|
|
Tiny deterministic replay engine.
|
|
Applies PlanDelta actions to a simple PnL model.
|
|
"""
|
|
def __init__(self, initial_cash: float = 100000.0):
|
|
self.cash = initial_cash
|
|
self.positions: List[dict] = []
|
|
|
|
def apply(self, signals: List[MarketSignal], plan: PlanDelta) -> float:
|
|
# Very simple PnL: sum(action.size * current_price) and adjust cash
|
|
pnl = 0.0
|
|
for act in plan.delta:
|
|
symbol = act.get("symbol") or act.get("asset") or "UNKNOWN"
|
|
size = act.get("size", 0.0)
|
|
price = act.get("price") or act.get("premium") or 0.0
|
|
if price is None:
|
|
price = 0.0
|
|
pnl += size * price
|
|
self.positions.append({"symbol": symbol, "size": size, "price": price})
|
|
self.cash += pnl
|
|
return self.cash
|