diagnose_root_cause.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import pandas as pd
  2. df = pd.read_csv('cyb50_optimized_trades_2017-06-01_2026-01-19.csv')
  3. print("盈亏计算根本问题分析:")
  4. print("=" * 80)
  5. # 逐步验证第一笔交易
  6. trade1 = df.iloc[0]
  7. print("第一笔交易详细分析:")
  8. print(f"买入价格: {trade1['买入价格']}")
  9. print(f"卖出价格: {trade1['卖出价格']}")
  10. print(f"持仓数量: {trade1['持仓数量']}")
  11. print(f"开仓市值: {trade1['开仓市值']}")
  12. print(f"平仓市值: {trade1['平仓市值']}")
  13. # 计算各种成本
  14. entry_cost = trade1['开仓市值'] * 0.0004 # 开仓成本
  15. exit_cost = trade1['平仓市值'] * 0.0004 # 卖出成本
  16. print(f"开仓成本: {entry_cost:.2f}元")
  17. print(f"卖出成本: {exit_cost:.2f}元")
  18. print(f"总成本: {entry_cost + exit_cost:.2f}元")
  19. # 计算毛盈亏
  20. gross_pnl = (trade1['卖出价格'] - trade1['买入价格']) * trade1['持仓数量']
  21. print(f"毛盈亏: {gross_pnl:.2f}元")
  22. # 计算净盈亏
  23. net_pnl = gross_pnl - entry_cost - exit_cost
  24. print(f"净盈亏: {net_pnl:.2f}元")
  25. print(f"记录盈亏: {trade1['盈亏金额']:.2f}元")
  26. print(f"差异: {abs(net_pnl - trade1['盈亏金额']):.2f}元")
  27. # 资金流动分析
  28. print(f"\n资金流动分析:")
  29. print(f"开仓时总资金: {trade1['开仓时总资金']:.2f}元")
  30. print(f"平仓时总资金: {trade1['平仓时总资金']:.2f}元")
  31. print(f"实际资金变化: {trade1['平仓时总资金'] - trade1['开仓时总资金']:.2f}元")
  32. # 如果第一笔交易应该的资金变化
  33. expected_final = trade1['开仓时总资金'] + net_pnl
  34. print(f"预期平仓资金: {expected_final:.2f}元 (开仓资金 + 净盈亏)")
  35. print(f"实际平仓资金: {trade1['平仓时总资金']:.2f}元")
  36. print(f"资金差异: {expected_final - trade1['平仓时总资金']:.2f}元")
  37. print(f"\n结论:")
  38. if abs(net_pnl - trade1['盈亏金额']) < 200:
  39. print("第一笔交易盈亏计算基本正确")
  40. else:
  41. print(f"第一笔交易盈亏计算仍有问题: 记录{trade1['盈亏金额']:.2f} vs 计算{net_pnl:.2f}")
  42. # 检查是否所有交易都有类似问题
  43. total_recorded_pnl = df['盈亏金额'].sum()
  44. total_actual_pnl = 0
  45. for i, row in df.iterrows():
  46. gross_pnl_i = (row['卖出价格'] - row['买入价格']) * row['持仓数量']
  47. entry_cost_i = row['开仓市值'] * 0.0004
  48. exit_cost_i = row['平仓市值'] * 0.0004
  49. net_pnl_i = gross_pnl_i - entry_cost_i - exit_cost_i
  50. total_actual_pnl += net_pnl_i
  51. print(f"\n全市场验证:")
  52. print(f"记录累计盈亏: {total_recorded_pnl:.2f}元")
  53. print(f"计算累计盈亏: {total_actual_pnl:.2f}元")
  54. print(f"差异: {abs(total_recorded_pnl - total_actual_pnl):.2f}元")