【问题标题】:How to draw Pivot points using plotly?如何使用 plotly 绘制枢轴点?
【发布时间】:2021-01-26 00:47:25
【问题描述】:

我正在使用 plotly 绘制一些带有 SMA 和 MACD 数据的图表。这很好用。

fig = make_subplots(vertical_spacing = 0, rows=3, cols=1, row_heights=[0.6, 0.2, 0.2])

fig.add_trace(go.Ohlc(x=data['timestamp'],
            open=data['open'],
            high=data['high'],
            low=data['low'],
            close=data['close']))

fig.add_trace(go.Scatter(x=data['timestamp'], y=data['sma'], line=dict(color='orange', width=1)), row=1, col=1)


fig.add_trace(go.Scatter(x=data['timestamp'], y = data['macd']), row=2, col=1)
fig.add_trace(go.Scatter(x=data['timestamp'], y = data['macds']*1.1), row=2, col=1)
fig.add_trace(go.Bar(x=data['timestamp'], y = data['volume']), row=3, col=1)
fig.update_layout(xaxis_rangeslider_visible=False,
                xaxis=dict(zerolinecolor='black', showticklabels=False),
                xaxis2=dict(showticklabels=False))

fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=False)
fig.show()

但现在我想添加枢轴:

    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 的输出如下所示:

                    Timestamp      Open      High       Low     Close  Volume    Pivot        R1        S1        R2        S2        R3        S3
499  2020-10-11T14:45:00.000Z  0.000321  0.000321  0.000319  0.000319  886.17  0.00032  0.000321  0.000319  0.000322  0.000318  0.000323  0.000316

如何将这些数据(Pivot、S1、S2、S3、...)添加到我的图形中?我想要与这里的图片相媲美的东西:https://www.fxpivot-points.com/?page=4

我试过了:

fig.add_trace(go.Scatter(x=last_day['Timestamp'], y=last_day['Pivot'], line=dict(color='purple', width=1)), row=1, col=1)

但它只画了一个点,因为我只提供了一个时间戳。我怎样才能使它成为一条水平线?

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    您特别要求仅绘制最后天的计算点,并在您拥有烛台图的完整时间段内显示它们。如果这实际上是您在这里的目标,那么下面的代码 sn-p 将使用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()
    

    【讨论】:

    • 非常感谢。我知道这些计算仅适用于最后一天,并且图表显示了多天!
    • @DenCowboy 好的,酷!鉴于链接源中的图像,这对我来说似乎有点奇怪。但如果这就是你想要的,那么这就是你会得到的 =)
    • 我现在正在使用多个 das,所以我可以检查多个 macd 交叉等,但最后我只会打印一天的图表 :)
    猜你喜欢
    • 1970-01-01
    • 2020-11-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多