check_trades.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 检查交易明细准确性
  5. """
  6. import pandas as pd
  7. import sys
  8. sys.path.insert(0, '/root/.openclaw/workspace/cat-fly')
  9. from trend_report_multi import TrendTrackingStrategy
  10. strategy = TrendTrackingStrategy()
  11. strategy.load_and_merge_data()
  12. strategy.calculate_indicators()
  13. result = strategy.backtest()
  14. print("="*60)
  15. print("📋 交易明细核对")
  16. print("="*60)
  17. print(f"\n总交易记录数: {len(result['trades'])}")
  18. print(f"买入次数: {len([t for t in result['trades'] if t['action'] == 'BUY'])}")
  19. print(f"卖出次数: {len([t for t in result['trades'] if t['action'] == 'SELL'])}")
  20. print("\n" + "-"*60)
  21. print("最近20条交易记录:")
  22. print("-"*60)
  23. for i, trade in enumerate(result['trades'][-20:], 1):
  24. date_str = trade['date'].strftime('%Y-%m-%d') if hasattr(trade['date'], 'strftime') else str(trade['date'])
  25. if trade['action'] == 'BUY':
  26. print(f"{i:2d}. {date_str} BUY @ {trade['price']:8.2f} 资产: {trade['capital']:12,.0f}")
  27. else:
  28. pnl = trade.get('pnl', 0)
  29. ret = trade.get('return_pct', 0)
  30. print(f"{i:2d}. {date_str} SELL @ {trade['price']:8.2f} 盈亏: {ret:+6.2f}% 资产: {trade['capital']:12,.0f}")
  31. # 检查是否有同一天买卖的情况
  32. print("\n" + "-"*60)
  33. print("检查异常交易:")
  34. print("-"*60)
  35. for i in range(len(result['trades']) - 1):
  36. t1 = result['trades'][i]
  37. t2 = result['trades'][i+1]
  38. if t1['action'] == 'BUY' and t2['action'] == 'SELL':
  39. hold_days = (t2['date'] - t1['date']).days
  40. if hold_days < 0:
  41. print(f"❌ 日期错误: 卖出日期早于买入日期!")
  42. print(f" 买入: {t1['date']}, 卖出: {t2['date']}")
  43. elif hold_days == 0:
  44. print(f"⚠️ T+0交易: {t1['date'].strftime('%Y-%m-%d')}")
  45. print("\n检查完成!")