refactor(execution): reuse payload helpers in runtime protection

This commit is contained in:
2026-07-03 10:40:52 +03:00
parent 610e6c3043
commit ce90e58060

View File

@@ -10,6 +10,15 @@ from src.core.numbers import safe_float
from src.core.types import JsonDict, NumericLike from src.core.types import JsonDict, NumericLike
from src.trading.auto.state import AutoTradeState from src.trading.auto.state import AutoTradeState
from src.trading.execution.models import ExecutionDecision from src.trading.execution.models import ExecutionDecision
from src.trading.execution.payloads import (
build_execution_price_payload,
build_execution_quality_payload,
build_full_position_intelligence_payload,
build_market_context_payload,
build_position_health_payload,
build_runtime_payload,
build_runtime_protection_payload,
)
from src.trading.execution.position_metrics import PositionMetrics, build_position_metrics from src.trading.execution.position_metrics import PositionMetrics, build_position_metrics
from src.trading.execution.pricing import ExecutionPrice from src.trading.execution.pricing import ExecutionPrice
from src.trading.journal.service import JournalService from src.trading.journal.service import JournalService
@@ -411,9 +420,7 @@ class ExecutionPositionProtectionMixin(_ExecutionPositionProtectionProtocol):
"reason": reason, "reason": reason,
# ---------- Runtime ---------- # ---------- Runtime ----------
"status": state.status, **build_runtime_payload(state),
"strategy": state.strategy,
"cycle_number": state.cycle_number,
# ---------- Position ---------- # ---------- Position ----------
"symbol": state.symbol, "symbol": state.symbol,
@@ -436,114 +443,25 @@ class ExecutionPositionProtectionMixin(_ExecutionPositionProtectionProtocol):
"hold_seconds": metrics.hold_seconds, "hold_seconds": metrics.hold_seconds,
# ---------- Runtime protection ---------- # ---------- Runtime protection ----------
"position_protection_status": state.position_protection_status, **build_runtime_protection_payload(state),
"position_protection_reason": state.position_protection_reason,
"runtime_protection_action": state.runtime_protection_action,
"runtime_protection_reason": state.runtime_protection_reason,
"runtime_protection_updated_at": state.runtime_protection_updated_at, "runtime_protection_updated_at": state.runtime_protection_updated_at,
"break_even_armed": state.break_even_armed,
"break_even_price": state.break_even_price,
"profit_lock_active": state.profit_lock_active,
"profit_lock_price": state.profit_lock_price,
"trailing_stop_active": state.trailing_stop_active,
"trailing_stop_price": state.trailing_stop_price,
# ---------- Protection thresholds ---------- # ---------- Protection thresholds ----------
"protection_thresholds": self._protection_thresholds(state), "protection_thresholds": self._protection_thresholds(state),
# ---------- Position Intelligence ---------- # ---------- Position Intelligence ----------
"position_health_status": state.position_health_status, **build_position_health_payload(state),
"position_health_score": state.position_health_score, "position_pressure": state.position_pressure,
"position_health_reason": state.position_health_reason, "position_exit_pressure": state.position_exit_pressure,
"position_exit_signal": state.position_exit_signal, **build_full_position_intelligence_payload(state),
"position_exit_confidence": state.position_exit_confidence,
"position_exit_urgency": state.position_exit_urgency,
"position_risk_level": state.position_risk_level,
"position_risk_reason": state.position_risk_reason,
"position_trend_alignment": state.position_trend_alignment,
"position_adverse_momentum": state.position_adverse_momentum,
"position_reversal_risk": state.position_reversal_risk,
"position_fatigue_state": state.position_fatigue_state,
"position_giveback_percent": state.position_giveback_percent,
"position_mfe_percent": state.position_mfe_percent,
"position_mae_percent": state.position_mae_percent,
"position_peak_pnl_usd": state.position_peak_pnl_usd,
"position_peak_pnl_percent": state.position_peak_pnl_percent,
# ---------- Execution ---------- # ---------- Execution ----------
"execution_quality": state.execution_quality, **build_execution_quality_payload(state),
"execution_quality_reason": state.execution_quality_reason, **build_execution_price_payload(state),
"execution_confidence_score": state.execution_confidence_score, # ---------- Market Context ----------
"execution_confidence_level": state.execution_confidence_level, **build_market_context_payload(state),
"spread_percent": state.spread_percent,
"snapshot_age_seconds": state.snapshot_age_seconds,
# ---------- Execution price ----------
"execution_price_source": state.execution_price_source,
"execution_price_age_seconds": state.execution_price_age_seconds,
"execution_bid_price": state.execution_bid_price,
"execution_ask_price": state.execution_ask_price,
"execution_last_price": state.execution_last_price,
# ---------- Market Score ----------
"market_score": state.market_score,
"market_score_label": state.market_score_label,
"market_long_score": state.market_long_score,
"market_short_score": state.market_short_score,
# ---------- Market ----------
"market_state": state.market_state,
"market_trend": state.market_trend,
"market_trend_strength": state.market_trend_strength,
"market_trend_quality": state.market_trend_quality,
"market_phase": state.market_phase,
"market_phase_direction": state.market_phase_direction,
# ---------- Candle ----------
"last_closed_candle_change_percent": state.last_closed_candle_change_percent,
"last_closed_candle_direction": state.last_closed_candle_direction,
"current_interval_change_percent": state.current_interval_change_percent,
"current_interval_direction": state.current_interval_direction,
"current_interval_label": state.current_interval_label,
# ---------- Structure ----------
"market_structure": state.market_structure,
"market_structure_reason": state.market_structure_reason,
# ---------- Momentum ----------
"momentum_state": state.momentum_state,
"momentum_direction": state.momentum_direction,
"momentum_strength": state.momentum_strength,
"momentum_change_percent": state.momentum_change_percent,
"breakout_level": state.breakout_level,
"breakout_distance_percent": state.breakout_distance_percent,
"breakout_reason": state.breakout_reason,
# ---------- HTF ----------
"htf_interval": state.htf_interval,
"htf_atr_percent": state.htf_atr_percent,
"htf_atr_percent_baseline": state.htf_atr_percent_baseline,
"htf_volatility_ratio": state.htf_volatility_ratio,
"htf_volatility": state.htf_volatility,
"htf_market_state": state.htf_market_state,
"htf_trend": state.htf_trend,
"htf_trend_strength": state.htf_trend_strength,
"htf_trend_quality": state.htf_trend_quality,
"htf_market_phase": state.htf_market_phase,
"htf_alignment": state.htf_alignment,
"htf_confirmation_score": state.htf_confirmation_score,
"htf_reason": state.htf_reason,
} }
def _log_runtime_protection_event( def _log_runtime_protection_event(