Search code examples
pythonpandasplotlydata-science

Missing part of the data in graph


I'm writing a program which will show the candlestick chart of Gold and detect patterns. I'm getting the data from yfinance and trying to draw the chart with plotly, but I see that some parts of the data are missing. I checked the data with mplfinance and everything worked successfully, but I need it in plotly.

import plotly.graph_objects as go
import pandas as pd
import yfinance as yf
import talib

import mplfinance as mpf



data = yf.download(tickers="GC=F", period="5d", interval="5m")
fig = go.Figure(data=[go.Candlestick(x=data.index,
            open=data['Open'], high=data['High'],
            low=data['Low'], close=data['Close'])
                 ])

fig.update_layout(xaxis_rangeslider_visible=False)
fig.write_html('first_figure.html', auto_open=True)

Solution

  • I don't have the code for mplfinance so I don't know, but I think nontarading=True is set and the gap is automatically removed. plotly has a feature for market holidays and nighttime exclusions. Since your time series data is in 5 minute increments, set dvalue=1000ms60sec60min minutes. The main point is to prepare and set the time series list you want to remove.

    import plotly.graph_objects as go
    import pandas as pd
    import yfinance as yf
    import numpy as np
    
    data = yf.download(tickers="GC=F", period="5d", interval="5m")
    full_range = pd.date_range(data.index[0],data.index[-1], freq='5min')
    data = data.reindex(full_range, fill_value=np.NaN, axis=0)
    delete_range = data[data['Open'].isnull()].index
    
    fig = go.Figure(data=[go.Candlestick(x=data.index,
                open=data['Open'], high=data['High'],
                low=data['Low'], close=data['Close'])
                     ])
    
    fig.update_layout(xaxis_rangeslider_visible=False)
    fig.update_xaxes(rangebreaks=[
        dict(values=delete_range, dvalue=3600000)
    ])
    # fig.write_html('first_figure.html', auto_open=True)
    fig.show()
    

    enter image description here