簡體   English   中英

如何從交互式經紀人API獲取歷史股價數據?

[英]How to get historical stock price data from interactive brokers API?

我想使用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 App(wrapper.EWrapper,client.EClient):EClient如果我想重寫EClient任何方法,我只會將其作為子類。 我不是很用python,但是在其他語言中卻是如此。

class App您需要覆蓋您感興趣的方法,例如historicalData

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM