删除不必要的图表
This commit is contained in:
@ -55,7 +55,7 @@ print(f"华虹半导体原始数据时间范围: {hhic_data.index.min()} 到 {hh
|
||||
|
||||
# 限制为最近半年数据
|
||||
end_date = smic_data.index.max()
|
||||
start_date = end_date - pd.Timedelta(days=365)
|
||||
start_date = end_date - pd.Timedelta(days=360) # 最近半年
|
||||
|
||||
print(f"\n限制回测时间范围: {start_date} 到 {end_date}")
|
||||
|
||||
@ -197,11 +197,34 @@ try:
|
||||
# 获取组合总价值
|
||||
portfolio_value = portfolio.value()
|
||||
|
||||
|
||||
# ========== 使用vectorbt进行专业分析 ==========
|
||||
print("\n=== VectorBT 专业分析(基于价格比率) ===")
|
||||
|
||||
# 修复:使用正确的列选择方法
|
||||
try:
|
||||
# 选择第一列(也是唯一的一列)
|
||||
portfolio_single = portfolio['RATIO']
|
||||
|
||||
print(portfolio_single.stats())
|
||||
|
||||
fig = portfolio_single.plot(subplots=[
|
||||
'orders', # 订单
|
||||
'trade_pnl', # 交易盈亏
|
||||
'cum_returns', # 累积收益
|
||||
'drawdowns' # 回撤
|
||||
])
|
||||
fig.update_layout(
|
||||
title='基于价格比率的配对交易详细分析',
|
||||
height=800
|
||||
)
|
||||
fig.show()
|
||||
except Exception as e:
|
||||
print(f"详细分析绘制失败: {e}")
|
||||
|
||||
# ========== 绘制基于比率的技术分析图表 ==========
|
||||
print("\n绘制基于比率的技术分析图表...")
|
||||
|
||||
portfolio['RATIO'].plot().show()
|
||||
|
||||
# 创建详细的技术分析图表
|
||||
fig, axes = plt.subplots(4, 1, figsize=(15, 16))
|
||||
|
||||
@ -236,7 +259,7 @@ try:
|
||||
axes[1].set_ylim(-1.5, 1.5)
|
||||
axes[1].legend()
|
||||
axes[1].grid(True, alpha=0.3)
|
||||
|
||||
|
||||
|
||||
# 4. 组合净值
|
||||
if len(portfolio_value) > 0:
|
||||
@ -251,85 +274,12 @@ try:
|
||||
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
# ========== 使用vectorbt进行专业分析 ==========
|
||||
print("\n=== VectorBT 专业分析(基于价格比率) ===")
|
||||
|
||||
# 1. 组合详细分析
|
||||
try:
|
||||
fig = portfolio.plot(subplots=[
|
||||
'orders', # 订单
|
||||
'trade_pnl', # 交易盈亏
|
||||
'cum_returns', # 累积收益
|
||||
'drawdowns' # 回撤
|
||||
])
|
||||
fig.update_layout(
|
||||
title='基于价格比率的配对交易详细分析',
|
||||
height=800
|
||||
)
|
||||
fig.show()
|
||||
except Exception as e:
|
||||
print(f"详细分析绘制失败: {e}")
|
||||
|
||||
# 2. 组合价值变化
|
||||
try:
|
||||
fig = portfolio_value.vbt.plot(
|
||||
title='基于价格比率的配对交易组合价值'
|
||||
)
|
||||
fig.update_layout(
|
||||
xaxis_title='日期',
|
||||
yaxis_title='组合价值'
|
||||
)
|
||||
fig.show()
|
||||
except Exception as e:
|
||||
print(f"组合价值变化绘制失败: {e}")
|
||||
|
||||
# 3. 累积收益
|
||||
try:
|
||||
cumulative_returns = portfolio.cumulative_returns()
|
||||
fig = cumulative_returns.vbt.plot(
|
||||
title='基于价格比率的配对交易累积收益率'
|
||||
)
|
||||
fig.update_layout(
|
||||
xaxis_title='日期',
|
||||
yaxis_title='累积收益'
|
||||
)
|
||||
fig.show()
|
||||
except Exception as e:
|
||||
print(f"累积收益绘制失败: {e}")
|
||||
|
||||
# 4. 回撤分析
|
||||
try:
|
||||
drawdown = portfolio.drawdown()
|
||||
fig = drawdown.vbt.plot(
|
||||
title='基于价格比率的配对交易回撤分析'
|
||||
)
|
||||
fig.update_layout(
|
||||
xaxis_title='日期',
|
||||
yaxis_title='回撤'
|
||||
)
|
||||
fig.show()
|
||||
except Exception as e:
|
||||
print(f"回撤分析绘制失败: {e}")
|
||||
|
||||
# 5. 交易分析
|
||||
try:
|
||||
trades = portfolio.trades
|
||||
if len(trades) > 0:
|
||||
fig = trades.plot_pnl()
|
||||
fig.update_layout(title='基于价格比率的交易盈亏分布')
|
||||
fig.show()
|
||||
|
||||
fig = trades.plot_duration()
|
||||
fig.update_layout(title='基于价格比率的交易持续时间分布')
|
||||
fig.show()
|
||||
except Exception as e:
|
||||
print(f"交易分析绘制失败: {e}")
|
||||
|
||||
|
||||
# ========== 打印详细统计 ==========
|
||||
print("\n=== 详细统计 ===")
|
||||
try:
|
||||
stats = portfolio.stats()
|
||||
# 使用单列统计
|
||||
stats = portfolio['RATIO'].stats()
|
||||
|
||||
def safe_get_stat(stat_dict, key, default="N/A"):
|
||||
value = stat_dict.get(key, default)
|
||||
@ -354,7 +304,7 @@ try:
|
||||
|
||||
# 分析每笔交易
|
||||
try:
|
||||
trades_df = portfolio.trades.records_readable
|
||||
trades_df = portfolio['RATIO'].trades.records_readable
|
||||
if len(trades_df) > 0:
|
||||
print(f"\n交易分析:")
|
||||
print(f"总交易次数: {len(trades_df)}")
|
||||
|
||||
@ -197,7 +197,6 @@ try:
|
||||
# 获取组合总价值
|
||||
portfolio_value = portfolio.value()
|
||||
|
||||
|
||||
|
||||
# ========== 使用vectorbt进行专业分析 ==========
|
||||
print("\n=== VectorBT 专业分析(基于价格比率) ===")
|
||||
@ -207,6 +206,8 @@ try:
|
||||
# 选择第一列(也是唯一的一列)
|
||||
portfolio_single = portfolio['RATIO']
|
||||
|
||||
print(portfolio_single.stats())
|
||||
|
||||
fig = portfolio_single.plot(subplots=[
|
||||
'orders', # 订单
|
||||
'trade_pnl', # 交易盈亏
|
||||
|
||||
Reference in New Issue
Block a user