如何从生成的元组列表中获取元素ib.历史数据来自API交互代理

2024-05-23 15:07:36 发布

您现在位置:Python中文网/ 问答频道 /正文

Interactive Brokers API为此请求提供了:

spzValue = ib.reqHistoricalData(spx, endDateTime='', durationStr='7200 S', barSizeSetting='1 hour', whatToShow='TRADES', useRTH =False)

变量“spxValue”的以下输出:

[BarData(date=datetime.datetime(2019, 7, 19, 20, 0), open=200.59, high=200.7, low=199.69, close=199.76, volume=97, average=200.045, barCount=87), BarData(date=datetime.datetime(2019, 7, 19, 21, 0), open=199.66, high=199.67, low=198.19, close=198.21, volume=369, average=198.969, barCount=322)]

不知何故,我没有成功地从列表中取出元素,也没有通过以下方式将其转换为数据帧:

df = util.df(spzValue)

有人对我如何从“BarData”列表中选择特定元素有什么建议吗?例如,我想有“关闭”值199.76的第一个酒吧。你知道吗

提前谢谢。你知道吗


Tags: 元素df列表closedatetimedateopeninteractive
1条回答
网友
1楼 · 发布于 2024-05-23 15:07:36

看起来你在用ib_insync?(不仅仅是本机IBKR Python API)。你知道吗

有一个在https://github.com/erdewit/ib_insync接收历史数据的例子

from ib_insync import *
# util.startLoop()  # uncomment this line when in a notebook

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)

contract = Forex('EURUSD')
bars = ib.reqHistoricalData(contract, endDateTime='', durationStr='30 D',
        barSizeSetting='1 hour', whatToShow='MIDPOINT', useRTH=True)

# convert to pandas dataframe:
df = util.df(bars)

一旦有了数据帧,就可以使用iloc访问第一行,然后使用列名访问值:

df.iloc[0]['close']

相关问题 更多 >