1 回答
TA贡献1773条经验 获得超3个赞
您特别要求仅绘制最后一天的计算点,并在您拥有烛台图的整个时间段内显示它们。如果这实际上是您想要实现的目标,那么下面的代码片段将生成以下绘图,其中使用 和fig.add_shapes()
分别fig.add_annotations()
显示水平线和枢轴点名称。
如果有什么地方不太对劲,请告诉我,我们可以讨论进一步的调整。
阴谋:
基于Apple示例数据的完整代码:
import plotly.graph_objects as go
import pandas as pd
from datetime import datetime
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv').tail(5)
fig = go.Figure(data=[go.Candlestick(x=df['Date'],
open=df['AAPL.Open'],
high=df['AAPL.High'],
low=df['AAPL.Low'],
close=df['AAPL.Close'])])
last_day = df.iloc[-1].to_frame().T
last_day = last_day.rename(columns = lambda x: x.replace('AAPL.', ''))
not_pivots = list(last_day.columns)
last_day['Pivot'] = (last_day['High'] + last_day['Low'] + last_day['Close'])/3
last_day['R1'] = 2*last_day['Pivot'] - last_day['Low']
last_day['S1'] = 2*last_day['Pivot'] - last_day['High']
last_day['R2'] = last_day['Pivot'] + (last_day['High'] - last_day['Low'])
last_day['S2'] = last_day['Pivot'] - (last_day['High'] - last_day['Low'])
last_day['R3'] = last_day['Pivot'] + 2*(last_day['High'] - last_day['Low'])
last_day['S3'] = last_day['Pivot'] - 2*(last_day['High'] - last_day['Low'])
last_day
pivots = [n for n in last_day.columns if n not in not_pivots]
pcols = ['green', 'blue', 'blue', 'red', 'red', 'black', 'black']
for i, col in enumerate(pivots):
# horizontal lines
fig.add_shape(type="line",
x0=df['Date'].iloc[0],
y0=last_day[col].iloc[-1],
x1=df['Date'].iloc[-1],
y1=last_day[col].iloc[-1],
line=dict(
color=pcols[i],
width=1,
#dash="dashdot",
),)
# line annotations
fig.add_annotation(dict(font=dict(color=pcols[i],size=12),
x=df['Date'].iloc[0],
y=last_day[col].iloc[0],
showarrow=False,
text=col,
textangle=0,
xanchor='right',
xref="x",
yref="y"))
fig.show()
添加回答
举报