Quellcode durchsuchen

Implement price validation and error handling in trading functions - Added error handling for price and stop loss inputs in the Telegram bot, ensuring robustness against invalid data. Introduced market price fallback mechanism to enhance user experience when invalid prices are provided.

Carles Sentis vor 5 Tagen
Ursprung
Commit
bb57f9fc22
1 geänderte Dateien mit 50 neuen und 6 gelöschten Zeilen
  1. 50 6
      src/telegram_bot.py

+ 50 - 6
src/telegram_bot.py

@@ -742,15 +742,24 @@ Tap any button below for instant access to bot functions:
             parts = callback_data.split('_')
             token = parts[2]
             usdc_amount = float(parts[3])
-            price = float(parts[4])
+            try:
+                price = float(parts[4])
+            except (ValueError, TypeError):
+                price = None  # Will be handled in execute_long_order
             is_limit = len(parts) > 5 and parts[5] == 'limit'
             
             # Parse stop loss if present
             stop_loss_price = None
             if len(parts) > 6 and parts[6] == 'sl':
-                stop_loss_price = float(parts[7])
+                try:
+                    stop_loss_price = float(parts[7])
+                except (ValueError, TypeError):
+                    stop_loss_price = None
             elif len(parts) > 5 and parts[5] == 'sl':
-                stop_loss_price = float(parts[6])
+                try:
+                    stop_loss_price = float(parts[6])
+                except (ValueError, TypeError):
+                    stop_loss_price = None
             
             await self._execute_long_order(query, token, usdc_amount, price, is_limit, stop_loss_price)
             return
@@ -759,15 +768,24 @@ Tap any button below for instant access to bot functions:
             parts = callback_data.split('_')
             token = parts[2]
             usdc_amount = float(parts[3])
-            price = float(parts[4])
+            try:
+                price = float(parts[4])
+            except (ValueError, TypeError):
+                price = None  # Will be handled in execute_short_order
             is_limit = len(parts) > 5 and parts[5] == 'limit'
             
             # Parse stop loss if present
             stop_loss_price = None
             if len(parts) > 6 and parts[6] == 'sl':
-                stop_loss_price = float(parts[7])
+                try:
+                    stop_loss_price = float(parts[7])
+                except (ValueError, TypeError):
+                    stop_loss_price = None
             elif len(parts) > 5 and parts[5] == 'sl':
-                stop_loss_price = float(parts[6])
+                try:
+                    stop_loss_price = float(parts[6])
+                except (ValueError, TypeError):
+                    stop_loss_price = None
             
             await self._execute_short_order(query, token, usdc_amount, price, is_limit, stop_loss_price)
             return
@@ -855,6 +873,19 @@ Tap any button below for instant access to bot functions:
         try:
             await query.edit_message_text("⏳ Opening long position...")
             
+            # Validate price
+            if price is None or price <= 0:
+                # Try to get current market price
+                market_data = self.client.get_market_data(symbol)
+                if market_data and market_data.get('ticker'):
+                    price = float(market_data['ticker'].get('last', 0))
+                    if price <= 0:
+                        await query.edit_message_text("❌ Unable to get valid market price. Please try again.")
+                        return
+                else:
+                    await query.edit_message_text("❌ Unable to fetch market price. Please try again.")
+                    return
+            
             # Calculate token amount based on USDC value and price
             token_amount = usdc_amount / price
             
@@ -925,6 +956,19 @@ Tap any button below for instant access to bot functions:
         try:
             await query.edit_message_text("⏳ Opening short position...")
             
+            # Validate price
+            if price is None or price <= 0:
+                # Try to get current market price
+                market_data = self.client.get_market_data(symbol)
+                if market_data and market_data.get('ticker'):
+                    price = float(market_data['ticker'].get('last', 0))
+                    if price <= 0:
+                        await query.edit_message_text("❌ Unable to get valid market price. Please try again.")
+                        return
+                else:
+                    await query.edit_message_text("❌ Unable to fetch market price. Please try again.")
+                    return
+            
             # Calculate token amount based on USDC value and price
             token_amount = usdc_amount / price