diagnose_report.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 诊断报告问题
  5. """
  6. import sys
  7. sys.path.insert(0, '/root/.openclaw/workspace/cat-fly')
  8. sys.path.insert(0, '/root/.openclaw/workspace/quant')
  9. from trend_report_fixed import TrendTrackingStrategy
  10. strategy = TrendTrackingStrategy()
  11. strategy.load_and_merge_data()
  12. strategy.calculate_indicators()
  13. result = strategy.backtest()
  14. trades = result['trades']
  15. print('='*70)
  16. print('交易记录诊断')
  17. print('='*70)
  18. print(f"\n总交易记录数: {len(trades)}")
  19. print(f"第一条交易: {trades[0]['date'].strftime('%Y-%m-%d')} {trades[0]['action']}")
  20. print(f"最后一条交易: {trades[-1]['date'].strftime('%Y-%m-%d')} {trades[-1]['action']}")
  21. print("\n前5条交易:")
  22. for i, t in enumerate(trades[:5], 1):
  23. print(f"{i}. {t['date'].strftime('%Y-%m-%d')} {t['action']}")
  24. print("\n后5条交易:")
  25. for i, t in enumerate(trades[-5:], len(trades)-4):
  26. print(f"{i}. {t['date'].strftime('%Y-%m-%d')} {t['action']}")
  27. print("\n" + "="*70)
  28. print('检查 get_recent_trades(20) 返回的内容:')
  29. print("="*70)
  30. recent = strategy.get_recent_trades(20)
  31. for i, t in enumerate(recent, 1):
  32. date_str = t['date'].strftime('%Y-%m-%d')
  33. action = t['action']
  34. price = t['price']
  35. if action == 'SELL':
  36. ret = t.get('return_pct', 0)
  37. print(f"{i:2d}. {date_str} {action:4s} @ {price:8.2f} ({ret:+.2f}%)")
  38. else:
  39. print(f"{i:2d}. {date_str} {action:4s} @ {price:8.2f}")
  40. print("\n" + "="*70)
  41. print('配对检查 - 最近10对交易:')
  42. print("="*70)
  43. # 从 recent 中配对
  44. pair_count = 0
  45. i = 0
  46. while i < len(recent) - 1:
  47. if recent[i]['action'] == 'BUY' and recent[i+1]['action'] == 'SELL':
  48. pair_count += 1
  49. buy = recent[i]
  50. sell = recent[i+1]
  51. hold_days = (sell['date'] - buy['date']).days
  52. ret = sell.get('return_pct', 0)
  53. print(f"{pair_count}. 买 {buy['date'].strftime('%Y-%m-%d')} → 卖 {sell['date'].strftime('%Y-%m-%d')} | {ret:+.2f}% | {hold_days}天")
  54. i += 2
  55. else:
  56. i += 1
  57. print(f"\n找到 {pair_count} 对完整交易")