How to use rangebreaks for a datetime index at levels smaller than 1 hour?

Follow-up:

I managed to find an article showing how to use dvalues to remove gaps… but the article is in Japanese.

By chance, I can read Japanese, so I’m posting this to hopefully show any future readers how to remove gaps of any size, theoretically down to 1 millisecond resolution data.

It turns out to be very simple using Pandas:

    ...
    # Note: Example is for 30-minute bars

    bars = ... <your candlestick data in a Pandas Dataframe>

    df_resample = bars.resample("30T").max()
    merged_index = bars.index.append(df_resample.index)
    timegap = merged_index[~merged_index.duplicated(keep=False)]
    dvalue = 30 * 60 * 1000  # 30min * 60sec/min * 1000msec/sec

    fig.update_xaxes(
        rangebreaks=[
            dict(values=timegap, dvalue=dvalue)
        ],
        ...

General process:

  1. Resample your bars using the interval of the bars. In my example I’m using 30-minute bars, so I resampled using ‘30T’ which means 30 minutes to Pandas. Reference: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects
  2. Merge the indexes of your original data with the new resampled data, then take a difference to find only values that do not exist in your original data (referred to as ‘timegap’ in the code). These will be the values you do not want to show (you want to skip) in the graph.
  3. Set ‘dvalues’ to match your bar interval. dvalues is specified in milliseconds, so my example sets dvalues to 30 minutes specified in milliseconds (30 * 60 * 1000).

Now the gaps are gone!

3 Likes