瀏覽代碼

Refactor InfoCommands to improve performance report formatting and user experience

- Streamlined message construction for daily, weekly, and monthly performance reports by consolidating text parts and enhancing readability.
- Updated handling of trading activity messages to provide clearer summaries and improved formatting.
- Ensured consistent use of escape characters for better message display in chat.
Carles Sentis 3 天之前
父節點
當前提交
b18401693d
共有 1 個文件被更改,包括 67 次插入64 次删除
  1. 67 64
      src/commands/info_commands.py

+ 67 - 64
src/commands/info_commands.py

@@ -904,48 +904,49 @@ class InfoCommands:
             
             if not daily_stats:
                 await context.bot.send_message(chat_id=chat_id, text=
-                    "📅 <b>Daily Performance</b>\n\n"
-                    "📭 No daily performance data available yet.\n\n"
-                    "💡 Daily stats are calculated from completed trades.\n"
-                    "Start trading to see daily performance!",
+                    "📅 <b>Daily Performance</b>\\n"
+                    "📭 No daily performance data available yet.\\n"
+                    "💡 Daily stats are calculated from completed trades. Start trading to see them!",
                     parse_mode='HTML'
                 )
                 return
             
-            daily_text = "📅 <b>Daily Performance (Last 10 Days)</b>\n\n"
+            daily_text_parts = [f"📅 <b>Daily Performance (Last 10 Days)</b>"]
             
-            total_pnl_all_days = 0 # Renamed to avoid conflict
-            total_trades_all_days = 0 # Renamed
-            trading_days_count = 0 # Renamed
+            total_pnl_all_days = 0
+            total_trades_all_days = 0
+            trading_days_count = 0
             
-            for day_stats_item in daily_stats: # Renamed to avoid conflict
+            period_lines = []
+            for day_stats_item in daily_stats:
                 if day_stats_item['has_trades']:
                     pnl_emoji = "🟢" if day_stats_item['pnl'] >= 0 else "🔴"
                     pnl_str = formatter.format_price_with_symbol(day_stats_item['pnl'])
-                    daily_text += f"📊 <b>{day_stats_item['date_formatted']}</b>\n"
-                    daily_text += f"   {pnl_emoji} P&L: {pnl_str} ({day_stats_item['pnl_pct']:+.1f}%)\n"
-                    daily_text += f"   🔄 Trades: {day_stats_item['trades']}\n\n"
-                    
+                    period_lines.append(f"📊 <b>{day_stats_item['date_formatted']}</b>: {pnl_emoji} {pnl_str} ({day_stats_item['pnl_pct']:+.1f}%) | Trades: {day_stats_item['trades']}")
                     total_pnl_all_days += day_stats_item['pnl']
                     total_trades_all_days += day_stats_item['trades']
                     trading_days_count += 1
                 else:
-                    daily_text += f"📊 <b>{day_stats_item['date_formatted']}</b>\n"
-                    daily_text += f"   📭 No trading activity\n\n"
+                    period_lines.append(f"📊 <b>{day_stats_item['date_formatted']}</b>: 📭 No trading activity")
             
+            if period_lines: # Add collected period lines if any
+                daily_text_parts.append("\\n".join(period_lines))
+
             if trading_days_count > 0:
                 avg_daily_pnl = total_pnl_all_days / trading_days_count
                 avg_pnl_emoji = "🟢" if avg_daily_pnl >= 0 else "🔴"
                 total_pnl_all_days_str = formatter.format_price_with_symbol(total_pnl_all_days)
                 avg_daily_pnl_str = formatter.format_price_with_symbol(avg_daily_pnl)
                 
-                daily_text += f"📈 <b>Period Summary:</b>\n"
-                daily_text += f"   {avg_pnl_emoji} Total P&L: {total_pnl_all_days_str}\n"
-                daily_text += f"   📊 Trading Days: {trading_days_count}/10\n"
-                daily_text += f"   📅 Avg Daily P&L: {avg_daily_pnl_str}\n"
-                daily_text += f"   🔄 Total Trades: {total_trades_all_days}\n"
-            
-            await context.bot.send_message(chat_id=chat_id, text=daily_text.strip(), parse_mode='HTML')
+                daily_text_parts.append(f"\\n\\n📈 <b>Period Summary:</b>")
+                daily_text_parts.append(f"  Total P&L: {avg_pnl_emoji} {total_pnl_all_days_str} | Avg Daily: {avg_daily_pnl_str}")
+                daily_text_parts.append(f"  Trading Days: {trading_days_count}/10 | Total Trades: {total_trades_all_days}")
+            else:
+                if not period_lines: # If there were no stat items at all, the header is the only part
+                    daily_text_parts = [daily_text_parts[0]] # Keep only the title
+                daily_text_parts.append("\\n\\n📉 No trading activity in the last 10 days.")
+
+            await context.bot.send_message(chat_id=chat_id, text="\\n".join(daily_text_parts).strip(), parse_mode='HTML')
                 
         except Exception as e:
             error_message = f"❌ Error processing daily command: {str(e)}"
