lucky1928
lucky1928

Reputation: 8905

plotly - remove holiday from stock plot not work as expected

Since 2025/2/17 is a holiday, It should be removed from plot.rangebreaks looks exactly for this case, but,it generate a very weird graph.

full code:

#!/usr/bin/env python3
import yfinance as yf
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import pandas as pd
import datetime

STK="AMZN"
end_time = datetime.datetime.strptime("2025-02-19","%Y-%m-%d")
start_time = end_time - datetime.timedelta(days=7)
df = yf.Ticker(STK).history(start=start_time,end=end_time,interval="30m")

fig = make_subplots(rows=1, cols=1,
                        vertical_spacing=0.01,
                        #shared_xaxes=True
                        )

trace = go.Candlestick(
            x=df.index,
            open=df["Open"],
            high=df["High"],
            low=df["Low"],
            close=df["Close"],
        )
fig.add_trace(trace)

fig.update_xaxes(
    rangebreaks=[
       dict(bounds=["sat", "mon"]),
       dict(values=["2025-02-17","2025-12-25"]),
       dict(bounds=[16, 9.5], pattern="hour")
    ]
)
fig.show()

Output: enter image description here

If comment out the second line in rangebreaks, output graph as below:

enter image description here

The expected output should be cut the range of 2025-02-17

enter image description here

Upvotes: 1

Views: 35

Answers (0)

Related Questions