谁能给我建议,我可以构建什么图表来代表 RSI 和交易状态?我有一个研究项目,我正在使用 yahoo_fin 检索交易数据,Panda 数据框包含如下 3 条信息
DateTime RSI Signal
2023-11-15 12:15:00 67.854002 HOLD
2023-11-15 12:20:00 67.854002 HOLD
2023-11-15 12:25:00 73.494992 SELL
2023-11-15 12:30:00 68.330019 HOLD
2023-11-15 12:35:00 63.522484 HOLD
2023-11-15 12:40:00 66.091706 HOLD
2023-11-15 12:45:00 68.330019 HOLD
2023-11-15 12:50:00 63.522484 HOLD
2023-11-15 12:55:00 66.091706 HOLD
2023-11-15 13:00:00 66.091706 BUY
2023-11-15 13:05:00 66.091706 HOLD
我可以通过提供上述信息来创建仪表图吗?我正在使用Python。如果您能为我提供一个例子,我将不胜感激。
我正在尝试做以下事情
col_names = ['Date', 'RSI', 'Signal']
df.columns = col_names
print(df)
fig = go.Figure(go.Indicator(
mode = "gauge+number",
value = "BUY",
domain = {'x': df['Date'], 'y': df['Signle']},
title = {'text': "Trading Signal"}))
It returns the following error:
(0) The 'x[0]' property is a number and may be specified as:
- An int or float in the interval [0, 1]
不清楚你想要什么,但会给出例子 首先,安装plotly
奔跑
pip install plotly
运行代码以查看示例结果
import plotly.graph_objects as go
import pandas as pd
data = {
'DateTime': ['2023-11-15 12:15:00', '2023-11-15 12:20:00', '2023-11-15 12:25:00',
'2023-11-15 12:30:00', '2023-11-15 12:35:00', '2023-11-15 12:40:00',
'2023-11-15 12:45:00', '2023-11-15 12:50:00', '2023-11-15 12:55:00',
'2023-11-15 13:00:00', '2023-11-15 13:05:00'],
'RSI': [67.854002, 67.854002, 73.494992, 68.330019, 63.522484, 66.091706,
68.330019, 63.522484, 66.091706, 66.091706, 66.091706],
'Status': ['HOLD', 'HOLD', 'SELL', 'HOLD', 'HOLD', 'HOLD', 'HOLD', 'HOLD', 'HOLD', 'BUY', 'HOLD']
}
df = pd.DataFrame(data)
df['DateTime'] = pd.to_datetime(df['DateTime'])
fig = go.Figure(go.Indicator(
mode="gauge+number",
value=df['RSI'][0],
title={'text': "RSI"},
domain={'x': [0, 1], 'y': [0, 1]},
gauge={'axis': {'range': [0, 100]},
'bar': {'color': "lightgray"},
'steps': [
{'range': [0, 30], 'color': "red"},
{'range': [30, 70], 'color': "yellow"},
{'range': [70, 100], 'color': "green"}],
'threshold': {
'line': {'color': "black", 'width': 4},
'thickness': 0.75,
'value': df['RSI'][0]}
}
))
fig.add_annotation(
go.layout.Annotation(
text=df['Status'][0],
align='center',
showarrow=False,
x=0.5,
y=0.5
)
)
def update_chart(trace, points, selector):
index = points.point_inds[0]
trace.value = df['RSI'][index]
fig.layout.annotations[0].text = df['Status'][index]
fig.update_layout(updatemenus=[{'type': 'buttons',
'showactive': False,
'buttons': [{'label': 'Play',
'method': 'animate',
'args': [None, {'frame': {'duration': 1000, 'redraw': True},
'fromcurrent': True}]}]}],
sliders=[{'yanchor': 'top',
'xanchor': 'left',
'currentvalue': {'font': {'size': 20},
'visible': True,
'prefix': 'Timestamp: ',
'suffix': ' UTC'},
'transition': {'duration': 300, 'easing': 'cubic-in-out'},
'steps': [{'args': [[f'{timestamp}'], {'frame': {'duration': 300, 'redraw': True},
'mode': 'immediate',
'transition': {'duration': 300}}],
'label': f'{timestamp}',
'method': 'animate'} for timestamp in df['DateTime'].dt.strftime('%Y-%m-%d %H:%M:%S')]},
])
fig.update_geos(projection_type="natural earth")
fig.show()