I'm using ib_insync and Python and I would like to capture those cases where secType has not been specified (but only symbol, exchange and maybe currency).
The code below tries to find a valid secType but by doing that IB generates messages 200 and / or 321 at the console. I would like to surpress those messages ONLY while performing these tests.
elif (typeSec == ""): # Type of contract not specified by the caller
secTypes = [ "", "IND", "FOREX", "OPT", "FUT", "CRYPTO", "STK"]
# Try various possibilities
# IB will generate errors 321 and 200 for each failure
for i in secTypes:
contract = Contract(symbol= symbol, exchange= exchange, currency= currency, secType=i)
if not (contract == None):
try:
self.ib.qualifyContracts(contract) # Fill the contract with missing info (Remark: self.ib is an ib-connection stored in a class)
if not ((contract.secType == None) or (contract.secType == "")):
typeSec = contract.secType
# Eventually a valid contract has been found, maybe the right one
break
except:
contract = None
if (contract == None):
print("Create contract for sectype= ", typeSec, " has not been implemented yet")
How can achieve this
This method could be used to gather all valid possibilities and let the user chose the one (s)he wants.
In a post I saw it is possible to surpress specific IB-errros/warnings and also errorahandlers for the whole program but that's not what I'm looking for: I would just like to turn off those messages for this small part of the code.