How to remove holidays / non-trading days in Candlestick?

I know there is a trick way to remove the holidays or non-trading days in Matplotlib.
However I rather like the format of plotly, but I don’t see any solutions regarding the holday removal in Plotly.
Anyone has any experience on that??

Hi @wswcheng welcome to the forum! Please take a look at this example hiding weekends and holidays.

import plotly.express as px
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

fig = px.scatter(df, x='Date', y='AAPL.High', range_x=['2015-12-01', '2016-01-15'],
                 title="Hide Gaps with rangebreaks")
fig.update_xaxes(
    rangebreaks=[
        dict(bounds=["sat", "mon"]), #hide weekends
        dict(values=["2015-12-25", "2016-01-01"])  # hide Christmas and New Year's
    ]
)
fig.show()
4 Likes

Hi @wswcheng

The use of rangebreaks that @Emmanuelle points out is indeed the tool to use, but that’s one side of it. I say this because for stock data you would be specifying dates for many holidays per year, and most of them change from one year to the other. So the best you can do is avoid using bounds for the rangebreaks, use values and feed it with a list of every date that you don’t have in the CSV file you are loading. In case you need a bigger push to solve this, what I mean is taking the ‘date’ column aside, looping through it, and appending dates that are not there in another list. Then you can go fig.update_xaxes(rangebreaks=[dict(values=mylistofdates)])
Hope this brings you back to Plotly!

Thanks @Emmanuelle and @chrix
Your solution really solve my problem
Is there a direct link to some kind of calender websites such that it can ignore the holidays itself?

There’s actually a simpler solution that is a little more universal/generalized. I made a slight change to @Emmanuelle example.

import plotly.express as px
import pandas as pd
df = pd.read_csv(‘https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv’)

fig = px.scatter(df, x=‘Date’, y=‘AAPL.High’, range_x=[‘2015-12-01’, ‘2016-01-15’],
title=“Hide Gaps with rangebreaks”)
fig.update_xaxes(type=“category”)
fig.show()