123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #!/usr/bin/env python3
- """
- Test script to verify the stats command fix
- """
- import sys
- from pathlib import Path
- # Add the project root and src directory to the path
- project_root = Path(__file__).parent.parent
- sys.path.insert(0, str(project_root))
- sys.path.insert(0, str(project_root / 'src'))
- from src.stats import TradingStats
- def test_stats_fix():
- """Test that stats work with no trades and with trades."""
- print("🧪 Testing Stats Fix")
- print("=" * 40)
-
- try:
- # Test 1: New stats instance (no trades)
- print("📊 Test 1: Empty stats (no trades)")
- stats = TradingStats(stats_file="test_stats.json")
-
- # Try to format stats message
- message = stats.format_stats_message(1000.0)
- print("✅ Empty stats message generated successfully")
- print(f"📝 Message length: {len(message)} characters")
-
- # Test 2: Performance stats with no trades
- print("\n📊 Test 2: Performance stats with no trades")
- perf_stats = stats.get_performance_stats()
-
- # Check that expectancy key exists
- if 'expectancy' in perf_stats:
- print(f"✅ Expectancy key exists: {perf_stats['expectancy']}")
- else:
- print("❌ Expectancy key missing!")
- return False
-
- # Check all required keys
- required_keys = [
- 'win_rate', 'profit_factor', 'avg_win', 'avg_loss',
- 'largest_win', 'largest_loss', 'consecutive_wins',
- 'consecutive_losses', 'total_wins', 'total_losses', 'expectancy'
- ]
-
- missing_keys = [key for key in required_keys if key not in perf_stats]
- if missing_keys:
- print(f"❌ Missing keys: {missing_keys}")
- return False
- else:
- print("✅ All required performance keys present")
-
- # Test 3: Add some sample trades
- print("\n📊 Test 3: Stats with sample trades")
- stats.record_trade("BTC/USDC:USDC", "buy", 0.001, 45000.0, "test1")
- stats.record_trade("BTC/USDC:USDC", "sell", 0.001, 46000.0, "test2")
-
- # Try to format stats message with trades
- message_with_trades = stats.format_stats_message(1010.0)
- print("✅ Stats message with trades generated successfully")
-
- # Test performance stats with trades
- perf_stats_with_trades = stats.get_performance_stats()
- print(f"✅ Win rate: {perf_stats_with_trades['win_rate']:.1f}%")
- print(f"✅ Expectancy: ${perf_stats_with_trades['expectancy']:.2f}")
-
- # Test 4: Error handling
- print("\n📊 Test 4: Error handling")
- try:
- # This should not fail due to the safe .get() access
- comprehensive_stats = stats.get_comprehensive_stats(1010.0)
- print("✅ Comprehensive stats generated successfully")
- except Exception as e:
- print(f"❌ Comprehensive stats failed: {e}")
- return False
-
- print("\n🎉 All stats tests passed!")
- print("\n📝 Sample stats message:")
- print("-" * 40)
- print(message[:200] + "..." if len(message) > 200 else message)
- print("-" * 40)
-
- # Clean up test file
- import os
- try:
- os.remove("test_stats.json")
- print("\n🧹 Test file cleaned up")
- except:
- pass
-
- return True
-
- except Exception as e:
- print(f"💥 Test failed with error: {e}")
- import traceback
- traceback.print_exc()
- return False
- if __name__ == "__main__":
- success = test_stats_fix()
-
- if success:
- print("\n🎉 Stats fix test PASSED!")
- print("\n✅ The /stats command should now work properly")
- sys.exit(0)
- else:
- print("\n💥 Stats fix test FAILED!")
- sys.exit(1)
|