瀏覽代碼

Increment BOT_VERSION to 2.2.147 and enhance RiskCleanupManager with active position verification.

- Updated BOT_VERSION for the upcoming release.
- Added logic in RiskCleanupManager to verify the existence of an active position before executing stop-loss triggers, preventing incorrect order placements and improving risk management.
- Implemented notification system for cancelled stop-loss triggers when no active position is found, enhancing user awareness.
Carles Sentis 2 天之前
父節點
當前提交
bac5d4e5d3
共有 2 個文件被更改,包括 23 次插入1 次删除
  1. 22 0
      src/monitoring/risk_cleanup_manager.py
  2. 1 1
      trading_bot.py

+ 22 - 0
src/monitoring/risk_cleanup_manager.py

@@ -75,6 +75,28 @@ class RiskCleanupManager:
                     logger.info(f"🟢 SL TRIGGER HIT (Buy): Order DB ID {order_db_id}, Symbol {symbol}, Trigger@ ${trigger_price:.4f}, Market@ ${current_price:.4f}")
                 
                 if trigger_hit:
+                    # Verify active position before executing SL
+                    active_position_exists = False
+                    cached_positions = self.market_monitor_cache.cached_positions or []
+                    for pos in cached_positions:
+                        if pos.get('symbol') == symbol and abs(float(pos.get('contracts', 0))) > 1e-9: # Check for non-zero contracts
+                            active_position_exists = True
+                            break
+                    
+                    if not active_position_exists:
+                        logger.warning(f"🚫 SL TRIGGER IGNORED for {symbol} (DB ID: {order_db_id}): No active position found. Cancelling trigger.")
+                        stats.update_order_status(order_db_id=order_db_id, new_status='cancelled_no_position')
+                        if self.notification_manager:
+                            await self.notification_manager.send_generic_notification(
+                                f"⚠️ Stop-Loss Trigger Cancelled (No Position)\\n"
+                                f"Symbol: {symbol}\\n"
+                                f"Side: {trigger_side.upper()}\\n"
+                                f"Trigger Price: ${trigger_price:.4f}\\n"
+                                f"Reason: The stop-loss trigger price was hit, but no active position was found for this symbol.\\n"
+                                f"The pending SL (DB ID: {order_db_id}) has been cancelled to prevent incorrect order placement."
+                            )
+                        continue # Skip to the next trigger
+
                     logger.info(f"Attempting to execute actual stop order for triggered DB ID: {order_db_id} (Parent Bot Ref: {trigger_order.get('parent_bot_order_ref_id')})")
                     execution_result = await self.trading_engine.execute_triggered_stop_order(original_trigger_order_db_id=order_db_id)
                     notification_message_detail = ""

+ 1 - 1
trading_bot.py

@@ -14,7 +14,7 @@ from datetime import datetime
 from pathlib import Path
 
 # Bot version
-BOT_VERSION = "2.2.146"
+BOT_VERSION = "2.2.147"
 
 # Add src directory to Python path
 sys.path.insert(0, str(Path(__file__).parent / "src"))