53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
import time
|
|
|
|
import pytest
|
|
|
|
from deltaforge.dsl import Asset, MarketSignal, StrategyDelta, PlanDelta
|
|
from deltaforge.coordinator import ADMMCoordinator
|
|
from deltaforge.execution import ExecutionEngine
|
|
from deltaforge.backtester import Backtester
|
|
from deltaforge.adapters import equity_feed as equity
|
|
from deltaforge.adapters import options_feed as options
|
|
|
|
|
|
def test_admm_coordinator_consolidates_deltas():
|
|
a1 = Asset(symbol="AAPL", asset_class="equity")
|
|
a2 = Asset(symbol="AAPL_OPT", asset_class="option")
|
|
|
|
sd1 = StrategyDelta(delta_positions={a1.symbol: 10}, notes="pos1")
|
|
sd2 = StrategyDelta(delta_positions={a2.symbol: -5}, notes="pos2")
|
|
|
|
plan = PlanDelta(deltas=[sd1, sd2])
|
|
coord = ADMMCoordinator(max_iterations=2)
|
|
merged = coord.reconcile(plan)
|
|
|
|
# Expect a single consolidated delta in plan.deltas
|
|
assert len(merged.deltas) == 1
|
|
consolidated = merged.deltas[0]
|
|
assert consolidated.delta_positions.get(a1.symbol) == 10
|
|
assert consolidated.delta_positions.get(a2.symbol) == -5
|
|
|
|
|
|
def test_execution_engine_routing_and_backtester_cycle():
|
|
now = time.time()
|
|
# Build simple signals via adapters
|
|
signals = [MarketSignal(asset=equity.build_asset("AAPL"), price=150.0, timestamp=now)]
|
|
signals += [MarketSignal(asset=options.build_asset("AAPL_OPT"), price=5.5, timestamp=now)]
|
|
|
|
a1 = Asset(symbol="AAPL", asset_class="equity")
|
|
a2 = Asset(symbol="AAPL_OPT", asset_class="option")
|
|
sd1 = StrategyDelta(delta_positions={a1.symbol: 2}, notes="to_buy")
|
|
sd2 = StrategyDelta(delta_positions={a2.symbol: -1}, notes="to_sell")
|
|
plan = PlanDelta(deltas=[sd1, sd2])
|
|
|
|
eng = ExecutionEngine()
|
|
routes = eng.route(plan, signals)
|
|
assert isinstance(routes, list)
|
|
assert any("route_delta_to" in r for r in routes)
|
|
|
|
backtester = Backtester(seed=42)
|
|
pnl = backtester.replay(signals, plan)
|
|
# Naive expectation: positive since some deltas are positive with price > 0
|
|
assert isinstance(pnl, float)
|
|
|