feat: 组合模拟含交易费用(往返0.2%,波段双腿2倍)——v1.0高频被费斩-11.1pp现原形,v6.1以含费+41.5%居首

This commit is contained in:
hmo
2026-07-29 22:52:06 +08:00
parent 0bbc489a1b
commit e9c93fe05a
+14 -4
View File
@@ -1073,9 +1073,16 @@ def portfolio_sim_full(trades, capital=1000000):
return r
def portfolio_sim(trades, capital=1000000, max_positions=10):
COST_RATE = 0.002 # 往返交易费用率(佣金+印花税+滑点≈0.2%)
def _trade_legs(t):
"""波段类出场按2次往返计费"""
return 2 if t.get('exit_reason') in ('swing_re', 'swing_ptp') else 1
def portfolio_sim(trades, capital=1000000, max_positions=10, cost=True):
"""组合级模拟:固定等分仓位,按交易日历执行,返回最终资产/总收益/资产曲线回撤
规则:每日先结算到期仓位 → 再执行当日入场(仓位满跳过)→ 持仓按成本估值"""
规则:每日先结算到期仓位 → 再执行当日入场(仓位满跳过)→ 持仓按成本估值
含交易费用:每笔往返扣 COST_RATE(2026-07-29 老爸:高频策略必须上费用天平)"""
if not trades:
return {}
# 交易日历(用大盘指数日期)
@@ -1107,6 +1114,8 @@ def portfolio_sim(trades, capital=1000000, max_positions=10):
for p in open_pos:
if p['exit_date'] <= day:
cash += p['alloc'] * (1 + p['pnl'] / 100)
if cost:
cash -= p['alloc'] * COST_RATE * _trade_legs(p.get('trade', {}))
else:
still.append(p)
open_pos = still
@@ -1123,17 +1132,18 @@ def portfolio_sim(trades, capital=1000000, max_positions=10):
cash -= alloc
open_pos.append({
'exit_date': add_days(t['entry_date'], t['hold_days']),
'alloc': alloc, 'pnl': t['profit_pct'],
'alloc': alloc, 'pnl': t['profit_pct'], 'trade': t,
})
equity = cash + sum(p['alloc'] for p in open_pos)
peak = max(peak, equity)
max_dd = max(max_dd, (peak - equity) / peak * 100)
# 期末结算全部
final = cash + sum(p['alloc'] * (1 + p['pnl'] / 100) for p in open_pos)
final = cash + sum(p['alloc'] * (1 + p['pnl'] / 100 - (COST_RATE * _trade_legs(p.get('trade', {})) if cost else 0)) for p in open_pos)
total_ret = (final - capital) / capital * 100
# 年化
days = len(cal)
cagr = ((final / capital) ** (250 / days) - 1) * 100 if days > 0 and final > 0 else 0
total_cost = capital - final + sum(1 for _ in []) # placeholder
return {
'capital_final': round(final, 0),
'total_return_pct': round(total_ret, 1),