I’ve been working with intraday finance data and would like a way to draw ‘shadows’ on non trading hours or pre/post market hours. I have seen the example on Shapes | Python | Plotly, but I was hoping there was some way to draw multiple vrect shapes quickly. I was wondering if maybe there was a way to pass a time instead of specifying a date (i.e. x0=“00:00”, x1=“09:30”) that could do this. Or is it possible to maybe pass a list into x0 and x1 for each date?
Here’s what I had tried which unfortunately won’t work but might be awesome:
fig.add_vrect(
x0="00:00",
x1="04:00",
row="all",
col=1,
fillcolor="grey",
opacity=0.2,
line_width=0,
layer="below",
exclude_empty_subplots=False
)
fig.add_vrect(
x0="04:00",
x1="09:30",
row="all",
col=1,
fillcolor="grey",
opacity=0.1,
line_width=0,
layer="below",
exclude_empty_subplots=False
)
fig.add_vrect(
x0="16:00",
x1="20:00",
row="all",
col=1,
fillcolor="grey",
opacity=0.1,
line_width=0,
layer="below",
exclude_empty_subplots=False
)
fig.add_vrect(
x0="20:00",
x1="00:00",
row="all",
col=1,
fillcolor="grey",
opacity=0.2,
line_width=0,
layer="below",
exclude_empty_subplots=False
)
I’ve tried this with datetime.time() as well.
I am also using rangebreaks based on user input to hide certain dates so I am also curious if vrect shapes are compatible with rangebreaks.
Maybe I am thinking about this completely wrong too. If anyone has any suggestions for some other way to change the background color based on the x-axis let me know.
Many thanks,
Evan
Update:
I have gotten it to work with a loop, but this just feels gross and is very slow (especially with large date ranges), so I would still appreciate suggestions.
for date in dates:
fig.add_vrect(
x0=date + datetime.timedelta(hours=0),
x1=date + datetime.timedelta(hours=4),
row="all",
col=1,
fillcolor="grey",
opacity=0.2,
line_width=0,
layer="below",
exclude_empty_subplots=False
)
fig.add_vrect(
x0=date + datetime.timedelta(hours=4),
x1=date + datetime.timedelta(hours=9.5),
row="all",
col=1,
fillcolor="grey",
opacity=0.1,
line_width=0,
layer="below",
exclude_empty_subplots=False
)
fig.add_vrect(
x0=date + datetime.timedelta(hours=16),
x1=date + datetime.timedelta(hours=20),
row="all",
col=1,
fillcolor="grey",
opacity=0.1,
line_width=0,
layer="below",
exclude_empty_subplots=False
)
fig.add_vrect(
x0=date + datetime.timedelta(hours=20),
x1=date + datetime.timedelta(hours=24),
row="all",
col=1,
fillcolor="grey",
opacity=0.2,
line_width=0,
layer="below",
exclude_empty_subplots=False
)
Thanks again