Преглед изворни кода

Refactor price formatting in InfoCommands and TradingCommands - Integrated the PriceFormatter for consistent price display across order and trade notifications. Updated relevant sections to utilize the formatter, enhancing clarity in user messages regarding order details and trade confirmations.

Carles Sentis пре 4 дана
родитељ
комит
c76ba56746
2 измењених фајлова са 46 додато и 36 уклоњено
  1. 7 4
      src/commands/info_commands.py
  2. 39 32
      src/commands/trading_commands.py

+ 7 - 4
src/commands/info_commands.py

@@ -204,6 +204,7 @@ class InfoCommands:
                 for symbol, symbol_orders in orders_by_symbol.items():
                     orders_text += f"📊 <b>{symbol}</b>\n"
                     
+                    formatter = get_formatter()
                     for order in symbol_orders:
                         side = order.get('side', '').upper()
                         amount = float(order.get('amount', 0))
@@ -214,7 +215,7 @@ class InfoCommands:
                         # Order emoji
                         side_emoji = "🟢" if side == "BUY" else "🔴"
                         
-                        orders_text += f"   {side_emoji} {side} {amount:.6f} @ ${price:,.2f}\n"
+                        orders_text += f"   {side_emoji} {side} {amount:.6f} @ {formatter.format_price_with_symbol(price, symbol)}\n"
                         orders_text += f"   📋 Type: {order_type} | ID: {order_id}\n"
                         
                         # Check for pending stop losses linked to this order
@@ -233,7 +234,7 @@ class InfoCommands:
                                         sl_order = linked_sls[0]  # Should only be one
                                         sl_price = sl_order.get('price', 0)
                                         sl_side = sl_order.get('side', '').upper()
-                                        orders_text += f"   🛑 Pending SL: {sl_side} @ ${sl_price:,.2f} (activates when filled)\n"
+                                        orders_text += f"   🛑 Pending SL: {sl_side} @ {formatter.format_price_with_symbol(sl_price, symbol)} (activates when filled)\n"
                         
                         orders_text += "\n"
                 
@@ -289,12 +290,14 @@ class InfoCommands:
         
         trades_text = "🔄 <b>Recent Trades</b>\n\n"
         
+        formatter = get_formatter()
         for trade in reversed(recent_trades[-5:]):  # Show last 5 trades
             timestamp = datetime.fromisoformat(trade['timestamp']).strftime('%m/%d %H:%M')
             side_emoji = "🟢" if trade['side'] == 'buy' else "🔴"
+            symbol = trade['symbol']
             
-            trades_text += f"{side_emoji} <b>{trade['side'].upper()}</b> {trade['amount']} {trade['symbol']}\n"
-            trades_text += f"   💰 ${trade['price']:,.2f} | 💵 ${trade['value']:,.2f}\n"
+            trades_text += f"{side_emoji} <b>{trade['side'].upper()}</b> {trade['amount']} {symbol}\n"
+            trades_text += f"   💰 {formatter.format_price_with_symbol(trade['price'], symbol)} | 💵 {formatter.format_price_with_symbol(trade['value'])}\n"
             trades_text += f"   📅 {timestamp}\n\n"
         
         await context.bot.send_message(chat_id=chat_id, text=trades_text, parse_mode='HTML')

+ 39 - 32
src/commands/trading_commands.py

