feat: add market data architecture and complete migration through build 039
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import math
|
||||
from datetime import datetime
|
||||
|
||||
from src.core.types import NumericLike
|
||||
from src.integrations.exchange.service import ExchangeService
|
||||
from src.trading.debug.state import DebugPositionState, DebugTradeState
|
||||
from src.trading.execution.models import ExecutionDecision
|
||||
@@ -389,49 +390,88 @@ class DebugExecutionEngine:
|
||||
return self._market_last_price(state.symbol)
|
||||
|
||||
def _entry_price_for_side(self, symbol: str, side: str) -> float:
|
||||
snapshot = ExchangeService().get_fresh_market_snapshot(symbol)
|
||||
snapshot = ExchangeService().get_execution_snapshot(
|
||||
symbol,
|
||||
runtime_key="debug_auto",
|
||||
)
|
||||
|
||||
if side == "LONG":
|
||||
return self._snapshot_price(snapshot, "ask_price", "last_price")
|
||||
return self._execution_price(
|
||||
snapshot.ask_price,
|
||||
snapshot.last_price,
|
||||
price_name="ask_price",
|
||||
)
|
||||
|
||||
if side == "SHORT":
|
||||
return self._snapshot_price(snapshot, "bid_price", "last_price")
|
||||
return self._execution_price(
|
||||
snapshot.bid_price,
|
||||
snapshot.last_price,
|
||||
price_name="bid_price",
|
||||
)
|
||||
|
||||
return self._snapshot_price(snapshot, "last_price")
|
||||
return self._execution_price(
|
||||
snapshot.last_price,
|
||||
price_name="last_price",
|
||||
)
|
||||
|
||||
def _exit_price_for_side(self, symbol: str, side: str) -> float:
|
||||
snapshot = ExchangeService().get_fresh_market_snapshot(symbol)
|
||||
snapshot = ExchangeService().get_execution_snapshot(
|
||||
symbol,
|
||||
runtime_key="debug_auto",
|
||||
)
|
||||
|
||||
if side == "LONG":
|
||||
return self._snapshot_price(snapshot, "bid_price", "last_price")
|
||||
return self._execution_price(
|
||||
snapshot.bid_price,
|
||||
snapshot.last_price,
|
||||
price_name="bid_price",
|
||||
)
|
||||
|
||||
if side == "SHORT":
|
||||
return self._snapshot_price(snapshot, "ask_price", "last_price")
|
||||
return self._execution_price(
|
||||
snapshot.ask_price,
|
||||
snapshot.last_price,
|
||||
price_name="ask_price",
|
||||
)
|
||||
|
||||
return self._snapshot_price(snapshot, "last_price")
|
||||
return self._execution_price(
|
||||
snapshot.last_price,
|
||||
price_name="last_price",
|
||||
)
|
||||
|
||||
def _market_last_price(self, symbol: str) -> float:
|
||||
snapshot = ExchangeService().get_fresh_market_snapshot(symbol)
|
||||
return self._snapshot_price(snapshot, "last_price")
|
||||
snapshot = ExchangeService().get_execution_snapshot(
|
||||
symbol,
|
||||
runtime_key="debug_auto",
|
||||
)
|
||||
return self._execution_price(
|
||||
snapshot.last_price,
|
||||
price_name="last_price",
|
||||
)
|
||||
|
||||
def _snapshot_price(
|
||||
def _execution_price(
|
||||
self,
|
||||
snapshot: dict[str, object],
|
||||
primary_key: str,
|
||||
fallback_key: str | None = None,
|
||||
raw_price: NumericLike | None,
|
||||
fallback_price: NumericLike | None = None,
|
||||
*,
|
||||
price_name: str,
|
||||
) -> float:
|
||||
raw_price = snapshot.get(primary_key)
|
||||
value = raw_price
|
||||
|
||||
if raw_price is None and fallback_key is not None:
|
||||
raw_price = snapshot.get(fallback_key)
|
||||
if value is None:
|
||||
value = fallback_price
|
||||
|
||||
if raw_price is None:
|
||||
raise ValueError(f"Market snapshot price '{primary_key}' is missing.")
|
||||
if value is None:
|
||||
raise ValueError(
|
||||
f"Execution price '{price_name}' is missing."
|
||||
)
|
||||
|
||||
price = float(raw_price)
|
||||
price = float(value)
|
||||
|
||||
if price <= 0:
|
||||
raise ValueError(f"Market snapshot price '{primary_key}' is invalid: {price}")
|
||||
raise ValueError(
|
||||
f"Execution price '{price_name}' is invalid: {price}"
|
||||
)
|
||||
|
||||
return price
|
||||
|
||||
|
||||
Reference in New Issue
Block a user