【问题标题】:How to retrieve delayed stock price by python via IB TWS如何通过 IB TWS 通过 python 检索延迟的股票价格
【发布时间】:2020-08-22 13:23:29
【问题描述】:

这是试图通过盈透证券 (IB) TWS 检索 AAPL 延迟股票价格的代码。

但是,没有检索到任何数据。

如您所见,已调用 app.reqMarketDataType(3) 来设置延迟数据。 (3是延迟)

我已在 IB TWS 中登录模拟账户并确保选择了“启用 ActiveX 和 Socket 客户端”。

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract

import threading
import time
   
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, 123)

#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 = 'AAPL'
apple_contract.secType = 'STK'
apple_contract.exchange = 'SMART'
apple_contract.currency = 'USD'

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

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

【问题讨论】:

  • 您确定演示帐户有延迟数据吗?代码看起来不错,但我会在星期一尝试测试它。请注意,当市场关闭时,您应该要求延迟冻结数据 (4)。
  • 我已成功运行此代码,谢谢 - 但返回市场数据后失败:'EReader thread Traceback 中未处理的异常(最近一次调用最后一次):文件“/home/lb /.local/lib/python3.8/site-packages/ibapi/reader.py”,第 34 行,运行数据 = self.conn.recvMsg() 文件“/home/lb/.local/lib/python3.8 /site-packages/ibapi/connection.py”,第 99 行,recvMsg buf = self._recvAllMsg() 文件“/home/lb/.local/lib/python3.8/site-packages/ibapi/connection.py” ,第 119 行,在 _recvAllMsg buf = self.socket.recv(4096) OSError: [Errno 9] Bad file descriptor'

标签: python algorithmic-trading interactive-brokers ibpy


【解决方案1】:

代码工作正常。问题是,除非您得到 tickType 2(实时询问),否则您不会打印,延迟询问是 tickType 67。

https://interactivebrokers.github.io/tws-api/tick_types.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2021-04-28
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多