07.4.3.16 — Production Execution Pricing Layer
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
|
||||
from src.core.event_bus import EventBus
|
||||
@@ -13,6 +14,15 @@ from src.trading.journal.service import JournalService
|
||||
from src.trading.position.state import PositionState
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class _ExecutionPrice:
|
||||
price: float
|
||||
source: str
|
||||
age_seconds: float | None
|
||||
updated_at: str
|
||||
pricing_role: str
|
||||
|
||||
|
||||
class ExecutionEngine:
|
||||
_position = PositionState()
|
||||
_size_precision = 5
|
||||
@@ -60,7 +70,8 @@ class ExecutionEngine:
|
||||
return ExecutionDecision("NONE", False, "Позиция уже открыта.")
|
||||
|
||||
try:
|
||||
entry_price = self._entry_price_for_side(state.symbol, side)
|
||||
entry = self._entry_price_for_side(state.symbol, side)
|
||||
entry_price = entry.price
|
||||
except Exception as exc:
|
||||
return ExecutionDecision("NONE", False, f"Не удалось получить цену для paper execution: {exc}")
|
||||
|
||||
@@ -116,6 +127,10 @@ class ExecutionEngine:
|
||||
"reason": state.last_signal_reason,
|
||||
"opened_at": now,
|
||||
"pricing": "ask_for_long_bid_for_short",
|
||||
"pricing_role": entry.pricing_role,
|
||||
"price_source": entry.source,
|
||||
"price_age_seconds": entry.age_seconds,
|
||||
"price_updated_at": entry.updated_at,
|
||||
}
|
||||
|
||||
JournalService().log_ui_info(
|
||||
@@ -142,8 +157,10 @@ class ExecutionEngine:
|
||||
return ExecutionDecision("NONE", False, "Нет направления для flip.")
|
||||
|
||||
try:
|
||||
exit_price = self._exit_price_for_side(position.symbol or state.symbol, position.side)
|
||||
new_entry_price = self._entry_price_for_side(state.symbol, new_side)
|
||||
exit_execution = self._exit_price_for_side(position.symbol or state.symbol, position.side)
|
||||
entry_execution = self._entry_price_for_side(state.symbol, new_side)
|
||||
exit_price = exit_execution.price
|
||||
new_entry_price = entry_execution.price
|
||||
except Exception as exc:
|
||||
return ExecutionDecision("NONE", False, f"Ошибка получения цены для flip: {exc}")
|
||||
|
||||
@@ -218,6 +235,14 @@ class ExecutionEngine:
|
||||
"closed_at": now,
|
||||
"new_opened_at": now,
|
||||
"pricing": "exit_by_side_then_entry_by_side",
|
||||
"exit_pricing_role": exit_execution.pricing_role,
|
||||
"exit_price_source": exit_execution.source,
|
||||
"exit_price_age_seconds": exit_execution.age_seconds,
|
||||
"exit_price_updated_at": exit_execution.updated_at,
|
||||
"entry_pricing_role": entry_execution.pricing_role,
|
||||
"entry_price_source": entry_execution.source,
|
||||
"entry_price_age_seconds": entry_execution.age_seconds,
|
||||
"entry_price_updated_at": entry_execution.updated_at,
|
||||
}
|
||||
|
||||
JournalService().log_ui_info(
|
||||
@@ -243,6 +268,7 @@ class ExecutionEngine:
|
||||
forced_reason: str | None = None,
|
||||
forced_exit_price: float | None = None,
|
||||
forced_pnl: float | None = None,
|
||||
forced_price_meta: _ExecutionPrice | None = None,
|
||||
) -> ExecutionDecision:
|
||||
position = type(self)._position
|
||||
|
||||
@@ -252,9 +278,11 @@ class ExecutionEngine:
|
||||
|
||||
if forced_exit_price is not None:
|
||||
exit_price = forced_exit_price
|
||||
exit_execution = forced_price_meta
|
||||
else:
|
||||
try:
|
||||
exit_price = self._exit_price_for_side(position.symbol or state.symbol, position.side)
|
||||
exit_execution = self._exit_price_for_side(position.symbol or state.symbol, position.side)
|
||||
exit_price = exit_execution.price
|
||||
except Exception as exc:
|
||||
return ExecutionDecision("NONE", False, f"Ошибка получения цены для закрытия: {exc}")
|
||||
|
||||
@@ -283,6 +311,10 @@ class ExecutionEngine:
|
||||
"opened_at": position.opened_at,
|
||||
"closed_at": now,
|
||||
"pricing": "bid_for_long_exit_ask_for_short_exit",
|
||||
"pricing_role": exit_execution.pricing_role if exit_execution else None,
|
||||
"price_source": exit_execution.source if exit_execution else None,
|
||||
"price_age_seconds": exit_execution.age_seconds if exit_execution else None,
|
||||
"price_updated_at": exit_execution.updated_at if exit_execution else None,
|
||||
}
|
||||
|
||||
JournalService().log_ui_info(
|
||||
@@ -318,7 +350,8 @@ class ExecutionEngine:
|
||||
return None
|
||||
|
||||
try:
|
||||
current_price = self._exit_price_for_side(position.symbol or state.symbol, position.side)
|
||||
current_execution = self._exit_price_for_side(position.symbol or state.symbol, position.side)
|
||||
current_price = current_execution.price
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
@@ -331,6 +364,7 @@ class ExecutionEngine:
|
||||
forced_reason="MAX_LOSS",
|
||||
forced_exit_price=current_price,
|
||||
forced_pnl=unrealized_pnl,
|
||||
forced_price_meta=current_execution,
|
||||
)
|
||||
|
||||
if self._is_stop_loss_hit(state, price_move_percent):
|
||||
@@ -339,6 +373,7 @@ class ExecutionEngine:
|
||||
forced_reason="STOP_LOSS",
|
||||
forced_exit_price=current_price,
|
||||
forced_pnl=unrealized_pnl,
|
||||
forced_price_meta=current_execution,
|
||||
)
|
||||
|
||||
if self._is_take_profit_hit(state, price_move_percent):
|
||||
@@ -347,6 +382,7 @@ class ExecutionEngine:
|
||||
forced_reason="TAKE_PROFIT",
|
||||
forced_exit_price=current_price,
|
||||
forced_pnl=unrealized_pnl,
|
||||
forced_price_meta=current_execution,
|
||||
)
|
||||
|
||||
return None
|
||||
@@ -412,7 +448,8 @@ class ExecutionEngine:
|
||||
return
|
||||
|
||||
try:
|
||||
current_price = self._exit_price_for_side(position.symbol or state.symbol, position.side)
|
||||
current_execution = self._exit_price_for_side(position.symbol or state.symbol, position.side)
|
||||
current_price = current_execution.price
|
||||
except Exception:
|
||||
self._sync_state_from_position(state)
|
||||
return
|
||||
@@ -438,7 +475,7 @@ class ExecutionEngine:
|
||||
|
||||
if price is None:
|
||||
try:
|
||||
price = self._signal_entry_price(state)
|
||||
price = self._signal_entry_price(state).price
|
||||
except Exception:
|
||||
return 0.0
|
||||
|
||||
@@ -487,7 +524,7 @@ class ExecutionEngine:
|
||||
state.execution_size_adjustment_reason = "MARGIN_LIMIT"
|
||||
return self._round_size(max_size)
|
||||
|
||||
def _signal_entry_price(self, state: AutoTradeState) -> float:
|
||||
def _signal_entry_price(self, state: AutoTradeState) -> _ExecutionPrice:
|
||||
if state.last_signal == "BUY":
|
||||
return self._entry_price_for_side(state.symbol, "LONG")
|
||||
|
||||
@@ -496,50 +533,83 @@ class ExecutionEngine:
|
||||
|
||||
return self._market_last_price(state.symbol)
|
||||
|
||||
def _entry_price_for_side(self, symbol: str, side: str) -> float:
|
||||
snapshot = ExchangeService().get_market_snapshot(symbol)
|
||||
def _entry_price_for_side(self, symbol: str, side: str) -> _ExecutionPrice:
|
||||
snapshot = ExchangeService().get_execution_snapshot(symbol)
|
||||
|
||||
if side == "LONG":
|
||||
return self._snapshot_price(snapshot, "ask_price", "last_price")
|
||||
return _ExecutionPrice(
|
||||
price=self._snapshot_price(snapshot.ask_price, "ask_price"),
|
||||
source=snapshot.source,
|
||||
age_seconds=snapshot.age_seconds,
|
||||
updated_at=snapshot.updated_at,
|
||||
pricing_role="LONG_ENTRY_ASK",
|
||||
)
|
||||
|
||||
if side == "SHORT":
|
||||
return self._snapshot_price(snapshot, "bid_price", "last_price")
|
||||
return _ExecutionPrice(
|
||||
price=self._snapshot_price(snapshot.bid_price, "bid_price"),
|
||||
source=snapshot.source,
|
||||
age_seconds=snapshot.age_seconds,
|
||||
updated_at=snapshot.updated_at,
|
||||
pricing_role="SHORT_ENTRY_BID",
|
||||
)
|
||||
|
||||
return self._snapshot_price(snapshot, "last_price")
|
||||
return _ExecutionPrice(
|
||||
price=self._snapshot_price(snapshot.last_price, "last_price"),
|
||||
source=snapshot.source,
|
||||
age_seconds=snapshot.age_seconds,
|
||||
updated_at=snapshot.updated_at,
|
||||
pricing_role="ENTRY_LAST",
|
||||
)
|
||||
|
||||
def _exit_price_for_side(self, symbol: str, side: str) -> float:
|
||||
snapshot = ExchangeService().get_market_snapshot(symbol)
|
||||
def _exit_price_for_side(self, symbol: str, side: str) -> _ExecutionPrice:
|
||||
snapshot = ExchangeService().get_execution_snapshot(symbol)
|
||||
|
||||
if side == "LONG":
|
||||
return self._snapshot_price(snapshot, "bid_price", "last_price")
|
||||
return _ExecutionPrice(
|
||||
price=self._snapshot_price(snapshot.bid_price, "bid_price"),
|
||||
source=snapshot.source,
|
||||
age_seconds=snapshot.age_seconds,
|
||||
updated_at=snapshot.updated_at,
|
||||
pricing_role="LONG_EXIT_BID",
|
||||
)
|
||||
|
||||
if side == "SHORT":
|
||||
return self._snapshot_price(snapshot, "ask_price", "last_price")
|
||||
return _ExecutionPrice(
|
||||
price=self._snapshot_price(snapshot.ask_price, "ask_price"),
|
||||
source=snapshot.source,
|
||||
age_seconds=snapshot.age_seconds,
|
||||
updated_at=snapshot.updated_at,
|
||||
pricing_role="SHORT_EXIT_ASK",
|
||||
)
|
||||
|
||||
return self._snapshot_price(snapshot, "last_price")
|
||||
return _ExecutionPrice(
|
||||
price=self._snapshot_price(snapshot.last_price, "last_price"),
|
||||
source=snapshot.source,
|
||||
age_seconds=snapshot.age_seconds,
|
||||
updated_at=snapshot.updated_at,
|
||||
pricing_role="EXIT_LAST",
|
||||
)
|
||||
|
||||
def _market_last_price(self, symbol: str) -> float:
|
||||
snapshot = ExchangeService().get_market_snapshot(symbol)
|
||||
return self._snapshot_price(snapshot, "last_price")
|
||||
def _market_last_price(self, symbol: str) -> _ExecutionPrice:
|
||||
snapshot = ExchangeService().get_execution_snapshot(symbol)
|
||||
|
||||
def _snapshot_price(
|
||||
self,
|
||||
snapshot: dict[str, object],
|
||||
primary_key: str,
|
||||
fallback_key: str | None = None,
|
||||
) -> float:
|
||||
raw_price = snapshot.get(primary_key)
|
||||
|
||||
if raw_price is None and fallback_key is not None:
|
||||
raw_price = snapshot.get(fallback_key)
|
||||
return _ExecutionPrice(
|
||||
price=self._snapshot_price(snapshot.last_price, "last_price"),
|
||||
source=snapshot.source,
|
||||
age_seconds=snapshot.age_seconds,
|
||||
updated_at=snapshot.updated_at,
|
||||
pricing_role="MARKET_LAST",
|
||||
)
|
||||
|
||||
def _snapshot_price(self, raw_price: object, name: str) -> float:
|
||||
if raw_price is None:
|
||||
raise ValueError(f"Market snapshot price '{primary_key}' is missing.")
|
||||
raise ValueError(f"Execution snapshot price '{name}' is missing.")
|
||||
|
||||
price = float(raw_price)
|
||||
|
||||
if price <= 0:
|
||||
raise ValueError(f"Market snapshot price '{primary_key}' is invalid: {price}")
|
||||
raise ValueError(f"Execution snapshot price '{name}' is invalid: {price}")
|
||||
|
||||
return price
|
||||
|
||||
|
||||
Reference in New Issue
Block a user