import json from datetime import datetime import sys file_path = '/Users/vivian/work/python/auto_trade_sys/交易记录_2026-02-05T11-39-32.json' def parse_time(time_str): if not time_str: return None try: # Adjust format based on actual JSON content if needed # Assuming ISO format or similar based on filename return datetime.fromisoformat(time_str.replace('Z', '+00:00')) except ValueError: return None try: with open(file_path, 'r', encoding='utf-8') as f: data = json.load(f) closed_trades = [t for t in data if t.get('状态') == '已平仓'] total_trades = len(data) closed_count = len(closed_trades) print(f"Total Trades in File: {total_trades}") print(f"Closed Trades: {closed_count}") if closed_count == 0: print("No closed trades to analyze.") sys.exit(0) wins = [t for t in closed_trades if float(t.get('盈亏', 0)) > 0] losses = [t for t in closed_trades if float(t.get('盈亏', 0)) <= 0] total_pnl = sum(float(t.get('盈亏', 0)) for t in closed_trades) total_win_pnl = sum(float(t.get('盈亏', 0)) for t in wins) total_loss_pnl = sum(float(t.get('盈亏', 0)) for t in losses) avg_win = total_win_pnl / len(wins) if wins else 0 avg_loss = total_loss_pnl / len(losses) if losses else 0 print(f"\n--- Performance Analysis ---") print(f"Win Count: {len(wins)}") print(f"Loss Count: {len(losses)}") win_rate = (len(wins) / closed_count * 100) if closed_count > 0 else 0 print(f"Win Rate: {win_rate:.2f}%") print(f"Total PnL: {total_pnl:.4f} USDT") print(f"Avg Win: {avg_win:.4f} USDT") print(f"Avg Loss: {avg_loss:.4f} USDT") if avg_loss != 0: rr_ratio = abs(avg_win / avg_loss) print(f"Avg Win / Avg Loss Ratio (R:R): {rr_ratio:.2f}") else: print("Avg Win / Avg Loss Ratio (R:R): Infinite (No Losses)") # Duration Analysis (if fields exist) # Assuming fields like '开仓时间' and '平仓时间' exist based on typical trade records # If not, this part will be skipped or adjusted # Let's inspect one record keys to be sure for future reference if closed_trades: print(f"\nSample Record Keys: {list(closed_trades[0].keys())}") except Exception as e: print(f"Error: {e}") import traceback traceback.print_exc()