简体   繁体   中英

How do I get the elements from list of tuples generated by ib.reqHistoricalData from API Interactive Brokers

Interactive Brokers API delivers for this request:

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

the following output for the variable "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)]

Somehow I am not succeeding in getting the elements out of this list nor in converting it into a DataFrame via:

df = util.df(spzValue)

Does anyone have any suggestions how I can select specific elements out of the list "BarData"? For example I would like to have the "close" value 199.76 for the first bar.

Thanks in advance.

It looks like you're using ib_insync ? (not just the native IBKR Python API).

There is an example of receiving historical data at 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)

Then once you have a dataframe, you can then access the first row using iloc, and then the value using the column name:

df.iloc[0]['close']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM