如何使用来自雅虎财经的数据创建烛台图表?

How to create candlestick chart with data that came from yahoo finance?

我正在使用 yfinanceplotly 库 (python 2.7) 来获取 Euro/USD 数据,然后创建一个 烛台图表

这是我从雅虎财经下载数据的代码:

import yfinance as yf

data = yf.download(tickers='EURUSD=X', period='1d', interval='30m')

示例输出:

data.tail(10)


                               Open         High    Low Close   Adj Close     Volume
                 Datetime                       
2022-02-25 17:30:00+00:00   1.125239    1.125239    1.124101    1.124354    1.124354    0
2022-02-25 18:00:00+00:00   1.124480    1.125873    1.124480    1.125492    1.125492    0
2022-02-25 18:30:00+00:00   1.125619    1.126507    1.125619    1.126126    1.126126    0
2022-02-25 19:00:00+00:00   1.125999    1.126507    1.125492    1.126253    1.126253    0
2022-02-25 19:30:00+00:00   1.126634    1.126888    1.125366    1.126634    1.126634    0
2022-02-25 20:00:00+00:00   1.126888    1.127015    1.126126    1.126634    1.126634    0
2022-02-25 20:30:00+00:00   1.126507    1.127650    1.126507    1.127015    1.127015    0
2022-02-25 21:00:00+00:00   1.127142    1.127523    1.126761    1.127523    1.127523    0
2022-02-25 21:30:00+00:00   1.127650    1.127777    1.127396    1.127777    1.127777    0
2022-02-25 22:00:00+00:00   1.127142    1.127142    1.127142    1.127142    1.127142    0

我的目标是绘制一个 candlestick 风格的图表,我尝试了这段代码来创建图表:

date_time = data.select_dtypes(['datetime64'])
fig = go.Figure(data=[go.Candlestick(x=date_time,
                                     open=data['Open'],
                                     high=data['High'],
                                     low=data['Low'],
                                     close=data['Close'])])

fig.show()

弹出窗口 window 什么都不显示(空白 window):

我做错了什么?我该怎么做?

根据@r-beginners 评论。 data.index.select_dtypes(['datetime64']) 没有日期时间类型的列。您想使用 xaxis 的索引值,因此不需要整行,只需使用 xaxis 的索引。

import yfinance as yf
import plotly.graph_objects as go

data = yf.download(tickers='EURUSD=X', period='1d', interval='30m')

fig = go.Figure(data=[go.Candlestick(x=data.index,
                                     open=data['Open'],
                                     high=data['High'],
                                     low=data['Low'],
                                     close=data['Close'])])

fig.show()