61 lines
2.6 KiB
Python
61 lines
2.6 KiB
Python
from __future__ import annotations
|
|
from typing import List
|
|
from .dsl import MarketSignal, PlanDelta, StrategyDelta
|
|
|
|
class ADMMCoordinator:
|
|
"""
|
|
Lightweight ADMM-like coordinator.
|
|
It collects MarketSignals from multiple venues and emits a PlanDelta
|
|
representing a synchronized hedging action.
|
|
This is a minimal, deterministic stub suitable for MVP testing.
|
|
"""
|
|
def __init__(self, max_iterations: int = 5, contract_id: str = "default-contract"):
|
|
# Number of iterative reconciliation steps. Exposed for tests and MVP flexibility.
|
|
self.max_iterations = max_iterations
|
|
self.contract_id = contract_id
|
|
self.last_plan: PlanDelta | None = None
|
|
|
|
def reconcile(self, plan: PlanDelta) -> PlanDelta:
|
|
# Consolidate all per-asset delta_positions from input plan.deltas into
|
|
# a single StrategyDelta with a delta_positions dict.
|
|
consolidated_positions = {}
|
|
if plan and getattr(plan, "deltas", None):
|
|
for d in plan.deltas:
|
|
pos = getattr(d, "delta_positions", None)
|
|
if isinstance(pos, dict):
|
|
for sym, val in pos.items():
|
|
consolidated_positions[sym] = consolidated_positions.get(sym, 0) + val
|
|
consolidated = StrategyDelta(delta_positions=consolidated_positions, notes="consolidated")
|
|
# Augment plan with lightweight dual/consensus metadata to simulate an ADMM-like
|
|
# cross-venue coherence step. This is intentionally a small stub for MVP testing.
|
|
dual_vars = {"rho": 1.0, "alpha": 0.5}
|
|
new_plan = PlanDelta(
|
|
deltas=[consolidated],
|
|
timestamp=getattr(plan, "timestamp", 0.0),
|
|
author=getattr(plan, "author", None),
|
|
contract_id=self.contract_id,
|
|
actions=getattr(plan, "actions", []),
|
|
dual_vars=dual_vars,
|
|
)
|
|
self.last_plan = new_plan
|
|
return new_plan
|
|
|
|
# Compatibility shim for existing runtime that imports Coordinator
|
|
class Coordinator(ADMMCoordinator):
|
|
def __init__(self, contract_id: str = "default-contract"):
|
|
super().__init__(contract_id=contract_id)
|
|
|
|
def coordinate(self, signals, author: str = "coordinator") -> PlanDelta:
|
|
# Lightweight, deterministic plan synthesis for MVP
|
|
merged = StrategyDelta(notes="coordinated")
|
|
# Propagate a minimal coherence envelope along with a pointer to the contract id.
|
|
dual_vars = {"rho": 1.0, "alpha": 0.9}
|
|
return PlanDelta(
|
|
deltas=[merged],
|
|
timestamp=0.0,
|
|
author=author,
|
|
contract_id=self.contract_id,
|
|
actions=[],
|
|
dual_vars=dual_vars,
|
|
)
|