-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_visualization.py
More file actions
68 lines (56 loc) · 2.63 KB
/
Copy pathtest_visualization.py
File metadata and controls
68 lines (56 loc) · 2.63 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
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import akshare as ak
# 获取数据
df = ak.stock_zh_a_hist(symbol='600519', period='daily', start_date='20200101', end_date='20241231', adjust='qfq')
df.rename(columns={'日期': 'Date', '开盘': 'Open', '最高': 'High', '最低': 'Low', '收盘': 'Close', '成交量': 'Volume'}, inplace=True)
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
df.sort_index(ascending=True, inplace=True)
# 创建测试数据来模拟多个中枢
continuous_indices = list(range(len(df)))
date_labels = df.index.strftime('%Y-%m-%d').tolist()
# 创建图表
fig = go.Figure(data=[go.Candlestick(x=continuous_indices, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], name='K线')])
# 手动创建多个不重叠的中枢来测试可视化
# 中枢1
fig.add_shape(type="rect",
x0=50, y0=800, x1=100, y1=900,
fillcolor='rgba(128, 0, 128, 0.4)', opacity=0.4, layer="below", line_width=2,
line=dict(color='rgba(128, 0, 128, 1.0)', width=2), xref="x", yref="y")
# 中枢2 (与中枢1在时间上有部分重叠,但价格不同)
fig.add_shape(type="rect",
x0=80, y0=950, x1=150, y1=1050,
fillcolor='rgba(255, 165, 0, 0.4)', opacity=0.4, layer="below", line_width=2,
line=dict(color='rgba(255, 165, 0, 1.0)', width=2), xref="x", yref="y")
# 中枢3 (完全不同的时间段)
fig.add_shape(type="rect",
x0=200, y0=1100, x1=250, y1=1200,
fillcolor='rgba(0, 0, 255, 0.4)', opacity=0.4, layer="below", line_width=2,
line=dict(color='rgba(0, 0, 255, 1.0)', width=2), xref="x", yref="y")
fig.update_layout(
title_text='测试多个中枢可视化',
template="plotly_white",
showlegend=False,
xaxis_rangeslider_visible=False,
yaxis_title="价格",
)
# 设置X轴标签
tickvals = continuous_indices[::max(1, len(continuous_indices)//10)]
ticktext = [date_labels[i] for i in tickvals]
fig.update_xaxes(
tickmode='array',
tickvals=tickvals,
ticktext=ticktext,
tickangle=45,
)
fig.update_xaxes(tickformat='%Y-%m-%d')
# 保存到HTML文件
fig.write_html("/Users/zyjk/Desktop/project/chan/test_multiple_pivots_visualization.html")
print("测试可视化已保存到 /Users/zyjk/Desktop/project/chan/test_multiple_pivots_visualization.html")
# 同时显示基本统计信息
print(f"\n数据统计:")
print(f"K线数量: {len(df)}")
print(f"时间范围: {df.index[0].strftime('%Y-%m-%d')} 到 {df.index[-1].strftime('%Y-%m-%d')}")
print(f"价格范围: {df['Low'].min():.2f} - {df['High'].max():.2f}")