Parcourir la source

Enhance trade price validation in Telegram bot - Added fallback mechanism to ensure valid price is used when recording trades, improving robustness against unexpected price values. This change reinforces error handling in trading functions.

Carles Sentis il y a 5 jours
Parent
commit
db32706b19
1 fichiers modifiés avec 12 ajouts et 0 suppressions
  1. 12 0
      src/telegram_bot.py

+ 12 - 0
src/telegram_bot.py

@@ -899,6 +899,9 @@ Tap any button below for instant access to bot functions:
                 # Record the trade in stats
                 order_id = order.get('id', 'N/A')
                 actual_price = order.get('average', price)  # Use actual fill price if available
+                if actual_price is None or actual_price <= 0:
+                    # This should not happen due to our price validation above, but extra safety
+                    actual_price = price
                 action_type = self.stats.record_trade_with_enhanced_tracking(symbol, 'buy', token_amount, actual_price, order_id, "bot")
                 
                 # Save pending stop loss if provided
@@ -982,6 +985,9 @@ Tap any button below for instant access to bot functions:
                 # Record the trade in stats
                 order_id = order.get('id', 'N/A')
                 actual_price = order.get('average', price)  # Use actual fill price if available
+                if actual_price is None or actual_price <= 0:
+                    # This should not happen due to our price validation above, but extra safety
+                    actual_price = price
                 action_type = self.stats.record_trade_with_enhanced_tracking(symbol, 'sell', token_amount, actual_price, order_id, "bot")
                 
                 # Save pending stop loss if provided
@@ -1046,6 +1052,9 @@ Tap any button below for instant access to bot functions:
                 # Record the trade in stats
                 order_id = order.get('id', 'N/A')
                 actual_price = order.get('average', price)  # Use actual fill price if available
+                if actual_price is None or actual_price <= 0:
+                    # Fallback to ensure we have a valid price
+                    actual_price = price
                 action_type = self.stats.record_trade_with_enhanced_tracking(symbol, exit_side, contracts, actual_price, order_id, "bot")
                 
                 position_type = "LONG" if exit_side == "sell" else "SHORT"
@@ -1223,6 +1232,9 @@ Tap any button below for instant access to bot functions:
                 # Record the trade in stats
                 order_id = order.get('id', 'N/A')
                 actual_price = order.get('average', price)  # Use actual fill price if available
+                if actual_price is None or actual_price <= 0:
+                    # Fallback to ensure we have a valid price
+                    actual_price = price
                 action_type = self.stats.record_trade_with_enhanced_tracking(symbol, exit_side, contracts, actual_price, order_id, "bot")
                 
                 position_type = "LONG" if exit_side == "sell" else "SHORT"