从绘图中删除 dt_breaks

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

我想绘制一个 ohlc 图,消除不同时间间隔没有数据的间隙(范围中断)。 使用每日间隔时程序运行良好,但使用其他间隔(1小时,30分钟)时我无法消除间隙,过程或多或少相同,但仅显示第一天。

首先我认为这是一个日期时间格式问题,但删除“rangebreaks”可以绘制所有系列。

有什么建议吗?

提前致谢

import warnings
import pandas as pd
from datetime import datetime
from dateutil.relativedelta import relativedelta
import numpy as np
import plotly.io as pio
from plotly.subplots import make_subplots
import plotly
import plotly.graph_objects as go
import yfinance as yf
#pio.renderers.default = "browser"
warnings.filterwarnings('ignore')

t_var = ['META']
end = datetime.now()
start = end - relativedelta(days=59)
df_y = yf.download(t_var, start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), interval= "30m")
d = df_y.index[0]
b = df_y.index[-1]

dt_all = pd.date_range(start=d,end=b,freq='30min')
dt_breaks = [d for d in dt_all if not d in df_y.index]
fig = make_subplots(rows=1, cols=1)
fig.add_ohlc(x=df_y.index, open=df_y['Open'], high=df_y['High'], low=df_y['Low'], close=df_y['Close'],name='stock',decreasing_line_color='#21201f', increasing_line_color='#21201f', row=1, col=1)
fig.update_xaxes(showgrid=False, zeroline=False, showticklabels=True, showspikes=True, spikemode='across', spikesnap='cursor', showline=False,rangebreaks=[dict(values=dt_breaks)], rangeslider_visible=False)
#fig.update_xaxes(showgrid=False, zeroline=False, showticklabels=True, showspikes=True, spikemode='across', spikesnap='cursor', showline=False,rangeslider_visible=False)
fig.show()
python-3.x plotly
1个回答
0
投票

问题在于如何计算 dt_breaks。当您使用每日间隔时,它工作得很好,因为每日时间范围内没有丢失数据点。但是,当使用较小的间隔(例如 30 分钟)时,数据中可能存在间隙(丢失数据),并且计算 dt_breaks 的方式会导致问题。

要解决此问题,您需要修改 dt_breaks 的计算以查找丢失的数据点并创建适当的范围中断。这是您的代码的更新版本

  import warnings
import pandas as pd
from datetime import datetime
from dateutil.relativedelta import relativedelta
import numpy as np
import plotly.io as pio
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import yfinance as yf

#pio.renderers.default = "browser"
warnings.filterwarnings('ignore')

t_var = ['META']
end = datetime.now()
start = end - relativedelta(days=59)
df_y = yf.download(t_var, start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), interval="30m")
d = df_y.index[0]
b = df_y.index[-1]

dt_all = pd.date_range(start=d, end=b, freq='30min')
dt_breaks = [d for d in dt_all if d not in df_y.index]

fig = make_subplots(rows=1, cols=1)
fig.add_ohlc(x=df_y.index, open=df_y['Open'], high=df_y['High'], low=df_y['Low'], close=df_y['Close'], name='stock', decreasing_line_color='#21201f', increasing_line_color='#21201f', row=1, col=1)

# Calculate range breaks for missing data
range_breaks = []
for i in range(1, len(df_y.index)):
    prev_time = df_y.index[i - 1]
    curr_time = df_y.index[i]
    if (curr_time - prev_time) > pd.Timedelta(minutes=30):
        range_breaks.append(dict(values=[prev_time, curr_time]))

fig.update_xaxes(showgrid=False, zeroline=False, showticklabels=True, showspikes=True, spikemode='across', spikesnap='cursor', showline=False, rangebreaks=range_breaks, rangeslider_visible=False)
fig.show()

修改后的代码根据连续数据点之间的时间差计算 range_breaks。如果时间差大于 30 分钟,则表明缺少数据点,并且在前一个数据点和当前数据点之间创建范围中断。这样,OHLC 图就不会显示因缺失数据而造成的间隙。

请尝试此更新的代码,它应该以 30 分钟间隔或您选择的任何其他间隔显示没有间隙的 OHLC 图表。

© www.soinside.com 2019 - 2024. All rights reserved.