0

I'm developing a trading bot with Interactive Broker API with python and ib_async library. So far, so good I managed to connect, get data, place order. But I have a question regarding callback function and error. I'm using the following code to place an order

import ib_async as ib_api
def order_filled(trade, fill):
 """
 Called when order has been placed
 """
 logging.config.fileConfig(Path(cwd, "logging.ini"), disable_existing_loggers=False) # commenting this line raised the error
 logger_ibkr = logging.getLogger("IBKR")
 
 for i in range(10):
 print(i) # it prints 0
 print(notdefined) # error is not raised
 
ib = ib_api.IB()
ib.connect("127.0.0.1", port=4002)
contract = ib_api.Contract(secType="FUT",
 symbol="MNQ",
 currency="USD",
 exchange="CME",
 localSymbol="MNQM5"
 )
order = ib_api.MarketOrder("BUY", 1)
trade = ib.placeOrder(contract, order)
trade.fillEvent += order_filled
ib.run()

I have voluntarily used a invalid name on the fill object (which is a namedtuple) but still the code is running without catching any errors. I think there is something to do with asyncio but couldn't find any good answer.

I will be thankful if someone has a proper explanation.

And Is there a way to catch error without using the try/except block ?

Many thanks in advance

asked Jun 2, 2025 at 12:56
11
  • always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. It will be also more readable in question, and more people can see it - so more people may help. Commented Jun 2, 2025 at 13:03
  • better create minimal working code which we could run to see problem. Commented Jun 2, 2025 at 13:04
  • 1
    maybe library already catched error and hid it - so it can run all time even if you made mistake in code or data. In some modules it needs to change settings in logging to see all errors. Commented Jun 2, 2025 at 13:05
  • python - How do I change the logging level for ibkr's ibapi? - Stack Overflow Commented Jun 2, 2025 at 13:09
  • 1
    as I said: some modules may catch errors automatically (using try/catch in inside its code) and hide them - and maybe it does the same. Commented Jun 2, 2025 at 13:23

1 Answer 1

0

OK, i think I found a reason why the error is not raised. I use a custom logger with a config file and somehow setting the config file for logger disable/delete I don't reallyknow the library own loggers logging.config.fileConfig(Path(cwd, "logging.ini"))

Even with key word disable_existing_loggers set to False

My logging.ini:

[loggers]
keys=root, IBKR
[handlers]
keys=IBKR_handler, consoleHandler
[formatters]
keys=main
[logger_root]
handlers=consoleHandler
[formatter_main]
class=classCustomLogging.CustomFormatter
[handler_consoleHandler]
class=StreamHandler
level=CRITICAL
formatter=main
args=(sys.stdout,)
[logger_IBKR]
level=DEBUG
handlers=IBKR_handler
qualname=IBKR
[handler_IBKR_handler]
level=DEBUG
class=classCustomLogging.CustomTimedRotatingFileHandler
formatter=main
args = ("Debug-IBKR-","D", 7)

Any ideas why disable_existing_loggers doesn't seem to be effective ?

answered Jun 2, 2025 at 19:44
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.