forked from kyksj-1/StrategyRealizationHelp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_visualization.py
More file actions
336 lines (272 loc) · 11.9 KB
/
simple_visualization.py
File metadata and controls
336 lines (272 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"""
MA20趋势跟踪策略 - 简化可视化工具
生成基础图表展示策略表现
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime
import os
import logging
# 设置中文字体和日志
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def create_simple_visualization():
"""创建简化可视化"""
logger.info("创建简化可视化报告...")
# 创建保存目录
save_dir = 'results'
os.makedirs(save_dir, exist_ok=True)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
# 读取最新的交易数据
import glob
trade_files = glob.glob('results/trades_*.csv')
if not trade_files:
logger.error("未找到交易数据文件")
return
latest_trade_file = max(trade_files, key=os.path.getctime)
logger.info(f"使用交易文件: {latest_trade_file}")
trades_df = pd.read_csv(latest_trade_file)
trades_df['date'] = pd.to_datetime(trades_df['date'])
# 筛选有盈亏的交易
trades_with_pnl = trades_df[trades_df['pnl'].notna()].copy()
if len(trades_with_pnl) == 0:
logger.error("没有有效的盈亏数据")
return
# 1. 盈亏分布直方图
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
pnls = trades_with_pnl['pnl']
plt.hist(pnls, bins=20, alpha=0.7, color='steelblue', edgecolor='black')
plt.axvline(x=0, color='red', linestyle='--', alpha=0.7, label='盈亏平衡点')
plt.axvline(x=pnls.mean(), color='orange', linestyle='--',
label=f'平均值: {pnls.mean():.0f}')
plt.title('盈亏分布直方图', fontsize=14, fontweight='bold')
plt.xlabel('盈亏 (CNY)')
plt.ylabel('频次')
plt.legend()
plt.grid(True, alpha=0.3)
# 2. 盈亏时间序列
plt.subplot(2, 2, 2)
colors = ['green' if pnl > 0 else 'red' for pnl in pnls]
plt.scatter(range(len(pnls)), pnls, c=colors, alpha=0.7, s=50)
plt.axhline(y=0, color='black', linestyle='-', alpha=0.5)
plt.title('盈亏时间序列', fontsize=14, fontweight='bold')
plt.xlabel('交易序号')
plt.ylabel('盈亏 (CNY)')
plt.grid(True, alpha=0.3)
# 3. 累计盈亏
plt.subplot(2, 2, 3)
cumulative_pnl = pnls.cumsum()
plt.plot(range(len(cumulative_pnl)), cumulative_pnl,
color='darkblue', linewidth=2)
plt.fill_between(range(len(cumulative_pnl)), cumulative_pnl,
alpha=0.3, color='lightblue')
plt.axhline(y=0, color='red', linestyle='--', alpha=0.7)
plt.title('累计盈亏曲线', fontsize=14, fontweight='bold')
plt.xlabel('交易序号')
plt.ylabel('累计盈亏 (CNY)')
plt.grid(True, alpha=0.3)
# 4. 盈亏统计
plt.subplot(2, 2, 4)
# 计算统计指标
win_trades = pnls[pnls > 0]
loss_trades = pnls[pnls < 0]
stats = {
'总交易': len(pnls),
'盈利': len(win_trades),
'亏损': len(loss_trades),
'胜率': f"{len(win_trades)/len(pnls)*100:.1f}%"
}
# 创建文本显示
plt.text(0.1, 0.8, '交易统计', fontsize=16, fontweight='bold',
transform=plt.gca().transAxes)
plt.text(0.1, 0.6, f"总交易次数: {stats['总交易']}", fontsize=12,
transform=plt.gca().transAxes)
plt.text(0.1, 0.5, f"盈利交易: {stats['盈利']}", fontsize=12,
color='green', transform=plt.gca().transAxes)
plt.text(0.1, 0.4, f"亏损交易: {stats['亏损']}", fontsize=12,
color='red', transform=plt.gca().transAxes)
plt.text(0.1, 0.3, f"胜率: {stats['胜率']}", fontsize=12,
transform=plt.gca().transAxes)
if len(win_trades) > 0:
plt.text(0.1, 0.2, f"平均盈利: {win_trades.mean():.0f}", fontsize=12,
color='green', transform=plt.gca().transAxes)
if len(loss_trades) > 0:
plt.text(0.1, 0.1, f"平均亏损: {loss_trades.mean():.0f}", fontsize=12,
color='red', transform=plt.gca().transAxes)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.axis('off')
plt.tight_layout()
save_path = os.path.join(save_dir, f'strategy_analysis_{timestamp}.png')
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"策略分析图已保存: {save_path}")
# 2. 创建单独的盈亏分布图
plt.figure(figsize=(10, 6))
plt.subplot(1, 2, 1)
plt.hist(pnls, bins=15, alpha=0.7, color='lightblue', edgecolor='black')
plt.axvline(x=0, color='red', linestyle='--', linewidth=2, label='盈亏平衡')
plt.axvline(x=pnls.mean(), color='orange', linestyle='--', linewidth=2,
label=f'均值: {pnls.mean():.0f}')
plt.title('盈亏分布', fontsize=14, fontweight='bold')
plt.xlabel('盈亏 (CNY)')
plt.ylabel('频次')
plt.legend()
plt.grid(True, alpha=0.3)
plt.subplot(1, 2, 2)
# 盈利vs亏损对比
if len(win_trades) > 0 and len(loss_trades) > 0:
plt.boxplot([win_trades, abs(loss_trades)],
labels=['盈利', '亏损(绝对值)'],
patch_artist=True,
boxprops=dict(facecolor='lightgreen', alpha=0.7))
plt.title('盈利vs亏损分布', fontsize=14, fontweight='bold')
plt.ylabel('金额 (CNY)')
plt.grid(True, alpha=0.3)
else:
plt.text(0.5, 0.5, '数据不足', ha='center', va='center',
transform=plt.gca().transAxes, fontsize=14)
plt.tight_layout()
save_path2 = os.path.join(save_dir, f'pnl_distribution_{timestamp}.png')
plt.savefig(save_path2, dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"盈亏分布图已保存: {save_path2}")
# 3. 创建累计盈亏图
plt.figure(figsize=(12, 6))
plt.subplot(1, 1, 1)
cumulative_pnl = pnls.cumsum()
# 创建颜色渐变效果
colors = ['green' if x >= 0 else 'red' for x in cumulative_pnl]
plt.plot(range(len(cumulative_pnl)), cumulative_pnl,
color='darkblue', linewidth=2, label='累计盈亏')
# 填充颜色
for i in range(len(cumulative_pnl)-1):
plt.fill_between([i, i+1], [cumulative_pnl.iloc[i], cumulative_pnl.iloc[i+1]],
alpha=0.3, color=colors[i])
plt.axhline(y=0, color='black', linestyle='-', alpha=0.5)
# 标记最终值
final_value = cumulative_pnl.iloc[-1]
plt.scatter(len(cumulative_pnl)-1, final_value,
color='red' if final_value < 0 else 'green', s=100,
marker='o', label=f'最终值: {final_value:,.0f}')
plt.title('累计盈亏趋势', fontsize=16, fontweight='bold')
plt.xlabel('交易序号')
plt.ylabel('累计盈亏 (CNY)')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
save_path3 = os.path.join(save_dir, f'cumulative_pnl_{timestamp}.png')
plt.savefig(save_path3, dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"累计盈亏图已保存: {save_path3}")
# 4. 创建月度表现分析(如果有足够数据)
if len(trades_with_pnl) > 10:
plt.figure(figsize=(12, 8))
# 按月份分组
trades_with_pnl['month'] = trades_with_pnl['date'].dt.to_period('M')
monthly_stats = trades_with_pnl.groupby('month').agg({
'pnl': ['sum', 'count', 'mean']
})
monthly_stats.columns = ['total_pnl', 'trade_count', 'avg_pnl']
plt.subplot(2, 1, 1)
monthly_pnls = monthly_stats['total_pnl']
colors = ['green' if x >= 0 else 'red' for x in monthly_pnls]
plt.bar(range(len(monthly_pnls)), monthly_pnls, color=colors, alpha=0.7)
plt.axhline(y=0, color='black', linestyle='-', alpha=0.5)
plt.title('月度盈亏', fontsize=14, fontweight='bold')
plt.ylabel('月度盈亏 (CNY)')
plt.xticks(range(len(monthly_pnls)),
[str(month) for month in monthly_pnls.index], rotation=45)
plt.grid(True, alpha=0.3)
plt.subplot(2, 1, 2)
monthly_counts = monthly_stats['trade_count']
plt.plot(range(len(monthly_counts)), monthly_counts,
marker='o', linewidth=2, markersize=6, color='steelblue')
plt.title('月度交易次数', fontsize=14, fontweight='bold')
plt.ylabel('交易次数')
plt.xlabel('月份')
plt.xticks(range(len(monthly_counts)),
[str(month) for month in monthly_counts.index], rotation=45)
plt.grid(True, alpha=0.3)
plt.tight_layout()
save_path4 = os.path.join(save_dir, f'monthly_analysis_{timestamp}.png')
plt.savefig(save_path4, dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"月度分析图已保存: {save_path4}")
logger.info("🎉 可视化报告生成完成!")
logger.info(f"生成的图表文件:")
logger.info(f"1. 策略综合分析图: {save_path}")
logger.info(f"2. 盈亏分布对比图: {save_path2}")
logger.info(f"3. 累计盈亏趋势图: {save_path3}")
if len(trades_with_pnl) > 10:
logger.info(f"4. 月度表现分析图: {save_path4}")
def show_sample_charts():
"""显示示例图表"""
logger.info("创建示例图表...")
# 创建模拟数据
np.random.seed(42)
# 1. 示例盈亏分布
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
# 模拟盈亏数据
sample_pnls = np.concatenate([
np.random.normal(2000, 1000, 60), # 盈利
np.random.normal(-1000, 500, 40) # 亏损
])
plt.hist(sample_pnls, bins=20, alpha=0.7, color='lightblue', edgecolor='black')
plt.axvline(x=0, color='red', linestyle='--', alpha=0.7, label='盈亏平衡')
plt.axvline(x=sample_pnls.mean(), color='orange', linestyle='--',
label=f'均值: {sample_pnls.mean():.0f}')
plt.title('示例: 盈亏分布', fontsize=12, fontweight='bold')
plt.xlabel('盈亏')
plt.ylabel('频次')
plt.legend()
plt.grid(True, alpha=0.3)
# 2. 示例累计盈亏
plt.subplot(1, 3, 2)
cumulative = np.cumsum(sample_pnls)
plt.plot(cumulative, color='darkblue', linewidth=2)
plt.fill_between(range(len(cumulative)), cumulative, alpha=0.3, color='lightblue')
plt.axhline(y=0, color='red', linestyle='--', alpha=0.7)
plt.title('示例: 累计盈亏', fontsize=12, fontweight='bold')
plt.xlabel('交易序号')
plt.ylabel('累计盈亏')
plt.grid(True, alpha=0.3)
# 3. 示例交易信号
plt.subplot(1, 3, 3)
# 模拟价格数据
dates = pd.date_range('2023-01-01', periods=50, freq='D')
prices = 4000 + np.cumsum(np.random.normal(0, 20, 50))
plt.plot(dates, prices, color='black', linewidth=1.5, label='价格')
# 添加模拟信号
buy_dates = dates[::10]
buy_prices = prices[::10]
sell_dates = dates[5::10]
sell_prices = prices[5::10]
plt.scatter(buy_dates, buy_prices, color='green', s=100, marker='^',
label='买入信号', zorder=5)
plt.scatter(sell_dates, sell_prices, color='red', s=100, marker='v',
label='卖出信号', zorder=5)
plt.title('示例: 交易信号', fontsize=12, fontweight='bold')
plt.xlabel('日期')
plt.ylabel('价格')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
save_path = 'results/sample_charts.png'
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"示例图表已保存: {save_path}")
if __name__ == "__main__":
# 首先创建可视化
create_simple_visualization()
# 然后显示示例
show_sample_charts()
logger.info("✅ 所有可视化任务完成!")
logger.info("📊 请查看 results 目录下的 PNG 图片文件!")