Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

python - Interactive Broker - No Security Definition has been found for the request

I am trying to get historical data of a specific stock from IBKR, but it seems like the testApp() function here is not being called or something. I'm able to connect to the TWS station, but somehow I'm not being fed back data, so was wondering if there is anything wrong with my Wrapper function?

Also, When I run the code, I get back this error message: "No Security Definition has been found for the request".

Any suggestions as to how I can go about fixing this?

class TestClient(EClient):
    def __init__(self, wrapper):
        EClient.__init__(self, wrapper)


class TestWrapper(wrapper.EWrapper):
    def __init__(self):
        wrapper.EWrapper.__init__(self)


class TestApp(TestWrapper, TestClient):
    def __init__(self):
        TestWrapper.__init__(self)
        TestClient.__init__(self, wrapper=self)

    def error(self, reqid, errorcode, errorstring):
        print("Error: ", reqid, " ", errorcode, " ", errorstring)

    def historicalData(self, reqid: int, bar: BarData):
        print("HistoricalData. ReqId:", reqid, "BarData.", bar)

    def historicalDataUpdate(self, reqid: int, bar: BarData):
        print("HistoricalDataUpdate. ReqId:", reqid, "BarData.", bar)

    def historicalDataEnd(self, reqid: int, start: str, end: str):
        print("HistoricalDataEnd. ReqId:", reqid, "from", start, "to", end)
        app.disconnect()
        print("Finished")

    def nextValidId(self, orderId: int):
        super().nextValidId(orderId)

        logging.debug("setting nextValidOrderId: %d", orderId)
        self.nextValidOrderId = orderId
        print("NextValidId:", orderId)

    def contractDetails(self, reqId: int, contractDetails: ContractDetails):
        super().contractDetails(reqId, contractDetails)
        printinstance(contractDetails)
        self.reqContractDetails(10004, ContractSamples.NewsFeedForQuery())

    def contractDetailsEnd(self, reqId: int):
        super().contractDetailsEnd(reqId)
        print("ContractDetailsEnd. ReqId:", reqId)


def main():
    app = TestApp()
    app.connect("127.0.0.1", 7497, clientId=1)
    print(app.isConnected())
    time.sleep(1)

    contract = Contract()

    contract.secType = "STK"
    contract.symbol = "AAPL"
    contract.currency = "USD"
    contract.exchange = "NASDAQ"


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

    app.reqHistoricalData(4001, contract, querytime, "1 M", "1 day", "MIDPOINT", 1, 1, False, [])

    app.run()


if __name__ == "__main__":
    main()
question from:https://stackoverflow.com/questions/65832558/interactive-broker-no-security-definition-has-been-found-for-the-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The exchange should be SMART for smart routing to all available exchanges. If you really want to just use NASDAQ then the exchange should be ISLAND I think. I know it works because I tested it but haven't ever used it in real trading for things like rebates. I also have no idea whether it would give historical data for just the one exchange but I seriously doubt it.

You have the contractDetails implemented so you could always call that first then you can trade AAPL by specifying just contract.conId = 265598


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...