07.4.4.1.9.4 Execution Semantic Layer
This commit is contained in:
@@ -139,6 +139,15 @@ def _build_stopped_without_position_text(state) -> str:
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
def _execution_semantic_line(state) -> str:
|
||||
message = getattr(state, "execution_semantic_message", None)
|
||||
|
||||
if not message:
|
||||
return ""
|
||||
|
||||
return str(message)
|
||||
|
||||
|
||||
def _build_waiting_text(state) -> str:
|
||||
price = _signal_entry_price(state)
|
||||
|
||||
@@ -157,9 +166,8 @@ def _build_waiting_text(state) -> str:
|
||||
_signal_confirmation_line(state),
|
||||
_market_semantic_line(state),
|
||||
_entry_block_line(state),
|
||||
_execution_quality_line(state),
|
||||
_execution_semantic_line(state),
|
||||
*_signal_confidence_lines(state),
|
||||
*_execution_block_lines(state),
|
||||
]
|
||||
|
||||
signal_lines = [line for line in signal_lines if line]
|
||||
@@ -213,8 +221,7 @@ def _build_active_position_text(state) -> str:
|
||||
|
||||
market_lines = [
|
||||
_market_semantic_line(state),
|
||||
_execution_quality_line(state),
|
||||
*_execution_block_lines(state),
|
||||
_execution_semantic_line(state),
|
||||
]
|
||||
market_lines = [line for line in market_lines if line]
|
||||
|
||||
@@ -360,64 +367,6 @@ def _entry_block_line(state) -> str:
|
||||
return f"🧩 Фильтр · {compact_message}"
|
||||
|
||||
|
||||
def _execution_quality_line(state) -> str:
|
||||
quality = getattr(state, "execution_quality", None)
|
||||
reason = getattr(state, "execution_quality_reason", None)
|
||||
spread_percent = getattr(state, "spread_percent", None)
|
||||
age_seconds = getattr(state, "snapshot_age_seconds", None)
|
||||
|
||||
if not quality:
|
||||
return ""
|
||||
|
||||
if quality == "GOOD":
|
||||
return ""
|
||||
|
||||
if reason == "WIDE_SPREAD" and spread_percent is not None:
|
||||
return f"⚠️ Вход · spread {_format_percent(spread_percent)}"
|
||||
|
||||
if reason == "AGING_SNAPSHOT" and age_seconds is not None:
|
||||
return f"⚠️ Вход · данные стареют {age_seconds:.1f}с"
|
||||
|
||||
if reason == "STALE_SNAPSHOT":
|
||||
return "⛔ Вход · рынок неактуален"
|
||||
|
||||
if reason == "HIGH_SPREAD" and spread_percent is not None:
|
||||
return f"⛔ Вход · высокий spread {_format_percent(spread_percent)}"
|
||||
|
||||
if reason == "SNAPSHOT_UNAVAILABLE":
|
||||
return "⚠️ Вход · нет стакана"
|
||||
|
||||
if reason == "SNAPSHOT_ERROR":
|
||||
return "⛔ Вход · нет данных рынка"
|
||||
|
||||
message = getattr(state, "execution_quality_message", None)
|
||||
|
||||
if not message:
|
||||
return ""
|
||||
|
||||
return f"⚠️ Вход · {message}"
|
||||
|
||||
|
||||
def _execution_block_lines(state) -> list[str]:
|
||||
lines: list[str] = []
|
||||
|
||||
reason = getattr(state, "execution_block_reason", None)
|
||||
if reason and reason not in {
|
||||
"высокий spread",
|
||||
"spread повышен",
|
||||
"snapshot устарел",
|
||||
"рынок неактуален",
|
||||
}:
|
||||
lines.append(f"Вход · {reason}")
|
||||
|
||||
adjustment = getattr(state, "execution_size_adjustment_reason", None)
|
||||
|
||||
if adjustment == "MARGIN_LIMIT":
|
||||
lines.append("Позиция ограничена настройкой Max Reserved.")
|
||||
|
||||
return lines
|
||||
|
||||
|
||||
def _allocated_balance(state) -> float:
|
||||
return float(getattr(state, "allocated_balance_usd", 1000.0) or 1000.0)
|
||||
|
||||
@@ -608,8 +557,7 @@ def _risk_loss_text(
|
||||
return ""
|
||||
|
||||
if size is None or size <= 0 or entry_price is None or entry_price <= 0:
|
||||
loss = _target_loss_by_percent_stub(percent)
|
||||
return f"-{_format_money_compact(loss)}" if loss is not None else ""
|
||||
return ""
|
||||
|
||||
move = entry_price * (percent / 100)
|
||||
loss = move * size
|
||||
@@ -635,13 +583,6 @@ def _risk_profit_text(
|
||||
return f"+{_format_money_compact(profit)}"
|
||||
|
||||
|
||||
def _target_loss_by_percent_stub(percent: float | None) -> float | None:
|
||||
if percent is None:
|
||||
return None
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _risk_reward_line(state) -> str:
|
||||
if (
|
||||
state.stop_loss_percent is None
|
||||
@@ -777,16 +718,6 @@ def _status_text(status: str) -> str:
|
||||
return mapping.get(status, status)
|
||||
|
||||
|
||||
def _decision_human_text(status: str) -> str:
|
||||
mapping = {
|
||||
"WAITING": "Ожидание сигнала",
|
||||
"CONFIRMING": "Подтверждение сигнала",
|
||||
"READY": "Сигнал готов",
|
||||
"BLOCKED": "Сигнал заблокирован",
|
||||
}
|
||||
return mapping.get(status, status)
|
||||
|
||||
|
||||
def _account_mode_line() -> str:
|
||||
return "DEMO аккаунт" if "DEMO" in mode_line().upper() else "LIVE аккаунт"
|
||||
|
||||
@@ -911,29 +842,6 @@ def _format_plain_or_dash(value: float | int | None) -> str:
|
||||
return _format_money_compact(value)
|
||||
|
||||
|
||||
def _format_usd_or_dash(value: float | None) -> str:
|
||||
return _format_plain_or_dash(value)
|
||||
|
||||
|
||||
def _format_signed_usd(value: float | int | None) -> str:
|
||||
if value is None:
|
||||
return "—"
|
||||
|
||||
amount = float(value)
|
||||
|
||||
if amount > 0:
|
||||
return f"+{_format_money_compact(amount)}"
|
||||
|
||||
if amount < 0:
|
||||
return f"−{_format_money_compact(abs(amount))}"
|
||||
|
||||
return "0"
|
||||
|
||||
|
||||
def _format_signed_usd_with_direction(value: float | int | None) -> str:
|
||||
return _format_signed_plain_with_direction(value)
|
||||
|
||||
|
||||
def _format_signed_plain_with_direction(value: float | int | None) -> str:
|
||||
if value is None:
|
||||
return "—"
|
||||
|
||||
@@ -373,6 +373,9 @@ class AutoTradeService:
|
||||
state.signal_confirmation_progress = 0.0
|
||||
state.signal_confirmation_reason = None
|
||||
state.execution_block_reason = None
|
||||
state.execution_semantic_status = None
|
||||
state.execution_semantic_message = None
|
||||
state.execution_semantic_reason = None
|
||||
state.signal_started_at = None
|
||||
state.signal_updated_at = None
|
||||
state.market_state = None
|
||||
@@ -381,6 +384,11 @@ class AutoTradeService:
|
||||
state.market_analysis_interval = None
|
||||
state.market_analysis_reason = None
|
||||
state.market_analysis_updated_at = None
|
||||
state.market_runtime_degraded = False
|
||||
state.market_trend_strength = None
|
||||
state.market_trend_quality = None
|
||||
state.market_phase = None
|
||||
state.market_phase_direction = None
|
||||
state.entry_block_reason = None
|
||||
state.entry_block_message = None
|
||||
state.runtime_expired_reason = None
|
||||
@@ -390,10 +398,6 @@ class AutoTradeService:
|
||||
state.execution_quality = None
|
||||
state.execution_quality_reason = None
|
||||
state.execution_quality_message = None
|
||||
state.market_runtime_degraded = False
|
||||
state.market_trend_strength = None
|
||||
state.market_trend_quality = None
|
||||
state.market_phase = None
|
||||
|
||||
# собрать контекст для стратегии
|
||||
def _build_strategy_context(self) -> StrategyContext:
|
||||
@@ -809,6 +813,7 @@ class AutoTradeService:
|
||||
state.market_trend_strength = payload.get("market_trend_strength")
|
||||
state.market_trend_quality = payload.get("market_trend_quality")
|
||||
state.market_phase = payload.get("market_phase")
|
||||
state.market_phase_direction = payload.get("market_phase_direction")
|
||||
state.market_analysis_interval = payload.get("market_analysis_interval")
|
||||
state.market_analysis_reason = payload.get("market_analysis_reason")
|
||||
state.market_analysis_updated_at = time.monotonic()
|
||||
@@ -1266,6 +1271,58 @@ class AutoTradeService:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _sync_execution_semantic_state(self, state: AutoTradeState) -> None:
|
||||
if state.execution_quality == "BLOCKED":
|
||||
state.execution_semantic_status = "BLOCKED"
|
||||
state.execution_semantic_message = self._execution_block_semantic_message(state)
|
||||
state.execution_semantic_reason = state.execution_quality_reason
|
||||
return
|
||||
|
||||
if state.position_side != "NONE":
|
||||
state.execution_semantic_status = "POSITION_OPEN"
|
||||
state.execution_semantic_message = "📌 Исполнение · позиция открыта"
|
||||
state.execution_semantic_reason = state.last_execution_reason
|
||||
return
|
||||
|
||||
if state.decision_status == "READY" and state.is_signal_ready:
|
||||
state.execution_semantic_status = "READY"
|
||||
state.execution_semantic_message = "✅ Исполнение · готово"
|
||||
state.execution_semantic_reason = state.decision_reason
|
||||
return
|
||||
|
||||
if state.decision_status == "CONFIRMING":
|
||||
state.execution_semantic_status = "WAITING_SIGNAL"
|
||||
state.execution_semantic_message = "⏳ Исполнение · ждёт подтверждения"
|
||||
state.execution_semantic_reason = state.decision_reason
|
||||
return
|
||||
|
||||
if state.last_signal in {"BUY", "SELL"}:
|
||||
state.execution_semantic_status = "WAITING_SIGNAL"
|
||||
state.execution_semantic_message = "⏳ Исполнение · сигнал проверяется"
|
||||
state.execution_semantic_reason = state.decision_reason
|
||||
return
|
||||
|
||||
state.execution_semantic_status = "IDLE"
|
||||
state.execution_semantic_message = ""
|
||||
state.execution_semantic_reason = state.decision_reason
|
||||
|
||||
def _execution_block_semantic_message(self, state: AutoTradeState) -> str:
|
||||
reason = state.execution_quality_reason
|
||||
|
||||
if reason == "STALE_SNAPSHOT":
|
||||
return "⛔ Исполнение · рынок неактуален"
|
||||
|
||||
if reason == "HIGH_SPREAD":
|
||||
return "⛔ Исполнение · высокий spread"
|
||||
|
||||
if reason == "SNAPSHOT_ERROR":
|
||||
return "⛔ Исполнение · нет данных рынка"
|
||||
|
||||
if reason == "SNAPSHOT_UNAVAILABLE":
|
||||
return "⚠️ Исполнение · нет стакана"
|
||||
|
||||
return "⛔ Исполнение · заблокировано"
|
||||
|
||||
def run_cycle(self) -> AutoTradeState:
|
||||
state = self.get_state()
|
||||
|
||||
@@ -1299,4 +1356,6 @@ class AutoTradeService:
|
||||
if state.execution_quality != "BLOCKED":
|
||||
ExecutionEngine().process(state)
|
||||
|
||||
self._sync_execution_semantic_state(state)
|
||||
|
||||
return state
|
||||
@@ -124,6 +124,9 @@ class AutoTradeState:
|
||||
# фаза рынка: IMPULSE / PULLBACK / RANGE / SQUEEZE / UNKNOWN
|
||||
market_phase: str | None = None
|
||||
|
||||
# направление короткой фазы рынка: UP / DOWN / FLAT / UNKNOWN
|
||||
market_phase_direction: str | None = None
|
||||
|
||||
# таймфрейм анализа рынка
|
||||
market_analysis_interval: str | None = None
|
||||
|
||||
@@ -179,4 +182,14 @@ class AutoTradeState:
|
||||
signal_confirmation_progress: float = 0.0
|
||||
|
||||
# человекочитаемая причина текущего confirmation status
|
||||
signal_confirmation_reason: str | None = None
|
||||
signal_confirmation_reason: str | None = None
|
||||
|
||||
# semantic-статус execution слоя:
|
||||
# IDLE / WAITING_SIGNAL / READY / BLOCKED / EXECUTED / POSITION_OPEN / PROTECTED
|
||||
execution_semantic_status: str | None = None
|
||||
|
||||
# короткая строка для UI
|
||||
execution_semantic_message: str | None = None
|
||||
|
||||
# техническая детализация для логов / отладки
|
||||
execution_semantic_reason: str | None = None
|
||||
2
app/src/trading/execution/quality.py
Normal file
2
app/src/trading/execution/quality.py
Normal file
@@ -0,0 +1,2 @@
|
||||
# app/src/trading/execution/quality.py
|
||||
|
||||
Reference in New Issue
Block a user