增加vectorbt绘图

This commit is contained in:
2025-11-01 16:10:01 +08:00
parent a66fcd0da9
commit 458da08b84

View File

@ -293,9 +293,11 @@ try:
print("\n绘制组合总价值变化...") print("\n绘制组合总价值变化...")
try: try:
fig = portfolio_value.vbt.plot( fig = portfolio_value.vbt.plot(
title='配对交易组合总价值', title='配对交易组合总价值'
xlabel='日期', )
ylabel='组合价值' fig.update_layout(
xaxis_title='日期',
yaxis_title='组合价值'
) )
fig.show() fig.show()
except Exception as e: except Exception as e:
@ -309,9 +311,11 @@ try:
if hasattr(cumulative_returns, 'columns') and len(cumulative_returns.columns) > 1: if hasattr(cumulative_returns, 'columns') and len(cumulative_returns.columns) > 1:
cumulative_returns = cumulative_returns.mean(axis=1) cumulative_returns = cumulative_returns.mean(axis=1)
fig = cumulative_returns.vbt.plot( fig = cumulative_returns.vbt.plot(
title='配对交易累积收益率', title='配对交易累积收益率'
xlabel='日期', )
ylabel='累积收益' fig.update_layout(
xaxis_title='日期',
yaxis_title='累积收益'
) )
fig.show() fig.show()
except Exception as e: except Exception as e:
@ -324,9 +328,11 @@ try:
if hasattr(drawdown, 'columns') and len(drawdown.columns) > 1: if hasattr(drawdown, 'columns') and len(drawdown.columns) > 1:
drawdown = drawdown.mean(axis=1) drawdown = drawdown.mean(axis=1)
fig = drawdown.vbt.plot( fig = drawdown.vbt.plot(
title='配对交易回撤分析', title='配对交易回撤分析'
xlabel='日期', )
ylabel='回撤' fig.update_layout(
xaxis_title='日期',
yaxis_title='回撤'
) )
fig.show() fig.show()
except Exception as e: except Exception as e:
@ -340,10 +346,12 @@ try:
if hasattr(monthly_returns, 'columns') and len(monthly_returns.columns) > 1: if hasattr(monthly_returns, 'columns') and len(monthly_returns.columns) > 1:
monthly_returns = monthly_returns.mean(axis=1) monthly_returns = monthly_returns.mean(axis=1)
fig = monthly_returns.vbt.heatmap( fig = monthly_returns.vbt.heatmap(
xaxis_title='年份',
yaxis_title='月份',
title='配对交易月度收益热力图' title='配对交易月度收益热力图'
) )
fig.update_layout(
xaxis_title='年份',
yaxis_title='月份'
)
fig.show() fig.show()
except Exception as e: except Exception as e:
print(f"月度收益热力图绘制失败: {e}") print(f"月度收益热力图绘制失败: {e}")
@ -396,18 +404,22 @@ try:
# 中芯国际持仓 # 中芯国际持仓
holdings_smic = portfolio['00981'].holdings holdings_smic = portfolio['00981'].holdings
fig1 = holdings_smic.vbt.plot( fig1 = holdings_smic.vbt.plot(
title='中芯国际持仓变化', title='中芯国际持仓变化'
xlabel='日期', )
ylabel='持仓价值' fig1.update_layout(
xaxis_title='日期',
yaxis_title='持仓价值'
) )
fig1.show() fig1.show()
# 华虹半导体持仓 # 华虹半导体持仓
holdings_hhic = portfolio['01347'].holdings holdings_hhic = portfolio['01347'].holdings
fig2 = holdings_hhic.vbt.plot( fig2 = holdings_hhic.vbt.plot(
title='华虹半导体持仓变化', title='华虹半导体持仓变化'
xlabel='日期', )
ylabel='持仓价值' fig2.update_layout(
xaxis_title='日期',
yaxis_title='持仓价值'
) )
fig2.show() fig2.show()
except Exception as e: except Exception as e:
@ -421,14 +433,35 @@ try:
if hasattr(cash, 'columns') and len(cash.columns) > 1: if hasattr(cash, 'columns') and len(cash.columns) > 1:
cash = cash.iloc[:, 0] cash = cash.iloc[:, 0]
fig = cash.vbt.plot( fig = cash.vbt.plot(
title='配对交易现金余额变化', title='配对交易现金余额变化'
xlabel='日期', )
ylabel='现金余额' fig.update_layout(
xaxis_title='日期',
yaxis_title='现金余额'
) )
fig.show() fig.show()
except Exception as e: except Exception as e:
print(f"现金变化绘制失败: {e}") print(f"现金变化绘制失败: {e}")
# 10. 资产配置比例
print("\n绘制资产配置比例...")
try:
# 计算各资产权重
asset_value = portfolio.asset_value()
total_value = portfolio.value()
weights = asset_value.div(total_value, axis=0).fillna(0)
fig = weights.vbt.areaplot(
title='资产配置比例变化'
)
fig.update_layout(
xaxis_title='日期',
yaxis_title='权重'
)
fig.show()
except Exception as e:
print(f"资产配置比例绘制失败: {e}")
# 显示交易记录 # 显示交易记录
non_zero_size = size[(size != 0).any(axis=1)] non_zero_size = size[(size != 0).any(axis=1)]
print(f"\n非零交易数量: {len(non_zero_size)}") print(f"\n非零交易数量: {len(non_zero_size)}")