Python 如何从交互式经纪人API获取历史股价数据?

Python 如何从交互式经纪人API获取历史股价数据?,python,api,interactive-brokers,Python,Api,Interactive Brokers,我想使用IBAPI获取历史股票数据。我的代码不起作用,什么也不返回。有人能帮我编辑代码吗?谢谢 from ibapi import client from ibapi import wrapper import datetime from ibapi.contract import Contract from ibapi.common import BarData # ! [socket_init] class App(wrapper.EWrapper,client.EClient):

我想使用IBAPI获取历史股票数据。我的代码不起作用,什么也不返回。有人能帮我编辑代码吗?谢谢

from ibapi import client
from ibapi import wrapper
import datetime
from ibapi.contract import Contract
from ibapi.common import BarData

# ! [socket_init]
class App(wrapper.EWrapper,client.EClient):
    def __init__(self):
        wrapper.EWrapper.__init__(self)
        client.EClient.__init__(self, wrapper=self)


 #Build a sample contract
contract = Contract();
contract.symbol = "9005.T";
contract.secType = "STK";
contract.currency = "JPY";
contract.exchange = "SMART";


app = App()
app.connect(host='localhost',port=7497, clientId=3)
print(app.isConnected())
queryTime = (datetime.datetime.today() - datetime.timedelta(days=180)).strftime("%Y%m%d %H:%M:%S")
print(app.reqHistoricalData(4102, contract, queryTime,"1 M", "1 day", "MIDPOINT", 1, 1, False, []))
queryTime = (datetime.datetime.today() - datetime.timedelta(days=180)).strftime("%Y%m%d %H:%M:%S")
print(app.historicalData(4102,BarData))


OUTPUT:
True
None
None

我试图阅读源代码。但我发现这对我来说很难理解。其他帖子给出的答案似乎与最新的api版本无关。

下面是我要做的

class应用程序(wrapper.EWrapper,client.EClient):
如果我想覆盖它的任何方法,我只会将
EClient
子类化。我不太使用python,但在其他语言中也是如此

类应用程序中
您需要覆盖您感兴趣的方法,如
历史数据

app.connect
之后,必须调用
app.run()
以启动其消息读取器线程。一旦该线程取得控制权,它将在您的程序中阻塞,所以您必须异步执行程序流

我会把数字放在评论里,让你看到流程

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

import datetime

class MyWrapper(EWrapper):

    def nextValidId(self, orderId:int):
        #4 first message received is this one
        print("setting nextValidOrderId: %d", orderId)
        self.nextValidOrderId = orderId
        #5 start requests here
        self.start()

    def historicalData(self, reqId:int, bar: BarData):
        #7 data is received for every bar
        print("HistoricalData. ReqId:", reqId, "BarData.", bar)

    def historicalDataEnd(self, reqId: int, start: str, end: str):
        #8 data is finished
        print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
        #9 this is the logical end of your program
        app.disconnect()
        print("finished")

    def error(self, reqId, errorCode, errorString):
        # these messages can come anytime.
        print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)

    def start(self):
        queryTime = (datetime.datetime.today() - datetime.timedelta(days=180)).strftime("%Y%m%d %H:%M:%S")

        fx = Contract()
        fx.secType = "CASH" 
        fx.symbol = "USD"
        fx.currency = "JPY"
        fx.exchange = "IDEALPRO"

        #6 request data, using fx since I don't have Japanese data
        app.reqHistoricalData(4102, fx, queryTime,"1 M", "1 day", "MIDPOINT", 1, 1, False, [])

app = EClient(MyWrapper()) #1 create wrapper subclass and pass it to EClient
app.connect("127.0.0.1", 7497, clientId=123) #2 connect to TWS/IBG
app.run() #3 start message thread

有人能帮我吗?显示制作简单程序的方法。您的程序甚至没有使用
app.run()
@brian启动消息循环谢谢您的回复,brian!我来检查一下。我从你们那个里找到了很多关于这个领域的答案。我很感激你的努力谢谢你,你很善良。你的帮助节省了我很多时间。