@@ -969,34 +970,33 @@ class InfoCommands:
             
             if not weekly_stats_list:
                 await context.bot.send_message(chat_id=chat_id, text=
-                    "📊 <b>Weekly Performance</b>\n\n"
-                    "📭 No weekly performance data available yet.\n\n"
-                    "💡 Weekly stats are calculated from completed trades.\n"
-                    "Start trading to see weekly performance!",
+                    "📊 <b>Weekly Performance</b>\\n"
+                    "📭 No weekly performance data available yet.\\n"
+                    "💡 Weekly stats are calculated from completed trades. Start trading to see them!",
                     parse_mode='HTML'
                 )
                 return
             
-            weekly_text = "📊 <b>Weekly Performance (Last 10 Weeks)</b>\n\n"
+            weekly_text_parts = [f"📊 <b>Weekly Performance (Last 10 Weeks)</b>"]
             
-            total_pnl_all_weeks = 0 # Renamed
-            total_trades_all_weeks = 0 # Renamed
-            trading_weeks_count = 0 # Renamed
+            total_pnl_all_weeks = 0
+            total_trades_all_weeks = 0
+            trading_weeks_count = 0
             
-            for week_stats_item in weekly_stats_list: # Renamed
+            period_lines = []
+            for week_stats_item in weekly_stats_list:
                 if week_stats_item['has_trades']:
                     pnl_emoji = "🟢" if week_stats_item['pnl'] >= 0 else "🔴"
                     pnl_str = formatter.format_price_with_symbol(week_stats_item['pnl'])
-                    weekly_text += f"📈 <b>{week_stats_item['week_formatted']}</b>\n"
-                    weekly_text += f"   {pnl_emoji} P&L: {pnl_str} ({week_stats_item['pnl_pct']:+.1f}%)\n"
-                    weekly_text += f"   🔄 Trades: {week_stats_item['trades']}\n\n"
-                    
+                    period_lines.append(f"📈 <b>{week_stats_item['week_formatted']}</b>: {pnl_emoji} {pnl_str} ({week_stats_item['pnl_pct']:+.1f}%) | Trades: {week_stats_item['trades']}")
                     total_pnl_all_weeks += week_stats_item['pnl']
                     total_trades_all_weeks += week_stats_item['trades']
                     trading_weeks_count += 1
                 else:
-                    weekly_text += f"📈 <b>{week_stats_item['week_formatted']}</b>\n"
-                    weekly_text += f"   📭 No trading activity\n\n"
+                    period_lines.append(f"📈 <b>{week_stats_item['week_formatted']}</b>: 📭 No trading activity")
+
+            if period_lines:
+                weekly_text_parts.append("\\n".join(period_lines))
             
             if trading_weeks_count > 0:
                 avg_weekly_pnl = total_pnl_all_weeks / trading_weeks_count
@@ -1004,13 +1004,15 @@ class InfoCommands:
                 total_pnl_all_weeks_str = formatter.format_price_with_symbol(total_pnl_all_weeks)
                 avg_weekly_pnl_str = formatter.format_price_with_symbol(avg_weekly_pnl)
 
-                weekly_text += f"📅 <b>Period Summary:</b>\n"
-                weekly_text += f"   {avg_pnl_emoji} Total P&L: {total_pnl_all_weeks_str}\n"
-                weekly_text += f"   📊 Trading Weeks: {trading_weeks_count}/10\n"
-                weekly_text += f"   📅 Avg Weekly P&L: {avg_weekly_pnl_str}\n"
-                weekly_text += f"   🔄 Total Trades: {total_trades_all_weeks}\n"
+                weekly_text_parts.append(f"\\n\\n📅 <b>Period Summary:</b>")
+                weekly_text_parts.append(f"  Total P&L: {avg_pnl_emoji} {total_pnl_all_weeks_str} | Avg Weekly: {avg_weekly_pnl_str}")
+                weekly_text_parts.append(f"  Trading Weeks: {trading_weeks_count}/10 | Total Trades: {total_trades_all_weeks}")
+            else:
+                if not period_lines:
+                    weekly_text_parts = [weekly_text_parts[0]]
+                weekly_text_parts.append("\\n\\n📉 No trading activity in the last 10 weeks.")
             
