Browse Source

Improve market command response handling by adding a loading message and updating the message editing process for unauthorized access, market data retrieval errors, and successful data display. Enhance price information formatting to include percentage positions within the daily range for better market insights.

Carles Sentis 4 days ago
parent
commit
bdfc910e3f
2 changed files with 25 additions and 7 deletions
  1. 24 6
      src/commands/info/market.py
  2. 1 1
      trading_bot.py

+ 24 - 6
src/commands/info/market.py

@@ -17,9 +17,13 @@ class MarketCommands(InfoCommandsBase):
 
 
     async def market_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
     async def market_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
         """Handle the /market command to show market information."""
         """Handle the /market command to show market information."""
+        
+        # Send a loading message first
+        sent_message = await self._reply(update, "⏳ Fetching market data...")
+        
         try:
         try:
             if not self._is_authorized(update):
             if not self._is_authorized(update):
-                await self._reply(update, "❌ Unauthorized access.")
+                await context.bot.edit_message_text(chat_id=sent_message.chat.id, message_id=sent_message.message_id, text="❌ Unauthorized access.")
                 return
                 return
 
 
             # Get token from command arguments or use default
             # Get token from command arguments or use default
@@ -31,7 +35,7 @@ class MarketCommands(InfoCommandsBase):
             # Get market data
             # Get market data
             market_data = await self.trading_engine.get_market_data(symbol)
             market_data = await self.trading_engine.get_market_data(symbol)
             if not market_data:
             if not market_data:
-                await self._reply(update, f"❌ Could not get market data for {symbol}. Check if the symbol is correct.")
+                await context.bot.edit_message_text(chat_id=sent_message.chat.id, message_id=sent_message.message_id, text=f"❌ Could not get market data for {symbol}. Check if the symbol is correct.")
                 return
                 return
             
             
             # Get all markets to find leverage info
             # Get all markets to find leverage info
@@ -49,6 +53,20 @@ class MarketCommands(InfoCommandsBase):
             last_price = ticker.get('last', 0)
             last_price = ticker.get('last', 0)
             prev_close = ticker.get('previousClose', 0)
             prev_close = ticker.get('previousClose', 0)
             change_24h = ((last_price - prev_close) / prev_close * 100) if prev_close else 0
             change_24h = ((last_price - prev_close) / prev_close * 100) if prev_close else 0
+            
+            # Calculate price position within the daily range
+            high_price = ticker.get('high', 0) or 0
+            low_price = ticker.get('low', 0) or 0
+            
+            percent_from_low_str = ""
+            percent_from_high_str = ""
+            if high_price > low_price and low_price > 0 and last_price > 0:
+                total_range = high_price - low_price
+                dist_from_low = last_price - low_price
+                percent_from_low = (dist_from_low / total_range) * 100
+                percent_from_high = 100 - percent_from_low
+                percent_from_low_str = f" ({percent_from_low:.0f}% above)"
+                percent_from_high_str = f" ({percent_from_high:.0f}% below)"
 
 
             # Format the message
             # Format the message
             message = f"📊 <b>Market Information for {token}</b>\n\n"
             message = f"📊 <b>Market Information for {token}</b>\n\n"
@@ -56,8 +74,8 @@ class MarketCommands(InfoCommandsBase):
             # Add price information
             # Add price information
             message += "💰 <b>Price Information:</b>\n"
             message += "💰 <b>Price Information:</b>\n"
             message += f"Last Price: {self.formatter.format_price_with_symbol(last_price)}\n"
             message += f"Last Price: {self.formatter.format_price_with_symbol(last_price)}\n"
-            message += f"24h High: {self.formatter.format_price_with_symbol(ticker.get('high', 0) or 0)}\n"
-            message += f"24h Low: {self.formatter.format_price_with_symbol(ticker.get('low', 0) or 0)}\n"
+            message += f"24h High: {self.formatter.format_price_with_symbol(high_price)}{percent_from_high_str}\n"
+            message += f"24h Low: {self.formatter.format_price_with_symbol(low_price)}{percent_from_low_str}\n"
             message += f"24h Change: {change_24h:.2f}%\n"
             message += f"24h Change: {change_24h:.2f}%\n"
             
             
             # Add leverage information if available
             # Add leverage information if available
@@ -81,8 +99,8 @@ class MarketCommands(InfoCommandsBase):
                 message += f"Ask: {self.formatter.format_price_with_symbol(ask)}\n"
                 message += f"Ask: {self.formatter.format_price_with_symbol(ask)}\n"
                 message += f"Spread: {self.formatter.format_price_with_symbol(spread)}\n"
                 message += f"Spread: {self.formatter.format_price_with_symbol(spread)}\n"
             
             
-            await self._reply(update, message.strip())
+            await context.bot.edit_message_text(chat_id=sent_message.chat.id, message_id=sent_message.message_id, text=message.strip(), parse_mode='HTML')
             
             
         except Exception as e:
         except Exception as e:
             logger.error(f"Error in market command: {e}")
             logger.error(f"Error in market command: {e}")
-            await self._reply(update, "❌ Error getting market information. Please try again later.")
+            await context.bot.edit_message_text(chat_id=sent_message.chat.id, message_id=sent_message.message_id, text="❌ Error getting market information. Please try again later.")

+ 1 - 1
trading_bot.py

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