|
@@ -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 = ""
|