-            await context.bot.send_message(chat_id=chat_id, text=weekly_text.strip(), parse_mode='HTML')
+            await context.bot.send_message(chat_id=chat_id, text="\\n".join(weekly_text_parts).strip(), parse_mode='HTML')
                 
         except Exception as e:
             error_message = f"❌ Error processing weekly command: {str(e)}"
@@ -1034,34 +1036,33 @@ class InfoCommands:
             
             if not monthly_stats_list:
                 await context.bot.send_message(chat_id=chat_id, text=
-                    "🗓️ <b>Monthly Performance</b>\n\n"
-                    "📭 No monthly performance data available yet.\n\n"
-                    "💡 Monthly stats are calculated from completed trades.\n"
-                    "Start trading to see monthly performance!",
+                    "🗓️ <b>Monthly Performance</b>\\n"
+                    "📭 No monthly performance data available yet.\\n"
+                    "💡 Monthly stats are calculated from completed trades. Start trading to see them!",
                     parse_mode='HTML'
                 )
                 return
             
-            monthly_text = "🗓️ <b>Monthly Performance (Last 12 Months)</b>\n\n"
+            monthly_text_parts = [f"🗓️ <b>Monthly Performance (Last 12 Months)</b>"]
             
-            total_pnl_all_months = 0 # Renamed
-            total_trades_all_months = 0 # Renamed
-            trading_months_count = 0 # Renamed
+            total_pnl_all_months = 0
+            total_trades_all_months = 0
+            trading_months_count = 0
             
-            for month_stats_item in monthly_stats_list: # Renamed
+            period_lines = []
+            for month_stats_item in monthly_stats_list:
                 if month_stats_item['has_trades']:
                     pnl_emoji = "🟢" if month_stats_item['pnl'] >= 0 else "🔴"
                     pnl_str = formatter.format_price_with_symbol(month_stats_item['pnl'])
-                    monthly_text += f"📅 <b>{month_stats_item['month_formatted']}</b>\n"
-                    monthly_text += f"   {pnl_emoji} P&L: {pnl_str} ({month_stats_item['pnl_pct']:+.1f}%)\n"
-                    monthly_text += f"   🔄 Trades: {month_stats_item['trades']}\n\n"
-                    
+                    period_lines.append(f"📅 <b>{month_stats_item['month_formatted']}</b>: {pnl_emoji} {pnl_str} ({month_stats_item['pnl_pct']:+.1f}%) | Trades: {month_stats_item['trades']}")
                     total_pnl_all_months += month_stats_item['pnl']
                     total_trades_all_months += month_stats_item['trades']
                     trading_months_count += 1
                 else:
-                    monthly_text += f"📅 <b>{month_stats_item['month_formatted']}</b>\n"
-                    monthly_text += f"   📭 No trading activity\n\n"
+                    period_lines.append(f"📅 <b>{month_stats_item['month_formatted']}</b>: 📭 No trading activity")
+
+            if period_lines:
+                monthly_text_parts.append("\\n".join(period_lines))
             
             if trading_months_count > 0:
                 avg_monthly_pnl = total_pnl_all_months / trading_months_count
@@ -1069,13 +1070,15 @@ class InfoCommands:
                 total_pnl_all_months_str = formatter.format_price_with_symbol(total_pnl_all_months)
                 avg_monthly_pnl_str = formatter.format_price_with_symbol(avg_monthly_pnl)
                 
-                monthly_text += f"📈 <b>Period Summary:</b>\n"
-                monthly_text += f"   {avg_pnl_emoji} Total P&L: {total_pnl_all_months_str}\n"
-                monthly_text += f"   📊 Trading Months: {trading_months_count}/12\n"
-                monthly_text += f"   🗓️ Avg Monthly P&L: {avg_monthly_pnl_str}\n"
-                monthly_text += f"   🔄 Total Trades: {total_trades_all_months}\n"
+                monthly_text_parts.append(f"\\n\\n📈 <b>Period Summary:</b>")
+                monthly_text_parts.append(f"  Total P&L: {avg_pnl_emoji} {total_pnl_all_months_str} | Avg Monthly: {avg_monthly_pnl_str}")
+                monthly_text_parts.append(f"  Trading Months: {trading_months_count}/12 | Total Trades: {total_trades_all_months}")
+            else:
+                if not period_lines:
+                    monthly_text_parts = [monthly_text_parts[0]]
+                monthly_text_parts.append("\\n\\n📉 No trading activity in the last 12 months.")
             
-            await context.bot.send_message(chat_id=chat_id, text=monthly_text.strip(), parse_mode='HTML')
+            await context.bot.send_message(chat_id=chat_id, text="\\n".join(monthly_text_parts).strip(), parse_mode='HTML')
                 
         except Exception as e:
             error_message = f"❌ Error processing monthly command: {str(e)}"