@@ -314,21 +314,22 @@ This will {"place a limit sell order" if limit_price else "execute a market sell
             pnl_emoji = "🟢" if unrealized_pnl >= 0 else "🔴"
             exit_emoji = "🔴" if position_type == "LONG" else "🟢"
             
+            formatter = get_formatter()
             confirmation_text = f"""
 {exit_emoji} <b>Exit Position Confirmation</b>
 
 📊 <b>Position Details:</b>
 • Token: {token}
 • Position: {position_type}
-• Size: {get_formatter(contracts)} contracts
-• Entry Price: {get_formatter(entry_price)}
-• Current Price: {get_formatter(current_price)}
-• {pnl_emoji} Unrealized P&L: {get_formatter(unrealized_pnl)}
+• Size: {contracts:.6f} contracts
+• Entry Price: {formatter.format_price_with_symbol(entry_price, token)}
+• Current Price: {formatter.format_price_with_symbol(current_price, token)}
+• {pnl_emoji} Unrealized P&L: {formatter.format_price_with_symbol(unrealized_pnl)}
 
 🎯 <b>Exit Order:</b>
 • Action: {exit_side.upper()} (Close {position_type})
-• Amount: {get_formatter(contracts)} {token}
-• Est. Value: ~{get_formatter(exit_value)}
+• Amount: {contracts:.6f} {token}
+• Est. Value: ~{formatter.format_price_with_symbol(exit_value)}
 • Order Type: Market Order
 
 ⚠️ <b>Are you sure you want to close this {position_type} position?</b>
@@ -379,21 +380,23 @@ This will {"place a limit sell order" if limit_price else "execute a market sell
             
             # Validate stop loss price based on position direction
             if position_type == "LONG" and stop_price >= entry_price:
+                formatter = get_formatter()
                 await context.bot.send_message(chat_id=chat_id, text=(
                     f"⚠️ Stop loss price should be BELOW entry price for long positions\n\n"
                     f"📊 Your {token} LONG position:\n"
-                    f"• Entry Price: {get_formatter(entry_price)}\n"
-                    f"• Stop Price: {get_formatter(stop_price)} ❌\n\n"
-                    f"💡 Try a lower price like: /sl {token} {get_formatter(entry_price * 0.95)}\n"
+                    f"• Entry Price: {formatter.format_price_with_symbol(entry_price, token)}\n"
+                    f"• Stop Price: {formatter.format_price_with_symbol(stop_price, token)} ❌\n\n"
+                    f"💡 Try a lower price like: /sl {token} {formatter.format_price(entry_price * 0.95, token)}\n"
                 ))
                 return
             elif position_type == "SHORT" and stop_price <= entry_price:
+                formatter = get_formatter()
                 await context.bot.send_message(chat_id=chat_id, text=(
                     f"⚠️ Stop loss price should be ABOVE entry price for short positions\n\n"
                     f"📊 Your {token} SHORT position:\n"
-                    f"• Entry Price: {get_formatter(entry_price)}\n"
-                    f"• Stop Price: {get_formatter(stop_price)} ❌\n\n"
-                    f"💡 Try a higher price like: /sl {token} {get_formatter(entry_price * 1.05)}\n"
+                    f"• Entry Price: {formatter.format_price_with_symbol(entry_price, token)}\n"
+                    f"• Stop Price: {formatter.format_price_with_symbol(stop_price, token)} ❌\n\n"
+                    f"💡 Try a higher price like: /sl {token} {formatter.format_price(entry_price * 1.05, token)}\n"
                 ))
                 return
             
@@ -411,26 +414,27 @@ This will {"place a limit sell order" if limit_price else "execute a market sell
             
             pnl_emoji = "🟢" if pnl_at_stop >= 0 else "🔴"
             
+            formatter = get_formatter()
             confirmation_text = f"""
 🛑 <b>Stop Loss Order Confirmation</b>
 
 📊 <b>Position Details:</b>
 • Token: {token}
 • Position: {position_type}
-• Size: {get_formatter(contracts)} contracts
-• Entry Price: {get_formatter(entry_price)}
-• Current Price: {get_formatter(current_price)}
+• Size: {contracts:.6f} contracts
+• Entry Price: {formatter.format_price_with_symbol(entry_price, token)}
+• Current Price: {formatter.format_price_with_symbol(current_price, token)}
 
 🎯 <b>Stop Loss Order:</b>
-• Stop Price: {get_formatter(stop_price)}
+• Stop Price: {formatter.format_price_with_symbol(stop_price, token)}
 • Action: {exit_side.upper()} (Close {position_type})
-• Amount: {get_formatter(contracts)} {token}
+• Amount: {contracts:.6f} {token}
 • Order Type: Limit Order
-• {pnl_emoji} Est. P&L: {get_formatter(pnl_at_stop)}
+• {pnl_emoji} Est. P&L: {formatter.format_price_with_symbol(pnl_at_stop)}
 
 ⚠️ <b>Are you sure you want to set this stop loss?</b>
 
-This will place a limit {exit_side} order at {get_formatter(stop_price)} to protect your {position_type} position.
+This will place a limit {exit_side} order at {formatter.format_price_with_symbol(stop_price, token)} to protect your {position_type} position.
             """
             
             keyboard = [
@@ -480,21 +484,23 @@ This will place a limit {exit_side} order at {get_formatter(stop_price)} to prot
             
             # Validate take profit price based on position direction
             if position_type == "LONG" and tp_price <= entry_price:
+                formatter = get_formatter()
                 await context.bot.send_message(chat_id=chat_id, text=(
                     f"⚠️ Take profit price should be ABOVE entry price for long positions\n\n"
                     f"📊 Your {token} LONG position:\n"
-                    f"• Entry Price: {get_formatter(entry_price)}\n"
-                    f"• Take Profit: {get_formatter(tp_price)} ❌\n\n"
-                    f"💡 Try a higher price like: /tp {token} {get_formatter(entry_price * 1.05)}\n"
+                    f"• Entry Price: {formatter.format_price_with_symbol(entry_price, token)}\n"
+                    f"• Take Profit: {formatter.format_price_with_symbol(tp_price, token)} ❌\n\n"
+                    f"💡 Try a higher price like: /tp {token} {formatter.format_price(entry_price * 1.05, token)}\n"
                 ))
                 return
             elif position_type == "SHORT" and tp_price >= entry_price:
+                formatter = get_formatter()
                 await context.bot.send_message(chat_id=chat_id, text=(
                     f"⚠️ Take profit price should be BELOW entry price for short positions\n\n"
                     f"📊 Your {token} SHORT position:\n"
-                    f"• Entry Price: {get_formatter(entry_price)}\n"
-                    f"• Take Profit: {get_formatter(tp_price)} ❌\n\n"
-                    f"💡 Try a lower price like: /tp {token} {get_formatter(entry_price * 0.95)}\n"
+                    f"• Entry Price: {formatter.format_price_with_symbol(entry_price, token)}\n"
+                    f"• Take Profit: {formatter.format_price_with_symbol(tp_price, token)} ❌\n\n"
+                    f"💡 Try a lower price like: /tp {token} {formatter.format_price(entry_price * 0.95, token)}\n"
                 ))
                 return
             
@@ -512,26 +518,27 @@ This will place a limit {exit_side} order at {get_formatter(stop_price)} to prot
             
             pnl_emoji = "🟢" if pnl_at_tp >= 0 else "🔴"
             
+            formatter = get_formatter()
             confirmation_text = f"""
 🎯 <b>Take Profit Order Confirmation</b>
 
 📊 <b>Position Details:</b>
 • Token: {token}
 • Position: {position_type}
-• Size: {get_formatter(contracts)} contracts
-• Entry Price: {get_formatter(entry_price)}
-• Current Price: {get_formatter(current_price)}
+• Size: {contracts:.6f} contracts
+• Entry Price: {formatter.format_price_with_symbol(entry_price, token)}
+• Current Price: {formatter.format_price_with_symbol(current_price, token)}
 
 🎯 <b>Take Profit Order:</b>
-• Take Profit Price: {get_formatter(tp_price)}
+• Take Profit Price: {formatter.format_price_with_symbol(tp_price, token)}
 • Action: {exit_side.upper()} (Close {position_type})
-• Amount: {get_formatter(contracts)} {token}
+• Amount: {contracts:.6f} {token}
 • Order Type: Limit Order
-• {pnl_emoji} Est. P&L: {get_formatter(pnl_at_tp)}
+• {pnl_emoji} Est. P&L: {formatter.format_price_with_symbol(pnl_at_tp)}
 
 ⚠️ <b>Are you sure you want to set this take profit?</b>
 
-This will place a limit {exit_side} order at {get_formatter(tp_price)} to secure profits from your {position_type} position.
+This will place a limit {exit_side} order at {formatter.format_price_with_symbol(tp_price, token)} to secure profits from your {position_type} position.
             """
             
             keyboard = [