Interactive brokers - 请求市场数据
Interactive borkers - requesting market data
我在尝试做一些简单的事情时遇到了一些麻烦 - 简而言之让 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
如果有人能提供帮助,将不胜感激!谢谢,
我猜 -100 是因为交易所关闭了。错误可以忽略,见
看起来你的代码工作正常,即使有点非正统。
我在尝试做一些简单的事情时遇到了一些麻烦 - 简而言之让 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
如果有人能提供帮助,将不胜感激!谢谢,
我猜 -100 是因为交易所关闭了。错误可以忽略,见
看起来你的代码工作正常,即使有点非正统。