3

I have set rangebreak to exclude trading hours. But when i choose a button that looks at the past 1 day (which includes the hours excluded in the rangebreak), the plot suddenly shows the non-trading hours. However when i choose past 2 days or more, the non-trading hours suddenly disappears.

import yfinance as yf
import plotly.graph_objs as go
from plotly.subplots import make_subplots
data = yf.Ticker('SPY').history(period='5d', interval='1m')

fig = make_subplots(rows=1, cols=1,)

## Scatter
_ = fig.append_trace(
    go.Scatter(
        x=data.index,
        y=data['Close'],
        fill='tozeroy',
        line=dict(color='rgb(0,0,255)'),
    ),
    row=1, col=1,
)

## X-Axes
_ = fig.update_xaxes(
    rangeslider_visible=True,
    rangeselector=dict(
        buttons=[
            dict(count=6.5, label="Intraday(6.5hrs)", step="hour", stepmode="todate"),
            dict(count=1, label="1d", step="day", stepmode="backward"),
            dict(count=2, label="2d", step="day", stepmode="backward"),
            dict(count=3, label="3d", step="day", stepmode="backward"),
            dict(count=4, label="4d", step="day", stepmode="backward"),
            dict(count=5, label="5d", step="day", stepmode="backward"),
            dict(step="all")
        ],
        font={"color": "white"},
        bgcolor = 'black',
    ),
    rangebreaks=[
        dict(bounds=["sat", "mon"]),  # hide weekends, eg. hide sat to before mon
        dict(bounds=[16, 9.5], pattern="hour"),  # hide hours outside of 9.30am-4pm 
    ],
    row=1, col=1,
)
## Y-Axes
minVal = min(data['Close'])
maxVal = max(data['Close'])
_ = fig.update_yaxes(
    fixedrange=False,
    range=[minVal, maxVal],
    row=1, col=1,
    )

fig.show()

The main plot that excludes the trading hours all

After clicking 1d (past 1 day) 1a

3d (3 days) enter image description here

leonardltk1
  • 257
  • 1
  • 4
  • 18
  • looks to work correctly to me. only thing I'm seeing is 6.5 hrs shows 9am to 9:30am all others exclude as expected. plotly 5.3.1 – Rob Raymond Nov 09 '21 at 14:15
  • yes, i want to change from `Intraday (6.5 hrs)` to latest trading window. Which is these two situations : 1) `9.30am to present time` during a trading window. 2) `9.30am to 4pm` if im running the code outside the trading window. – leonardltk1 Nov 11 '21 at 14:44
  • it is working fine for me too. Maybe because I tested with a different version (plotly.__version__ = 5.4.0) – Eric Marchand Nov 20 '21 at 17:44

0 Answers0