主力合约回测
This commit is contained in:
@@ -31,12 +31,13 @@ class ResultAnalyzer:
|
||||
Args:
|
||||
portfolio_snapshots (List[PortfolioSnapshot]): 回测引擎输出的投资组合快照列表。
|
||||
trade_history (List[Trade]): 回测引擎输出的交易历史记录列表。
|
||||
bars (List[Bar]): 回测引擎输出的所有K线数据列表 (可能包含多个合约的K线)。
|
||||
initial_capital (float): 初始资金。
|
||||
"""
|
||||
self.portfolio_snapshots = portfolio_snapshots
|
||||
self.trade_history = trade_history
|
||||
self.initial_capital = initial_capital
|
||||
self.bars = bars
|
||||
self.bars = bars # 接收所有K线数据
|
||||
self._metrics_cache: Optional[Dict[str, Any]] = None
|
||||
|
||||
print("\n--- 结果分析器初始化完成 ---")
|
||||
@@ -69,30 +70,41 @@ class ResultAnalyzer:
|
||||
print(f"{'夏普比率':<15}: {metrics['夏普比率']:.2f}")
|
||||
print(f"{'卡玛比率':<15}: {metrics['卡玛比率']:.2f}")
|
||||
print(f"{'总交易次数':<15}: {metrics['总交易次数']}")
|
||||
print(f"{'总实现盈亏':<15}: {metrics['总实现盈亏']:.2f}") # 新增
|
||||
print(f"{'交易成本':<15}: {metrics['交易成本']:.2f}")
|
||||
|
||||
# 新增交易相关详细指标,以适应更全面的交易分析需求
|
||||
print("\n--- 交易详情 ---")
|
||||
print(f"{'盈利交易次数':<15}: {metrics['盈利交易次数']}")
|
||||
print(f"{'亏损交易次数':<15}: {metrics['亏损交易次数']}")
|
||||
print(f"{'胜率':<15}: {metrics['胜率']:.2%}")
|
||||
print(f"{'盈亏比':<15}: {metrics['盈亏比']:.2f}")
|
||||
print(f"{'平均每次盈利':<15}: {metrics['平均每次盈利']:.2f}")
|
||||
print(f"{'平均每次亏损':<15}: {metrics['平均每次亏损']:.2f}")
|
||||
|
||||
|
||||
if self.trade_history:
|
||||
print("\n--- 部分交易明细 (最近5笔) ---")
|
||||
for trade in self.trade_history[-5:]:
|
||||
# 调整输出格式,显示实现盈亏
|
||||
pnl_display = f" | PnL: {trade.realized_pnl:.2f}" if trade.is_close_trade else ""
|
||||
print(
|
||||
f" {trade.fill_time} | {trade.direction:<10} | {trade.symbol} | Vol: {trade.volume} | Price: {trade.price:.2f} | Commission: {trade.commission:.2f}"
|
||||
f" {trade.fill_time} | {trade.direction:<10} | {trade.symbol} | Vol: {trade.volume} | Price: {trade.price:.2f} | Comm: {trade.commission:.2f}{pnl_display}"
|
||||
)
|
||||
else:
|
||||
print("\n没有交易记录。")
|
||||
|
||||
def plot_performance(self) -> None:
|
||||
"""
|
||||
绘制投资组合净值和回撤曲线。
|
||||
绘制投资组合净值和回撤曲线,以及所有合约的收盘价曲线。
|
||||
"""
|
||||
print("正在绘制绩效图表...")
|
||||
# plot_performance_chart(self.portfolio_snapshots, self.initial_capital, self.bars)
|
||||
plot_equity_and_drawdown_chart(
|
||||
self.portfolio_snapshots,
|
||||
self.initial_capital,
|
||||
title="Portfolio Equity and Drawdown Curve",
|
||||
title="Portfolio Equity and Drawdown Curve (All Contracts)" # 明确标题,表明是整体曲线
|
||||
)
|
||||
|
||||
# 绘制单独的收盘价曲线
|
||||
plot_close_price_chart(self.bars, title="Underlying Asset Close Price")
|
||||
|
||||
print("图表绘制完成。")
|
||||
# 绘制所有处理过的K线收盘价曲线
|
||||
plot_close_price_chart(self.bars, title="Underlying Asset Close Price (Concatenated Bars)") # 明确标题
|
||||
print("图表绘制完成。")
|
||||
Reference in New Issue
Block a user