如何去除 15 分钟间隔数据集的烛台散景图中的间隙?

问题描述 投票:0回答:0

我有一个包含 15 分钟间隔数据的数据集,用于创建烛台图表。数据集中的每一行代表一个特定的时间间隔,例如 09:15:00、09:30:00 等等,直到 15:15:00。该数据集涵盖了 2023 年 5 月 1 日至 2023 年 5 月 16 日期间。

但是,数据集中存在一些缺失的数据区间,例如01 May 2023 15:15:00和02 May 2023 09:15:00之间的差距。此外,周六和周日没有可用数据。

我正在使用 Bokeh Python 库来绘制烛台图表。但是,图表显示缺失数据的缺口,这破坏了烛台可视化的连续性。

我尝试过各种方法来解决这个问题,包括调整x轴、用NaN值填充缺失数据、修改图表设置,但都没有提供令人满意的解决方案。

任何人都可以指导我如何消除烛台图表中的间隙并确保可视化连续显示烛台而没有缺失数据间隔的间隙吗?

感谢任何解决此问题的见解、代码示例或建议。谢谢!

def get_timeframe_in_ms(df):

    candle_size = 0.5 # Persentage of size requried
    
    time_diff = df['DATE&TIME'].diff().iloc[1]
    tf_ms = int((time_diff.total_seconds() * 1000))
    return tf_ms
    
def chart(name, df):
    
    RED_candles = df.filter(pl.col('OPEN') >= pl.col('CLOSE')).to_pandas()
    GREEN_candles = df.filter(pl.col('OPEN') <= pl.col('CLOSE')).to_pandas()
    RED_volume_bar = df.select(pl.col('VOLUME').filter(pl.col('OPEN') >= pl.col('CLOSE'))).to_pandas()
    GREEN_volume_bar = df.select(pl.col('VOLUME').filter(pl.col('OPEN') <= pl.col('CLOSE'))).to_pandas()
    
    
    pandas_df = df.to_pandas()
    pandas_df['DATE&TIME'] = pd.to_datetime(pandas_df['DATE&TIME'])
    
    tf_ms = get_timeframe_in_ms(pandas_df)
    
    blank_datetime = pandas_df[['DATE&TIME']].assign(diff=pandas_df['DATE&TIME'].diff()-pd.Timedelta(milliseconds= get_timeframe_in_ms(df=pandas_df)))
    blank_datetime = blank_datetime[blank_datetime['diff']>=pd.Timedelta(milliseconds= get_timeframe_in_ms(df=pandas_df))]

    RED = Category20[7][6]
    GREEN = Category20[5][4]


    TOOLS = "pan,wheel_zoom,crosshair,undo,redo,reset"
    
    p = figure(x_axis_type="datetime", tools=TOOLS, title=name, width=1280, height=520, y_axis_location="right", match_aspect=True)
    p.add_layout(Legend(click_policy="hide", orientation='horizontal', spacing=20), 'below')
    p.sizing_mode = 'scale_both'
    
    p.toolbar.active_scroll = p.select_one(WheelZoomTool) 

    """p.x_range = Range1d(start= df['DATE&TIME'][0],
                        end= df['DATE&TIME'][-1],
                        bounds= ((df['DATE&TIME'][0] - dt.timedelta(days=90)),(df['DATE&TIME'][-1] + dt.timedelta(days=90))),
                        syncable=False)
    min_interval= tf_ms, max_interval = tf_ms*4*6)"""

    """time_range = df['DATE&TIME'].max() - df['DATE&TIME'].min()
    padding = time_range * 0.1  # Add some padding to the time range

    p.x_range = Range1d(start=df['DATE&TIME'].min() - padding,
                        end=df['DATE&TIME'].max() + padding,
                        bounds=((df['DATE&TIME'].min() - padding - dt.timedelta(days=90)),
                                (df['DATE&TIME'].max() + padding + dt.timedelta(days=90))),
                        syncable=False)"""

    start_time = df['DATE&TIME'].min()
    end_time = df['DATE&TIME'].max()

    # Calculate the total number of 15-minute intervals in the time range
    total_intervals = int((end_time - start_time).total_seconds() / (15 * 60))

    # Calculate the expected end time based on the total number of intervals
    expected_end_time = start_time + dt.timedelta(minutes=total_intervals * 15)

    p.x_range = Range1d(start=start_time, end=expected_end_time)

    p.xaxis[0].formatter = DatetimeTickFormatter(months="%b")
    #p.xaxis.major_label_overrides = {i: date.strftime("%d %b %y %H:%M") for i, date in enumerate(df['DATE&TIME'])}
    
    
    p.y_range = Range1d(min_interval= df['CLOSE'][-1]*0.005, max_interval = df['HIGH'].max())

    
    RED = Category20[7][6]
    GREEN = Category20[5][4]
    
    segmnts_green = p.segment('DATE&TIME', 'HIGH', 'DATE&TIME', 'LOW', color=GREEN, source=GREEN_candles)
    segmnts_red = p.segment('DATE&TIME', 'HIGH', 'DATE&TIME', 'LOW', color=RED, source=RED_candles)
    vbars_green = p.vbar('DATE&TIME',width= (tf_ms*0.5),bottom= 'OPEN',top= 'CLOSE', source=GREEN_candles, fill_color=GREEN, line_color=GREEN)
    vbars_red = p.vbar('DATE&TIME', width= (tf_ms*0.5),bottom= 'CLOSE',top= 'OPEN', source=RED_candles, fill_color=RED, line_color=RED)

    return p
python-3.x pandas bokeh python-polars
© www.soinside.com 2019 - 2024. All rights reserved.