How to obtain Contract Details from the Interactive Brokers API?
Asked Answered
E

2

2

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, "\n")


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:\Users\Greg\Anaconda3\lib\site-packages\IPython\core\interactiveshell.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.

Exceptionable answered 17/1, 2019 at 6:17 Comment(0)
F
3
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.

Freitas answered 29/1, 2019 at 15:1 Comment(7)
baffling; how did you get hold of an instance of app inside MyWrapper before it is even created?Megathere
@Megathere I'm not a python programmer but I guess app would be a global variable. I've also written python examples where the app class inherits from both EClient and EWrapper but that's not how I write everything in java.Freitas
@Megathere look here https://mcmap.net/q/1781857/-python-ibapi-reqcontractdetails-won-39-t-return-result-when-run-second-timeFreitas
thanks for coming back; yes your answer in the comment is a typical example of how IB API would be wired up; if you read carefully you'd see that the app needs to be initialised like you did your other example; and MyWrapper is one of the parent classes for app. At best, unfortunately, your example here is creating circular dependency.Megathere
@Megathere It works perfectly and is exactly how it's done in other languages without multiple inheritance (usually without globals and using plenty of intervening logic). I personally find it simpler to keep the wrapper and client separate and think the multi-inheritance python examples are wrong. I think what you're calling circular logic is actually asynchronous programming. When you ask for data you need to specify where to send it and in trading you also need to act on it.Freitas
we might have talk cross-purposed; after peruse I realised you are calling ‘app’ in the asynchronous callback ‘nextValidId’; right that’s possible and I shall take back my comment above for constructor issue I had. however, I’d still suggest to correct your example as the crucial ‘init’ (constructor) is missing in your ‘MyWrapper’ class. I guess this was how I misread in my first attempt.Megathere
@Megathere It's been a while but to clarify EWrapper has no init method so calling it would do nothing. Iff I wanted to init some variables in my subclass would I make an init method.Freitas
O
0

Using ib_insync package with Python 3, you can also do the following if you want to get the details for a list of contracts all stored in a df:

from ib_insync import *
import pandas as pd


def add_contract_details(ib_client, ib_contract, df):
    list_of_contract_details = ib_client.reqContractDetails(contract=ib_contract)
    if list_of_contract_details:
        print(
            f"Found {len(list_of_contract_details)} contract{'s' if len(list_of_contract_details) > 1 else ''} for {ib_contract.symbol}: "
        )
        for contract_details in list_of_contract_details:
            data = {}
            for k, v in contract_details.contract.__dict__.items():
                data[k] = v
            for k, v in contract_details.__dict__.items():
                if k != "contract":
                    data[k] = v
            df = pd.DataFrame([data]) if df is None else df.append(pd.DataFrame([data]))
    else:
        print(f"No details found for contract {ib_contract.symbol}.")
    return df



ib = IB()
ib.connect(host="127.0.0.1", port=7497, clientId=1)
btc_fut_cont_contract = ContFuture("BRR", "CMECRYPTO")
ib.qualifyContracts(btc_fut_cont_contract)
fx_contract_usdjpy = Forex('USDJPY')
ib.qualifyContracts(fx_contract_usdjpy)

df_contract_details = None
for c in [btc_fut_cont_contract, fx_contract_usdjpy]:
    df_contract_details = add_contract_details(ib, c, df_contract_details)
print(df_contract_details)
Omeromero answered 13/5, 2021 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.