34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
from __future__ import annotations
|
|
from typing import List, Optional, Dict, Any
|
|
from .dsl import LocalEvent, PlanDelta, OrderEvent, FillEvent, TraceGraph
|
|
|
|
class ReplayEngine:
|
|
def __init__(self, delta_stream: List[object], event_log: List[object]):
|
|
self.delta_stream = delta_stream
|
|
self.event_log = event_log
|
|
|
|
def replay(self) -> Dict[str, Any]:
|
|
deltas_by_id = {}
|
|
for item in self.delta_stream:
|
|
if isinstance(item, PlanDelta):
|
|
deltas_by_id[item.delta_id] = item
|
|
|
|
known = 0
|
|
total = 0
|
|
details: List[str] = []
|
|
for ev in self.event_log:
|
|
if isinstance(ev, FillEvent):
|
|
total += 1
|
|
if ev.delta_id and ev.delta_id in deltas_by_id:
|
|
known += 1
|
|
details.append(f"Fill {ev.fill_id} matched delta {ev.delta_id}")
|
|
else:
|
|
details.append(f"Fill {ev.fill_id} has no matching delta")
|
|
fidelity = (known / total) if total else 0.0
|
|
return {
|
|
"total_fills": total,
|
|
"known_delta_fills": known,
|
|
"fidelity": fidelity,
|
|
"details": details,
|
|
}
|