You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在存在缺失值的Plotly OHLC蜡烛图上添加带枢轴价格差值标注的连线

如何在Plotly OHLC蜡烛图上绘制带价格差值标注的连续枢轴点连线

我明白你遇到的问题了——因为Pivot Price列里的NaN值,直接用go.Scatter会导致折线在缺失值处断开,而且没法自动添加相邻枢轴点的价格差值标注。下面是一套完整的解决方案,完全满足你的需求:

首先,我们需要先提取并整理有效的枢轴点数据,然后基于这些数据绘制连续连线,最后添加差值标注。

完整实现代码

import plotly.graph_objects as go
import pandas as pd
from datetime import datetime
import numpy as np

df = pd.DataFrame( {
    'index': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24},
    'Date': {0: '2018-09-03', 1: '2018-09-04', 2: '2018-09-05', 3: '2018-09-06', 4: '2018-09-07', 5: '2018-09-10', 6: '2018-09-11', 7: '2018-09-12', 8: '2018-09-13', 9: '2018-09-14', 10: '2018-09-17', 11: '2018-09-18', 12: '2018-09-19', 13: '2018-09-20', 14: '2018-09-21', 15: '2018-09-24', 16: '2018-09-25', 17: '2018-09-26', 18: '2018-09-27', 19: '2018-09-28', 20: '2018-10-01', 21: '2018-10-02', 22: '2018-10-03', 23: '2018-10-04', 24: '2018-10-05'},
    'Open': {0: 1.2922067642211914, 1: 1.2867859601974487, 2: 1.2859420776367188, 3: 1.2914056777954102, 4: 1.2928247451782229, 5: 1.292808175086975, 6: 1.3027958869934082, 7: 1.3017443418502808, 8: 1.30451238155365, 9: 1.3110626935958862, 10: 1.3071041107177734, 11: 1.3146650791168213, 12: 1.3166556358337402, 13: 1.3140604496002195, 14: 1.3271400928497314, 15: 1.3080958127975464, 16: 1.3117163181304932, 17: 1.3180439472198486, 18: 1.3169677257537842, 19: 1.3077707290649414, 20: 1.3039510250091553, 21: 1.3043931722640991, 22: 1.2979763746261597, 23: 1.2941633462905884, 24: 1.3022021055221558},
    'High': {0: 1.2934937477111816, 1: 1.2870012521743774, 2: 1.2979259490966797, 3: 1.2959914207458496, 4: 1.3024225234985352, 5: 1.3052103519439695, 6: 1.30804443359375, 7: 1.3044441938400269, 8: 1.3120088577270508, 9: 1.3143367767333984, 10: 1.3156682252883911, 11: 1.3171066045761108, 12: 1.3211784362792969, 13: 1.3296104669570925, 14: 1.3278449773788452, 15: 1.3166556358337402, 16: 1.3175750970840454, 17: 1.3196094036102295, 18: 1.3180439472198486, 19: 1.3090718984603882, 20: 1.3097577095031738, 21: 1.3049719333648682, 22: 1.3020155429840088, 23: 1.3036959171295166, 24: 1.310753345489502},
    'Low': {0: 1.2856279611587524, 1: 1.2813942432403564, 2: 1.2793285846710205, 3: 1.289723515510559, 4: 1.2918561697006226, 5: 1.289823293685913, 6: 1.2976733446121216, 7: 1.298414707183838, 8: 1.3027619123458862, 9: 1.3073604106903076, 10: 1.3070186376571655, 11: 1.3120776414871216, 12: 1.3120431900024414, 13: 1.3140085935592651, 14: 1.305841088294983, 15: 1.3064552545547483, 16: 1.3097233772277832, 17: 1.3141123056411743, 18: 1.309706211090088, 19: 1.3002548217773438, 20: 1.3014055490493774, 21: 1.2944146394729614, 22: 1.2964619398117063, 23: 1.2924572229385376, 24: 1.3005592823028564},
    'Close': {0: 1.292306900024414, 1: 1.2869019508361816, 2: 1.2858428955078125, 3: 1.2914891242980957, 4: 1.2925406694412231, 5: 1.2930254936218262, 6: 1.302643060684204, 7: 1.3015578985214231, 8: 1.304546356201172, 9: 1.311131477355957, 10: 1.307326316833496, 11: 1.3146305084228516, 12: 1.3168463706970217, 13: 1.3141123056411743, 14: 1.327087163925171, 15: 1.30804443359375, 16: 1.3117333650588991, 17: 1.3179919719696045, 18: 1.3172800540924072, 19: 1.3078734874725342, 20: 1.3039000034332275, 21: 1.3043591976165771, 22: 1.2981956005096436, 23: 1.294062852859497, 24: 1.3024225234985352},
    'Pivot Price': {0: 1.2934937477111816, 1: np.nan, 2: 1.2793285846710205, 3: np.nan, 4: np.nan, 5: np.nan, 6: np.nan, 7: np.nan, 8: np.nan, 9: np.nan, 10: np.nan, 11: np.nan, 12: np.nan, 13: 1.3296104669570925, 14: np.nan, 15: np.nan, 16: np.nan, 17: np.nan, 18: np.nan, 19: np.nan, 20: np.nan, 21: np.nan, 22: np.nan, 23: 1.2924572229385376, 24: np.nan}
})

