[英]Get bid/ask price from Interactive Brokers using python
This code:这段代码:
ib = IB()
ib.connect('127.0.0.1', 7496)
contract = Stock('SLV', 'SMART', 'USD')
test=ib.reqTickers(contract)
print(test)
will print out this:将打印出这个:
[Ticker(contract=Stock(symbol='SLV', exchange='SMART', currency='USD'), time=datetime.datetime(2019, 7, 1, 15, 18, 43, 287622, tzinfo=datetime.timezone.utc), bid=14.26, bidSize=11224, ask=14.27, askSize=2970, last=14.27, lastSize=1, volume=48694, open=14.24, high=14.33, low=14.24, close=14.33, halted=0.0, ticks=[], tickByTicks=[], domBids=[], domAsks=[], domTicks=[])]
Now I need to get the bid and ask prices.现在我需要得到出价和要价。
Doing print(test.ask)
or print(test.bid)
normally gets error: AttributeError: 'list' object has no attribute 'ask'.执行
print(test.ask)
或print(test.bid)
通常会得到错误:AttributeError: 'list' object has no attribute 'ask'。
I tried many other things similar to the above but got similar errors.我尝试了许多其他与上述类似的事情,但遇到了类似的错误。
您的test
是一个长度为1的列表,其唯一条目是您感兴趣的Ticker
对象。请尝试test[0].ask
。
I had a similar problem, solved this way:我有一个类似的问题,解决了这个问题:
test , in your example, is a list of Ticker objects.在您的示例中, test是一个 Ticker 对象列表。 Also, in your example, contract contains a single contract, but would more commonly contain a group of contracts (* contracts ), as per the ib_insync documents here .
此外,在您的示例中,合同包含单个合同,但更常见的是包含一组合同(*合同),根据此处的 ib_insync 文档。
The following code is a solution:下面的代码是一个解决方案:
test = ib.reqTickers(contract)
for _, r in enumerate(test):
print(r.contract.symbol, r.time, r.bid, r.ask, r.close)
The better code (also per the documents link) would be:更好的代码(也根据文档链接)将是:
tickers = [ib.reqTickers(*contracts)]
for i, r in enumerate(tickers):
for j, t in enumerate(r):
print(t.contract.symbol, t.time, t.bid, t.ask, t.close)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.