1

I am trying to plot candlestick and simple moving average data using plot.ly as you see in the chart, there is empty value line plotted where there is no date/price data available actually. the data for this chart is also shown.

So in chart, you can see that the non-trading hours are also plotted. How to avoid this?

stock chart

stock data in pandas dataframe

Ameya
  • 1,712
  • 1
  • 14
  • 29

2 Answers2

0

Plotly doesn't seem to support just hidding gaps with no data, as you can see from this discussion in the plotly community

The answer on this SO removes the gap by entering explicit null values for every index where the data is empty.

PabTorre
  • 2,878
  • 21
  • 30
  • in my case the input data is from historical candle data and unfortunately i can not add null values or modify the input data in any way. as a work around to insert null values, i will have to first capture the historical candle data into a csv and add null values and then read that csv back into dataframes before plotting. – Shivaprashanth Hiremath Jul 28 '18 at 10:49
  • 1
    @Shivaprashanth. That doesn't make sense. Dataframes are writable. If yours isn't, make a copy that it. Do the operation in memory. – Mad Physicist Jul 28 '18 at 14:44
  • i guess thats what i have to do. will try that. Thanks @MadPhysicist – Shivaprashanth Hiremath Jul 28 '18 at 16:39
0

you can use the following code that helped me

fig = go.Figure(data=[go.Candlestick(
x=dataset_5.index,
open=dataset_5['open'], 
high=dataset_5['high'],
low=dataset_5['low'], 
close=dataset_5['close'])])
fig.update_xaxes(rangeslider_visible=True,
    rangebreaks=[
        # NOTE: Below values are bound (not single values), ie. hide x to y
        dict(bounds=["sat", "mon"]),  # hide weekends, eg. hide sat to before mon
        dict(bounds=[18, 9.0], pattern="hour"),  # hide hours outside of 9.30am-4pm
        # dict(values=["2019-12-25", "2020-12-24"])  # hide holidays (Christmas and 
        New Year's, etc)
     ]
  )
  fig.update_layout(xaxis_rangeslider_visible=True)
  fig.show()
RickP
  • 1
  • 1