# 1. 提取有效的枢轴点数据并计算差值
pivot_df = df.dropna(subset=['Pivot Price']).reset_index(drop=True)
# 计算相邻枢轴点的价格差值(绝对值保留三位小数)
pivot_df['Price Diff'] = pivot_df['Pivot Price'].diff().abs().round(3)
# 去掉第一个NaN值(因为第一个点没有前一个点)
pivot_df = pivot_df.dropna(subset=['Price Diff'])

# 2. 创建蜡烛图
fig = go.Figure(data=[go.Candlestick(
    x=df['Date'],
    open=df['Open'],
    high=df['High'],
    low=df['Low'],
    close=df['Close'],
    name='OHLC'
)])

# 3. 添加连续的枢轴点连线(黑色)
fig.add_trace(go.Scatter(
    x=pivot_df['Date'],
    y=pivot_df['Pivot Price'],
    mode='lines+markers',
    line=dict(color='black', width=2),
    marker=dict(color='black', size=8),
    name='Pivot Points'
))

# 4. 添加价格差值标注
for i in range(len(pivot_df)):
    # 获取当前和前一个枢轴点的信息
    prev_date = pivot_df.loc[i-1, 'Date'] if i > 0 else None
    curr_date = pivot_df.loc[i, 'Date']
    prev_price = pivot_df.loc[i-1, 'Pivot Price'] if i > 0 else None
    curr_price = pivot_df.loc[i, 'Pivot Price']
    diff = pivot_df.loc[i, 'Price Diff']
    
    if prev_date is not None and prev_price is not None:
        # 计算标注的位置:日期取中间,价格取两个点的平均位置(可以根据需要调整)
        prev_idx = df[df['Date'] == prev_date].index[0]
        curr_idx = df[df['Date'] == curr_date].index[0]
        mid_x_idx = (prev_idx + curr_idx) / 2
        mid_x = df.loc[mid_x_idx, 'Date'] if mid_x_idx.is_integer() else None
        # 如果中间不是整数索引,用两个日期的中间位置(Plotly会自动处理时间轴的中间点)
        if not mid_x:
            mid_x = [prev_date, curr_date]
        mid_y = (prev_price + curr_price) / 2
        
        # 添加标注
        fig.add_annotation(
            x=mid_x,
            y=mid_y,
            text=f"Δ={diff}",
            showarrow=False,
            font=dict(color='red', size=12),
            bgcolor='white',
            opacity=0.8
        )

# 调整布局
fig.update_layout(
    autosize=False,
    width=1000,
    height=800,
    title='OHLC with Pivot Points and Price Difference Annotations',
    xaxis_title='Date',
    yaxis_title='Price'
)

fig.show()

关键步骤解释

  1. 数据预处理:
    • 用`dropna(subset=['P
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:14:07