0

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)
aipz
  • 25
  • 4

2 Answers2

1

There are indeed some lacunae in the original yahoo data (the website has inconsistent X axis rather than showing gaps). For the purpose of time series analysis, applying data = data.resample('5T').ffill() (or interpolate()) is about the best you can do I presume.

If you wish to imitate yahoo chart behaviour, you'll have to configure rangebreaks like in this question.

dx2-66
  • 2,376
  • 2
  • 4
  • 14
0

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

r-beginners
  • 31,170
  • 3
  • 14
  • 32