【问题标题】:Interactive borkers - requesting market data互动经纪人 - 请求市场数据
【发布时间】:2020-05-26 17:55:08
【问题描述】:

我在尝试做一些简单的事情时遇到了一些灾难 - 简而言之,让 ib api 工作。我想获取 LSE 股票的当前市场价格,我订阅了正确的市场信息并运行了以下代码:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
import threading
import time

from ibapi.contract import Contract

class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 2 and reqId == 1:
            print('The current ask price is: ', price)

def run_loop():
    app.run()

app = IBapi()

app.connect('127.0.0.1', 7497, 4002)

#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

time.sleep(1) #Sleep interval to allow time for connection to server

#Create contract object
apple_contract = Contract()
apple_contract.symbol = 'BARC'
apple_contract.secType = 'STK'
apple_contract.exchange = 'LSE'
apple_contract.currency = 'GBP'

#Request Market Data
app.reqMktData(1, apple_contract, '', False, False, [])

time.sleep(10) #Sleep interval to allow time for incoming price data
app.disconnect()

但是我又收到了这个错误:

The current ask price is:  -100.0
unhandled exception in EReader thread
Traceback (most recent call last):
  File "C:\TWS API\source\pythonclient\ibapi\reader.py", line 34, in run
    data = self.conn.recvMsg()
  File "C:\TWS API\source\pythonclient\ibapi\connection.py", line 99, in recvMsg
    buf = self._recvAllMsg()
  File "C:\TWS API\source\pythonclient\ibapi\connection.py", line 119, in _recvAllMsg
    buf = self.socket.recv(4096)
OSError: [WinError 10038] An operation was attempted on something that is not a socket

如果有人可以提供帮助,将不胜感激!谢谢,

【问题讨论】:

    标签: interactive-brokers ib-api


    【解决方案1】:

    我猜 -100 是因为交易所关闭了。错误可以忽略,见https://stackoverflow.com/a/58684561/2855515

    看起来你的代码运行良好,即使有点不正统。

    【讨论】:

    • 我的主!这类似于不打开计算机!完美运行,谢谢布赖恩。出于兴趣,你为什么说“有点不正统”?
    • sleep() 可以用于测试代码,但我不会使用它。您应该在收到 nextValidId 回调后开始请求,以便您知道连接正在工作。然后我会在收到要价或超时后断开连接,而不是睡觉。我也不会使用 4002 作为客户端 ID,因为您可能会对端口感到困惑,但技术上没有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2022-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    相关资源
    最近更新 更多