Browse Source

Refactor trading commands to use async/await for market data and position retrieval, improving responsiveness and ensuring non-blocking operations. This change enhances the overall performance of trading operations.

Carles Sentis 3 days ago
parent
commit
8f636db6cb
2 changed files with 10 additions and 10 deletions
  1. 9 9
      src/commands/trading_commands.py
  2. 1 1
      trading_bot.py

+ 9 - 9
src/commands/trading_commands.py

@@ -82,7 +82,7 @@ class TradingCommands:
                         return
             
             # Get current market price
-            market_data = self.trading_engine.get_market_data(f"{token}/USDC:USDC")
+            market_data = await self.trading_engine.get_market_data(f"{token}/USDC:USDC")
             if not market_data:
                 await context.bot.send_message(chat_id=chat_id, text=f"❌ Could not fetch market data for {token}")
                 return
@@ -203,7 +203,7 @@ This will {"place a limit buy order" if limit_price else "execute a market buy o
                         return
             
             # Get current market price
-            market_data = self.trading_engine.get_market_data(f"{token}/USDC:USDC")
+            market_data = await self.trading_engine.get_market_data(f"{token}/USDC:USDC")
             if not market_data:
                 await context.bot.send_message(chat_id=chat_id, text=f"❌ Could not fetch market data for {token}")
                 return
@@ -302,7 +302,7 @@ This will {"place a limit sell order" if limit_price else "execute a market sell
             token = _normalize_token_case(context.args[0])
             
             # Find the position
-            position = self.trading_engine.find_position(token)
+            position = await self.trading_engine.find_position(token)
             if not position:
                 await context.bot.send_message(chat_id=chat_id, text=f"📭 No open position found for {token}")
                 return
@@ -313,7 +313,7 @@ This will {"place a limit sell order" if limit_price else "execute a market sell
             unrealized_pnl = float(position.get('unrealizedPnl', 0))
             
             # Get current market price
-            market_data = self.trading_engine.get_market_data(f"{token}/USDC:USDC")
+            market_data = await self.trading_engine.get_market_data(f"{token}/USDC:USDC")
             if not market_data:
                 await context.bot.send_message(chat_id=chat_id, text=f"❌ Could not fetch current price for {token}")
                 return
@@ -380,7 +380,7 @@ This will {"place a limit sell order" if limit_price else "execute a market sell
             stop_price = float(context.args[1])
             
             # Find the position
-            position = self.trading_engine.find_position(token)
+            position = await self.trading_engine.find_position(token)
             if not position:
                 await context.bot.send_message(chat_id=chat_id, text=f"📭 No open position found for {token}")
                 return
@@ -407,12 +407,12 @@ This will {"place a limit sell order" if limit_price else "execute a market sell
                     f"📊 Your {token} SHORT position:\n"
                     f"• Entry Price: {await formatter.format_price_with_symbol(entry_price, token)}\n"
                     f"• Stop Price: {await formatter.format_price_with_symbol(stop_price, token)} ❌\n\n"
-                    f"�� Try a higher price like: /sl {token} {await formatter.format_price(entry_price * 1.05, token)}\n"
+                    f"💡 Try a higher price like: /sl {token} {await formatter.format_price(entry_price * 1.05, token)}\n"
                 ))
                 return
             
             # Get current market price
-            market_data = self.trading_engine.get_market_data(f"{token}/USDC:USDC")
+            market_data = await self.trading_engine.get_market_data(f"{token}/USDC:USDC")
             current_price = 0
             if market_data:
                 current_price = float(market_data['ticker'].get('last', 0))
@@ -484,7 +484,7 @@ This will place a limit {exit_side} order at {await formatter.format_price_with_
             tp_price = float(context.args[1])
             
             # Find the position
-            position = self.trading_engine.find_position(token)
+            position = await self.trading_engine.find_position(token)
             if not position:
                 await context.bot.send_message(chat_id=chat_id, text=f"📭 No open position found for {token}")
                 return
@@ -516,7 +516,7 @@ This will place a limit {exit_side} order at {await formatter.format_price_with_
                 return
             
             # Get current market price
-            market_data = self.trading_engine.get_market_data(f"{token}/USDC:USDC")
+            market_data = await self.trading_engine.get_market_data(f"{token}/USDC:USDC")
             current_price = 0
             if market_data:
                 current_price = float(market_data['ticker'].get('last', 0))

+ 1 - 1
trading_bot.py

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