refactor(execution): reuse shared payload builders in position actions

This commit is contained in:
2026-07-03 09:43:29 +03:00
parent 63bea1831f
commit f50ba047ee

View File

@@ -14,7 +14,20 @@ from src.trading.auto.state_reset import (
reset_execution_block_state, reset_execution_block_state,
) )
from src.trading.execution.models import ExecutionDecision from src.trading.execution.models import ExecutionDecision
from src.trading.execution.payloads import build_market_context_payload from src.trading.execution.payloads import (
build_adaptive_size_payload,
build_decision_payload,
build_execution_price_payload,
build_execution_quality_payload,
build_market_context_payload,
build_position_health_payload,
build_position_intelligence_payload,
build_risk_settings_payload,
build_runtime_blocks_payload,
build_runtime_payload,
build_signal_payload,
build_autonomous_payload,
)
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
from src.trading.position.state import PositionState from src.trading.position.state import PositionState
@@ -129,70 +142,18 @@ class ExecutionPositionActionsMixin(_ExecutionPositionActionsProtocol):
"action": action, "action": action,
"reject_reason": reason, "reject_reason": reason,
# ---------- Runtime ----------
"status": state.status,
"strategy": state.strategy,
"cycle_number": state.cycle_number,
# ---------- Instrument ---------- # ---------- Instrument ----------
"symbol": state.symbol, "symbol": state.symbol,
"side": side, "side": side,
# ---------- Signal ---------- **build_runtime_payload(state),
"signal": state.last_signal, **build_signal_payload(state),
"confidence": state.last_signal_confidence, **build_decision_payload(state),
"repeat_count": state.last_signal_repeat_count, **build_runtime_blocks_payload(state),
"reason": state.last_signal_reason, **build_execution_quality_payload(state),
**build_execution_price_payload(state),
# ---------- Decision ---------- **build_adaptive_size_payload(state),
"decision_status": state.decision_status, **build_risk_settings_payload(state),
"decision_reason": state.decision_reason,
# ---------- Runtime blocks ----------
"entry_block_reason": state.entry_block_reason,
"entry_block_message": state.entry_block_message,
"execution_block_reason": state.execution_block_reason,
"execution_block_title": state.execution_block_title,
"execution_block_message": state.execution_block_message,
"execution_block_action": state.execution_block_action,
"last_flip_block_reason": state.last_flip_block_reason,
# ---------- Execution ----------
"execution_confidence_score": state.execution_confidence_score,
"execution_confidence_level": state.execution_confidence_level,
"execution_confidence_reason": state.execution_confidence_reason,
"execution_quality": state.execution_quality,
"execution_quality_reason": state.execution_quality_reason,
"execution_quality_message": state.execution_quality_message,
"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,
"execution_price_freshness": state.execution_price_freshness,
# ---------- Adaptive size ----------
"adaptive_size_base": state.adaptive_size_base,
"adaptive_size_final": state.adaptive_size_final,
"adaptive_size_multiplier": state.adaptive_size_multiplier,
"adaptive_size_reason": state.adaptive_size_reason,
"adaptive_size_factors": state.adaptive_size_factors,
"effective_risk_percent": state.effective_risk_percent,
"effective_target_risk_usd": state.effective_target_risk_usd,
# ---------- Risk settings ----------
"risk_percent": state.risk_percent,
"stop_loss_percent": state.stop_loss_percent,
"take_profit_percent": state.take_profit_percent,
"max_loss_usd": state.max_loss_usd,
"max_reserved_balance_percent": state.max_reserved_balance_percent,
"allocated_balance_usd": state.allocated_balance_usd,
"leverage": state.leverage,
**build_market_context_payload(state), **build_market_context_payload(state),
} }
@@ -219,11 +180,6 @@ class ExecutionPositionActionsMixin(_ExecutionPositionActionsProtocol):
"execution_type": EXECUTION_TYPE_ENTRY, "execution_type": EXECUTION_TYPE_ENTRY,
"action": action, "action": action,
# ---------- Runtime ----------
"status": state.status,
"strategy": state.strategy,
"cycle_number": state.cycle_number,
# ---------- Position ---------- # ---------- Position ----------
"symbol": state.symbol, "symbol": state.symbol,
"side": side, "side": side,
@@ -234,52 +190,6 @@ class ExecutionPositionActionsMixin(_ExecutionPositionActionsProtocol):
"opened_at": now, "opened_at": now,
"opened_monotonic_at": opened_monotonic_at, "opened_monotonic_at": opened_monotonic_at,
# ---------- Runtime position state ----------
"position_pressure": state.position_pressure,
"position_health_status": state.position_health_status,
"position_health_score": state.position_health_score,
"position_risk_level": state.position_risk_level,
"position_risk_reason": state.position_risk_reason,
# ---------- Signal ----------
"signal": state.last_signal,
"confidence": state.last_signal_confidence,
"repeat_count": state.last_signal_repeat_count,
"reason": state.last_signal_reason,
# ---------- Decision ----------
"decision_status": state.decision_status,
"decision_reason": state.decision_reason,
# ---------- Runtime blocks ----------
"entry_block_reason": state.entry_block_reason,
"entry_block_message": state.entry_block_message,
"execution_block_reason": state.execution_block_reason,
"execution_block_title": state.execution_block_title,
"execution_block_message": state.execution_block_message,
"execution_block_action": state.execution_block_action,
"last_flip_block_reason": state.last_flip_block_reason,
# ---------- Execution ----------
"execution_confidence_score": state.execution_confidence_score,
"execution_confidence_level": state.execution_confidence_level,
"execution_confidence_reason": state.execution_confidence_reason,
"execution_quality": state.execution_quality,
"execution_quality_reason": state.execution_quality_reason,
"execution_quality_message": state.execution_quality_message,
"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,
"execution_price_freshness": state.execution_price_freshness,
# ---------- Pricing ---------- # ---------- Pricing ----------
"pricing": PRICING_ENTRY_MODE, "pricing": PRICING_ENTRY_MODE,
"pricing_role": entry.pricing_role, "pricing_role": entry.pricing_role,
@@ -287,24 +197,15 @@ class ExecutionPositionActionsMixin(_ExecutionPositionActionsProtocol):
"price_age_seconds": entry.age_seconds, "price_age_seconds": entry.age_seconds,
"price_updated_at": entry.updated_at, "price_updated_at": entry.updated_at,
# ---------- Adaptive size ---------- **build_runtime_payload(state),
"adaptive_size_base": state.adaptive_size_base, **build_position_health_payload(state),
"adaptive_size_final": state.adaptive_size_final, **build_signal_payload(state),
"adaptive_size_multiplier": state.adaptive_size_multiplier, **build_decision_payload(state),
"adaptive_size_reason": state.adaptive_size_reason, **build_runtime_blocks_payload(state),
"adaptive_size_factors": state.adaptive_size_factors, **build_execution_quality_payload(state),
**build_execution_price_payload(state),
"effective_risk_percent": state.effective_risk_percent, **build_adaptive_size_payload(state),
"effective_target_risk_usd": state.effective_target_risk_usd, **build_risk_settings_payload(state),
# ---------- Risk settings ----------
"risk_percent": state.risk_percent,
"stop_loss_percent": state.stop_loss_percent,
"take_profit_percent": state.take_profit_percent,
"max_loss_usd": state.max_loss_usd,
"max_reserved_balance_percent": state.max_reserved_balance_percent,
"allocated_balance_usd": state.allocated_balance_usd,
**build_market_context_payload(state), **build_market_context_payload(state),
} }
@@ -339,11 +240,6 @@ class ExecutionPositionActionsMixin(_ExecutionPositionActionsProtocol):
"close_reason": close_reason, "close_reason": close_reason,
"is_forced": forced_reason is not None, "is_forced": forced_reason is not None,
# ---------- Runtime ----------
"status": state.status,
"strategy": state.strategy,
"cycle_number": state.cycle_number,
# ---------- Instrument / Position ---------- # ---------- Instrument / Position ----------
"symbol": state.symbol, "symbol": state.symbol,
"side": position.side, "side": position.side,
@@ -370,45 +266,6 @@ class ExecutionPositionActionsMixin(_ExecutionPositionActionsProtocol):
"hold_seconds": metrics.hold_seconds, "hold_seconds": metrics.hold_seconds,
"overnight_count": metrics.overnight_count, "overnight_count": metrics.overnight_count,
# ---------- Signal ----------
"signal": state.last_signal,
"confidence": state.last_signal_confidence,
"repeat_count": state.last_signal_repeat_count,
"reason": state.last_signal_reason,
# ---------- Decision ----------
"decision_status": state.decision_status,
"decision_reason": state.decision_reason,
# ---------- Runtime blocks ----------
"entry_block_reason": state.entry_block_reason,
"entry_block_message": state.entry_block_message,
"execution_block_reason": state.execution_block_reason,
"execution_block_title": state.execution_block_title,
"execution_block_message": state.execution_block_message,
"execution_block_action": state.execution_block_action,
"last_flip_block_reason": state.last_flip_block_reason,
# ---------- Execution ----------
"execution_quality": state.execution_quality,
"execution_quality_reason": state.execution_quality_reason,
"execution_quality_message": state.execution_quality_message,
"execution_confidence_score": state.execution_confidence_score,
"execution_confidence_level": state.execution_confidence_level,
"execution_confidence_reason": state.execution_confidence_reason,
"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,
"execution_price_freshness": state.execution_price_freshness,
# ---------- Pricing ---------- # ---------- Pricing ----------
"pricing": PRICING_EXIT_MODE, "pricing": PRICING_EXIT_MODE,
"pricing_role": exit_execution.pricing_role if exit_execution else None, "pricing_role": exit_execution.pricing_role if exit_execution else None,
@@ -416,23 +273,6 @@ class ExecutionPositionActionsMixin(_ExecutionPositionActionsProtocol):
"price_age_seconds": exit_execution.age_seconds 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, "price_updated_at": exit_execution.updated_at if exit_execution else None,
# ---------- Adaptive size ----------
"adaptive_size_base": state.adaptive_size_base,
"adaptive_size_final": state.adaptive_size_final,
"adaptive_size_multiplier": state.adaptive_size_multiplier,
"adaptive_size_reason": state.adaptive_size_reason,
"adaptive_size_factors": state.adaptive_size_factors,
"effective_risk_percent": state.effective_risk_percent,
"effective_target_risk_usd": state.effective_target_risk_usd,
# ---------- Risk Settings ----------
"risk_percent": state.risk_percent,
"stop_loss_percent": state.stop_loss_percent,
"take_profit_percent": state.take_profit_percent,
"max_loss_usd": state.max_loss_usd,
"max_reserved_balance_percent": state.max_reserved_balance_percent,
"allocated_balance_usd": state.allocated_balance_usd,
# ---------- Cycle Stats Before Close Sync ---------- # ---------- Cycle Stats Before Close Sync ----------
"realized_pnl_usd_before": state.realized_pnl_usd, "realized_pnl_usd_before": state.realized_pnl_usd,
"cycle_realized_pnl_usd_before": state.cycle_realized_pnl_usd, "cycle_realized_pnl_usd_before": state.cycle_realized_pnl_usd,
@@ -443,36 +283,17 @@ class ExecutionPositionActionsMixin(_ExecutionPositionActionsProtocol):
"cycle_trade_fees_usd_before": state.cycle_trade_fees_usd, "cycle_trade_fees_usd_before": state.cycle_trade_fees_usd,
"cycle_overnight_fees_usd_before": state.cycle_overnight_fees_usd, "cycle_overnight_fees_usd_before": state.cycle_overnight_fees_usd,
# ---------- Position Health ---------- **build_runtime_payload(state),
"position_hold_seconds": state.position_hold_seconds, **build_signal_payload(state),
"position_health_status": state.position_health_status, **build_decision_payload(state),
"position_health_score": state.position_health_score, **build_runtime_blocks_payload(state),
"position_health_reason": state.position_health_reason, **build_execution_quality_payload(state),
"position_risk_level": state.position_risk_level, **build_execution_price_payload(state),
"position_risk_reason": state.position_risk_reason, **build_adaptive_size_payload(state),
"position_trend_alignment": state.position_trend_alignment, **build_risk_settings_payload(state),
"position_adverse_momentum": state.position_adverse_momentum, **build_position_health_payload(state),
**build_position_intelligence_payload(state),
# ---------- Position Intelligence ---------- **build_autonomous_payload(state),
"position_exit_signal": state.position_exit_signal,
"position_exit_confidence": state.position_exit_confidence,
"position_exit_urgency": state.position_exit_urgency,
"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,
# ---------- Autonomous ----------
"autonomous_action": state.autonomous_action,
"autonomous_action_reason": state.autonomous_action_reason,
"autonomous_action_confidence": state.autonomous_action_confidence,
"autonomous_protection_required": state.autonomous_protection_required,
"autonomous_reduce_required": state.autonomous_reduce_required,
"autonomous_exit_required": state.autonomous_exit_required,
**build_market_context_payload(state), **build_market_context_payload(state),
} }