test_positions_display.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python3
  2. """
  3. Test script to demonstrate the enhanced positions display with P&L percentages.
  4. """
  5. def demo_positions_display():
  6. """Demo what the enhanced positions message will look like."""
  7. print("📈 Enhanced Positions Display Demo")
  8. print("=" * 50)
  9. # Mock position data
  10. positions = [
  11. {
  12. 'symbol': 'BTC/USDC:USDC',
  13. 'contracts': 0.05, # Long position
  14. 'entryPx': 45000,
  15. 'unrealizedPnl': 1250
  16. },
  17. {
  18. 'symbol': 'ETH/USDC:USDC',
  19. 'contracts': -2.0, # Short position
  20. 'entryPx': 3000,
  21. 'unrealizedPnl': -150
  22. },
  23. {
  24. 'symbol': 'SOL/USDC:USDC',
  25. 'contracts': 10.0, # Long position
  26. 'entryPx': 100,
  27. 'unrealizedPnl': 75
  28. }
  29. ]
  30. print("📈 <b>Open Positions</b>\n")
  31. total_unrealized = 0
  32. total_position_value = 0
  33. for position in positions:
  34. symbol = position['symbol']
  35. contracts = float(position['contracts'])
  36. unrealized_pnl = float(position['unrealizedPnl'])
  37. entry_price = float(position['entryPx'])
  38. # Calculate position value and P&L percentage
  39. position_value = abs(contracts) * entry_price
  40. pnl_percentage = (unrealized_pnl / position_value * 100) if position_value > 0 else 0
  41. pnl_emoji = "🟢" if unrealized_pnl >= 0 else "🔴"
  42. # Extract token name for cleaner display
  43. token = symbol.split('/')[0] if '/' in symbol else symbol
  44. position_type = "LONG" if contracts > 0 else "SHORT"
  45. print(f"📊 {token} ({position_type})")
  46. print(f" 📏 Size: {abs(contracts):.6f} {token}")
  47. print(f" 💰 Entry: ${entry_price:,.2f}")
  48. print(f" 💵 Value: ${position_value:,.2f}")
  49. print(f" {pnl_emoji} P&L: ${unrealized_pnl:,.2f} ({pnl_percentage:+.2f}%)")
  50. print()
  51. total_unrealized += unrealized_pnl
  52. total_position_value += position_value
  53. # Calculate overall P&L percentage
  54. total_pnl_percentage = (total_unrealized / total_position_value * 100) if total_position_value > 0 else 0
  55. total_pnl_emoji = "🟢" if total_unrealized >= 0 else "🔴"
  56. print(f"💼 Total Portfolio:")
  57. print(f" 💵 Total Value: ${total_position_value:,.2f}")
  58. print(f" {total_pnl_emoji} Total P&L: ${total_unrealized:,.2f} ({total_pnl_percentage:+.2f}%)")
  59. def demo_comparison():
  60. """Show before vs after comparison."""
  61. print("\n📊 Before vs After Comparison")
  62. print("=" * 50)
  63. print("BEFORE (old format):")
  64. print("📊 BTC/USDC:USDC")
  65. print(" 📏 Size: 0.05 contracts")
  66. print(" 💰 Entry: $45,000.00")
  67. print(" 🟢 PnL: $1,250.00")
  68. print()
  69. print("💼 Total Unrealized P&L: $1,175.00")
  70. print("\nAFTER (enhanced format):")
  71. print("📊 BTC (LONG)")
  72. print(" 📏 Size: 0.050000 BTC")
  73. print(" 💰 Entry: $45,000.00")
  74. print(" 💵 Value: $2,250.00")
  75. print(" 🟢 P&L: $1,250.00 (+55.56%)")
  76. print()
  77. print("💼 Total Portfolio:")
  78. print(" 💵 Total Value: $8,250.00")
  79. print(" 🟢 Total P&L: $1,175.00 (+14.24%)")
  80. if __name__ == "__main__":
  81. print("🚀 Enhanced Positions Display Test")
  82. print("=" * 60)
  83. demo_positions_display()
  84. demo_comparison()
  85. print("\n" + "=" * 60)
  86. print("✅ Enhanced features:")
  87. print("• Shows position direction (LONG/SHORT)")
  88. print("• Displays token names instead of full symbols")
  89. print("• Shows position size in token units")
  90. print("• Adds position value for context")
  91. print("• P&L shown as both $ and % for easy assessment")
  92. print("• Total portfolio value and P&L percentage")
  93. print("• Clean, mobile-friendly formatting")