-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart_utils.py
More file actions
63 lines (54 loc) · 2.26 KB
/
Copy pathchart_utils.py
File metadata and controls
63 lines (54 loc) · 2.26 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
# chart_utils.py
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import pandas as pd
def create_candlestick_chart(ohlcv_data, symbol):
if not ohlcv_data:
# 返回一个空的图表,并设置背景色和字体颜色以适应暗色主题
fig = go.Figure()
fig.update_layout(
template="plotly_dark", # 使用暗色主题模板
title=f'{symbol} K线图',
xaxis_title='时间',
yaxis_title='价格',
height=700,
annotations=[
dict(
text="无数据",
xref="paper",
yref="paper",
showarrow=False,
font=dict(size=28, color="#cccccc")
)
]
)
return fig
df = pd.DataFrame(ohlcv_data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
fig = make_subplots(rows=2, cols=1, shared_xaxes=True,
vertical_spacing=0.1,
row_heights=[0.7, 0.3]) # K线图占70%,成交量占30%
# K线图
fig.add_trace(go.Candlestick(x=df['timestamp'],
open=df['open'],
high=df['high'],
low=df['low'],
close=df['close'],
name='Candlestick',
increasing_line_color='#00CC96', # 绿色
decreasing_line_color='#EF553B'), # 红色
row=1, col=1)
# 成交量图
fig.add_trace(go.Bar(x=df['timestamp'], y=df['volume'], name='Volume',
marker_color='#636EFA'), # 蓝色,与暗色主题更搭
row=2, col=1)
fig.update_layout(
template="plotly_dark", # 使用暗色主题模板
title=f'{symbol} K线图',
xaxis_rangeslider_visible=False, # 隐藏底部时间滑块
hovermode='x unified',
height=700 # 调整图表高度
)
fig.update_yaxes(title_text="价格", row=1, col=1)
fig.update_yaxes(title_text="成交量", row=2, col=1)
return fig