如何通过IB TWS通过python检索延迟的股票价格

问题描述

这是试图通过盈透证券(IB)交易平台检索APL延迟股票价格的代码。

但是,没有数据被检索。

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

我已经在IB TWS中登录了模拟帐户,并确保选择了“启用ActiveX和套接字客户端”。

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,[])

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

解决方法

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

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...