diff --git a/app/src/trading/execution/position_protection.py b/app/src/trading/execution/position_protection.py index ee9b62a..9d75852 100644 --- a/app/src/trading/execution/position_protection.py +++ b/app/src/trading/execution/position_protection.py @@ -10,6 +10,15 @@ from src.core.numbers import safe_float from src.core.types import JsonDict, NumericLike from src.trading.auto.state import AutoTradeState 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.pricing import ExecutionPrice from src.trading.journal.service import JournalService @@ -411,9 +420,7 @@ class ExecutionPositionProtectionMixin(_ExecutionPositionProtectionProtocol): "reason": reason, # ---------- Runtime ---------- - "status": state.status, - "strategy": state.strategy, - "cycle_number": state.cycle_number, + **build_runtime_payload(state), # ---------- Position ---------- "symbol": state.symbol, @@ -436,114 +443,25 @@ class ExecutionPositionProtectionMixin(_ExecutionPositionProtectionProtocol): "hold_seconds": metrics.hold_seconds, # ---------- Runtime protection ---------- - "position_protection_status": state.position_protection_status, - "position_protection_reason": state.position_protection_reason, - "runtime_protection_action": state.runtime_protection_action, - "runtime_protection_reason": state.runtime_protection_reason, + **build_runtime_protection_payload(state), "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": self._protection_thresholds(state), # ---------- Position Intelligence ---------- - "position_health_status": state.position_health_status, - "position_health_score": state.position_health_score, - "position_health_reason": state.position_health_reason, + **build_position_health_payload(state), + "position_pressure": state.position_pressure, + "position_exit_pressure": state.position_exit_pressure, - "position_exit_signal": state.position_exit_signal, - "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, + **build_full_position_intelligence_payload(state), # ---------- Execution ---------- - "execution_quality": state.execution_quality, - "execution_quality_reason": state.execution_quality_reason, + **build_execution_quality_payload(state), + **build_execution_price_payload(state), - "execution_confidence_score": state.execution_confidence_score, - "execution_confidence_level": state.execution_confidence_level, - - "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, + # ---------- Market Context ---------- + **build_market_context_payload(state), } def _log_runtime_protection_event(