浏览代码

Improve notification formatting in RiskCleanupManager for emergency exit actions. Enhanced readability by restructuring multiline messages and ensuring consistent presentation of key information, including position details and error handling.

Carles Sentis 6 天之前
父节点
当前提交
ee90cddaf2
共有 2 个文件被更改,包括 26 次插入30 次删除
  1. 25 29
      src/monitoring/risk_cleanup_manager.py
  2. 1 1
      trading_bot.py

+ 25 - 29
src/monitoring/risk_cleanup_manager.py

@@ -164,13 +164,14 @@ class RiskCleanupManager:
                         
                         if self.notification_manager:
                             await self.notification_manager.send_generic_notification(
-                                f"""🚨 AUTOMATIC STOP LOSS TRIGGERED!\\n
-Token: {token}\\n
-Lifecycle ID: {lifecycle_id_str}\\n
-Position: {position_side} {abs(contracts):.6f}\\n
-Entry Price: ${entry_price:.4f}\\n
-Unrealized P&L: ${unrealized_pnl:+.2f} ({roe_percentage:+.2f}% ROE)\\n
-Safety Threshold: -{Config.STOP_LOSS_PERCENTAGE}% ROE\\n
+                                f"""🚨 AUTOMATIC STOP LOSS TRIGGERED!
+
+Token: {token}
+Lifecycle ID: {lifecycle_id_str}
+Position: {position_side} {abs(contracts):.6f}
+Entry Price: ${entry_price:.4f}
+Unrealized P&L: ${unrealized_pnl:+.2f} ({roe_percentage:+.2f}% ROE)
+Safety Threshold: -{Config.STOP_LOSS_PERCENTAGE}% ROE
 Action: Executing emergency exit order..."""
                             )
 
@@ -179,25 +180,18 @@ Action: Executing emergency exit order..."""
                         if exit_result.get('success'):
                             placed_order_details = exit_result.get('order_placed_details', {})
                             logger.info(f"✅ Emergency exit order placed for {token} (Lifecycle: {lifecycle_id_str}). Order details: {placed_order_details}")
-                            if stats:
-                                cancelled_sl_count = stats.cancel_pending_stop_losses_by_symbol(
-                                    symbol=symbol,
-                                    new_status='cancelled_auto_exit'
-                                )
-                                if cancelled_sl_count > 0:
-                                    logger.info(f"🛑 Cancelled {cancelled_sl_count} pending stop losses for {symbol} (Lifecycle: {lifecycle_id_str}) after automatic exit")
                             
                             if self.notification_manager:
                                 await self.notification_manager.send_generic_notification(
-                                    f"""✅ <b>Emergency Exit Initiated</b>\\n\\n
-📊 <b>Position:</b> {token} {position_side}\\n
-🆔 <b>Lifecycle ID:</b> {lifecycle_id_str}\\n
-📉 <b>Loss at Trigger:</b> {roe_percentage:+.2f}% ROE (${unrealized_pnl:+.2f})\\n
-⚠️ <b>Threshold:</b> -{Config.STOP_LOSS_PERCENTAGE}% ROE\\n
-✅ <b>Action:</b> Market exit order placed successfully\\n
-🆔 <b>Exit Order ID:</b> {placed_order_details.get('exchange_order_id', 'N/A')}\\n
-{f'🛑 <b>Cleanup:</b> Cancelled {cancelled_sl_count} other pending stop losses' if cancelled_sl_count > 0 else ''}
-\\n\\n
+                                    f"""✅ <b>Emergency Exit Initiated</b>
+
+📊 <b>Position:</b> {token} {position_side}
+🆔 <b>Lifecycle ID:</b> {lifecycle_id_str}
+📉 <b>Loss at Trigger:</b> {roe_percentage:+.2f}% ROE (${unrealized_pnl:+.2f})
+⚠️ <b>Threshold:</b> -{Config.STOP_LOSS_PERCENTAGE}% ROE
+✅ <b>Action:</b> Market exit order placed successfully
+🆔 <b>Exit Order ID:</b> {placed_order_details.get('exchange_order_id', 'N/A')}
+
 🛡️ The system will confirm closure and P&L once the exit order fill is processed."""
                                 )
                         else:
@@ -205,12 +199,14 @@ Action: Executing emergency exit order..."""
                             logger.error(f"❌ Failed to execute emergency exit order for {token} (Lifecycle: {lifecycle_id_str}): {error_msg}")
                             if self.notification_manager:
                                 await self.notification_manager.send_generic_notification(
-                                    f"""❌ <b>CRITICAL: Emergency Exit Failed!</b>\\n\\n
-📊 <b>Position:</b> {token} {position_side}\\n
-🆔 <b>Lifecycle ID:</b> {lifecycle_id_str}\\n
-📉 <b>Loss:</b> {roe_percentage:+.2f}% ROE\\n
-❌ <b>Error Placing Order:</b> {error_msg}\\n\\n
-⚠️ <b>MANUAL INTERVENTION REQUIRED</b>\\n
+                                    f"""❌ <b>CRITICAL: Emergency Exit Failed!</b>
+
+📊 <b>Position:</b> {token} {position_side}
+🆔 <b>Lifecycle ID:</b> {lifecycle_id_str}
+📉 <b>Loss:</b> {roe_percentage:+.2f}% ROE
+❌ <b>Error Placing Order:</b> {error_msg}
+
+⚠️ <b>MANUAL INTERVENTION REQUIRED</b>
 Please close this position manually via /exit {token}"""
                                 )
                 except Exception as pos_error:

+ 1 - 1
trading_bot.py

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