auto_trade_sys/analyze_losses.py
薇薇安 cb5f513904 1
2026-02-13 18:32:06 +08:00

90 lines
3.6 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from datetime import datetime
# Load trading history
try:
with open('/Users/vivian/work/python/auto_trade_sys/交易记录_2026-02-13T10-01-03.json', 'r') as f:
trades = json.load(f)
except FileNotFoundError:
print("Error: Trading history file not found.")
trades = []
# Load current positions
try:
with open('/Users/vivian/work/python/auto_trade_sys/持仓记录_2026-02-13T10-01-38.json', 'r') as f:
positions = json.load(f)
except FileNotFoundError:
print("Error: Current positions file not found.")
positions = []
print("=== 亏损分析报告 (Loss Analysis Report) ===")
print(f"分析时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("-" * 50)
# 1. Analyze Closed Trades (Focus on Losses)
print("\n[已平仓亏损分析 (Closed Losing Trades)]")
loss_trades = [t for t in trades if t.get('盈亏', 0) < 0]
if not loss_trades:
print("今天没有亏损交易 (No losing trades today).")
else:
for t in loss_trades:
symbol = t.get('交易对')
pnl = t.get('盈亏', 0)
pnl_pct = t.get('盈亏比例', 0)
margin = t.get('保证金', 0)
leverage = t.get('杠杆', 0)
exit_reason = t.get('离场原因', 'unknown')
# Calculate expected stop loss based on old config (2.5% price move * leverage)
expected_sl_pct = 2.5 * leverage
print(f"交易对: {symbol}")
print(f" 盈亏: {pnl:.2f} USDT ({pnl_pct:.2f}%)")
print(f" 保证金: {margin:.2f} USDT")
print(f" 杠杆: {leverage}x")
print(f" 离场原因: {exit_reason}")
if abs(pnl_pct) >= 15:
print(f" ⚠️ 严重亏损 (Severe Loss): >15% margin loss")
if abs(pnl_pct) >= expected_sl_pct - 5 and abs(pnl_pct) <= expected_sl_pct + 10:
print(f" 原因推测: 旧配置 MIN_STOP_LOSS_PRICE_PCT=2.5% (预期亏损 ~{expected_sl_pct}%)")
print("-" * 30)
# 2. Analyze Current Positions
print("\n[当前持仓分析 (Current Positions Analysis)]")
if not positions:
print("当前无持仓 (No active positions).")
else:
high_risk_positions = []
for p in positions:
symbol = p.get('symbol')
pnl = p.get('pnl', 0)
pnl_pct = p.get('pnl_percent', 0)
leverage = p.get('leverage', 0)
print(f"交易对: {symbol}")
print(f" 当前盈亏: {pnl:.2f} USDT ({pnl_pct:.2f}%)")
print(f" 杠杆: {leverage}x")
# Check against new 10% risk limit
if pnl_pct <= -10:
print(f" 🔴 建议平仓 (Recommended Close): 亏损超过 10% (新配置限制)")
high_risk_positions.append(symbol)
elif pnl_pct <= -5:
print(f" 🟠 风险警告 (Warning): 亏损接近 10%")
else:
print(f" 🟢 正常持有 (Holding)")
print("-" * 30)
if high_risk_positions:
print(f"\n🚨 紧急建议: 请立即检查并考虑平仓以下 {len(high_risk_positions)} 个高风险持仓:")
print(f" {', '.join(high_risk_positions)}")
else:
print("\n✅ 所有持仓风险在可控范围内 (<10% 亏损).")
print("\n=== 结论 (Conclusion) ===")
print("1. 今天的严重亏损 (-15% ~ -40%) 主要是由于旧配置 'MIN_STOP_LOSS_PRICE_PCT = 2.5%' 导致的。")
print(" 在 8x-10x 杠杆下2.5% 的价格波动会导致 20%-25% 的本金亏损。")
print("2. 新配置 (0.5% 最小止损) 已生效,未来交易的止损将控制在 ~5% - 10% 本金亏损。")
print("3. 建议手动平仓当前亏损超过 10% 的老订单,以避免进一步扩大损失。")