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
417 views
in Technique[技术] by (71.8m points)

python - How to obtain Contract Details from the Interactive Brokers API?

Following the Interactive Brokers documentation I am trying to obtain the contract details using the below code:

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

class MyWrapper(EWrapper):

    def contractDetails(self, reqId, contractDetails):
        super().contractDetails(reqId, contractDetails)

        print("ContractDetails. ReqId:", reqId,
              contractDetails.summary.symbol,
              contractDetails.summary.secType,
              "ConId:", contractDetails.summary.conId,
              "@", contractDetails.summary.exchange)

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


wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=0)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))

from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"

app.reqContractDetails(4444, contract)
app.run()

And the output that is returned is:

serverVersion:148 connectionTime:b'20190117 17:11:38 AEST'

An exception has occurred, use %tb to see the full traceback.

SystemExit


C:UsersGregAnaconda3libsite-packagesIPythoncoreinteractiveshell.py:2969: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

How to obtain the contract details from the Interactive Brokers API? I tried using %tb but don't think I put it on the correct line.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
from ibapi.client import EClient
from ibapi.wrapper import EWrapper


class MyWrapper(EWrapper):

    def nextValidId(self, orderId:int):
        print("setting nextValidOrderId: %d", orderId)
        self.nextValidOrderId = orderId
        # start program here or use threading
        app.reqContractDetails(4444, contract)

    def contractDetails(self, reqId, contractDetails):
        print(reqId, contractDetails.contract)# my version doesnt use summary

    def contractDetailsEnd(self, reqId):
        print("ContractDetailsEnd. ", reqId)
        # this is the logical end of your program
        app.disconnect() # delete if threading and you want to stay connected

    def error(self, reqId, errorCode, errorString):
        print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)


wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=123)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))

from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"

app.run() # delete this line if threading

# def runMe():
#     app.run()

# import threading
# thread = threading.Thread(target = runMe)
# thread.start()

# input('enter to disconnect')
# app.disconnect()

You are asking for data before you start the message reader. Maybe you get the data before it starts.

IB recommends starting the program after you receive nextValidId so you know everything is running properly. Since the python API blocks in a message read loop you need to implement threading or structure your program to run asynchronously.

I've shown how to do it so it will just run with no user input and it is event driven, or asynchronous. This means the program waits until it is supposed to do something and then it does it.

I've including the threading option, just change the comments.

ContractDetails.summary has been changed to contract. I'm not sure it ever was summary in python, don't know where you got that from.


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

...