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
1 Answer 1
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 ?
Comments
Explore related questions
See similar questions with these tags.
minimal working codewhich we could run to see problem.loggingto see all errors.