浏览代码

Enhance market information retrieval in MarketCommands by extracting ticker and orderbook data for improved clarity. Calculate 24h price change and update volume and turnover formatting to utilize new ticker information. Refactor order book display to ensure accurate spread calculation.

Carles Sentis 4 天之前
父节点
当前提交
c146ef180a
共有 2 个文件被更改,包括 23 次插入11 次删除
  1. 22 10
      src/commands/info/market.py
  2. 1 1
      trading_bot.py

+ 22 - 10
src/commands/info/market.py

@@ -38,34 +38,46 @@ class MarketCommands(InfoCommandsBase):
             all_markets = await self.trading_engine.client.get_markets()
             all_markets = await self.trading_engine.client.get_markets()
             market_info = all_markets.get(symbol) if all_markets else None
             market_info = all_markets.get(symbol) if all_markets else None
             
             
+            # Extract ticker and orderbook for easier access
+            ticker = market_data.get('ticker', {})
+            orderbook = market_data.get('orderbook', {})
+            
+            # Safely get nested info dictionary
+            ticker_info = ticker.get('info', {})
+
+            # Calculate 24h change
+            last_price = ticker.get('last', 0)
+            prev_close = ticker.get('previousClose', 0)
+            change_24h = ((last_price - prev_close) / prev_close * 100) if prev_close else 0
+
             # 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"
             
             
             # 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(market_data.get('last', 0))}\n"
-            message += f"24h High: {self.formatter.format_price_with_symbol(market_data.get('high', 0))}\n"
-            message += f"24h Low: {self.formatter.format_price_with_symbol(market_data.get('low', 0))}\n"
-            message += f"24h Change: {market_data.get('change', 0):.2f}%\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 Change: {change_24h:.2f}%\n"
             
             
             # Add leverage information if available
             # Add leverage information if available
             if market_info:
             if market_info:
-                max_leverage = market_info.get('limits', {}).get('leverage', {}).get('max', 'N/A')
+                max_leverage = ticker_info.get('maxLeverage', 'N/A')
                 if max_leverage != 'N/A':
                 if max_leverage != 'N/A':
                     message += f"Leverage: Up to {max_leverage}x\n"
                     message += f"Leverage: Up to {max_leverage}x\n"
             
             
             # Add volume information
             # Add volume information
             message += f"\n📈 <b>Volume Information:</b>\n"
             message += f"\n📈 <b>Volume Information:</b>\n"
-            message += f"24h Volume: {self.formatter.format_amount(market_data.get('volume', 0), token)}\n"
-            message += f"24h Turnover: {self.formatter.format_price_with_symbol(market_data.get('turnover', 0))}\n"
+            message += f"24h Volume: {self.formatter.format_amount(float(ticker_info.get('dayBaseVlm', 0)), token)}\n"
+            message += f"24h Turnover: {self.formatter.format_price_with_symbol(float(ticker_info.get('dayNtlVlm', 0)))}\n"
             
             
             # Add order book information if available
             # Add order book information if available
-            if 'orderbook' in market_data:
-                orderbook = market_data['orderbook']
+            if orderbook:
+                spread = orderbook.get('ask', 0) - orderbook.get('bid', 0)
                 message += f"\n📚 <b>Order Book:</b>\n"
                 message += f"\n📚 <b>Order Book:</b>\n"
                 message += f"Bid: {self.formatter.format_price_with_symbol(orderbook.get('bid', 0))}\n"
                 message += f"Bid: {self.formatter.format_price_with_symbol(orderbook.get('bid', 0))}\n"
                 message += f"Ask: {self.formatter.format_price_with_symbol(orderbook.get('ask', 0))}\n"
                 message += f"Ask: {self.formatter.format_price_with_symbol(orderbook.get('ask', 0))}\n"
-                message += f"Spread: {self.formatter.format_price_with_symbol(orderbook.get('spread', 0))}\n"
+                message += f"Spread: {self.formatter.format_price_with_symbol(spread)}\n"
             
             
             await self._reply(update, message.strip())
             await self._reply(update, message.strip())
             
             

+ 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.224"
+BOT_VERSION = "2.4.225"
 
 
 # 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"))