Cannot connect to Interactive Brokers Api because of a logging error

906 views Asked by At

So I am I am using the python interactive brokers api, and I am trying to place a limit order with interactive brokers. The code I have is:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
import threading
import time


class TradingApp(EWrapper, EClient, Contract, Order):
    def __init__(self):
        EClient.__init__(self, self)
 
    def nextValidOrderId(self, orderId):
        super().nextValidId(orderId)
        self.nextValidOrderId = orderId
        print("NextValidId:", orderId)

def websocket_con():
    app.run()
    
        
app = TradingApp()
app.connect("127.0.0.1", 7497, clientId=1)

con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1)

contract=Contract()
contract.symbol = 'AMD'
contract.secType = 'STK'
contract.currency = 'USD'
contract.exchange = 'SMART'

order=Order()
order.action = 'BUY'
order.orderType = 'LMT'
order.totalQuantity = 1
order.lmtPrice = 10

app.placeOrder(app.nextValidOrderId,contract,order)
time.sleep(5) 

This code should be able to make a connection to an already made paper trading account. Though I am reciving the error:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\B1880\Anaconda3\lib\logging\__init__.py", line 1025, in emit
    msg = self.format(record)
  File "C:\Users\B1880\Anaconda3\lib\logging\__init__.py", line 869, in format
    return fmt.format(record)
  File "C:\Users\B1880\Anaconda3\lib\logging\__init__.py", line 608, in format
    record.message = record.getMessage()
  File "C:\Users\B1880\Anaconda3\lib\logging\__init__.py", line 369, in getMessage
    msg = msg % self.args
  File "C:\Users\B1880\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\object_implem.py", line 12, in __repr__
    return str(id(self)) + ": " + self.__str__()
  File "C:\Users\B1880\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\contract.py", line 85, in __str__
    str(self.conId),
AttributeError: 'TradingApp' object has no attribute 'conId'
Call stack:
  File "C:\Users\B1880\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\B1880\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "C:\Users\B1880\Anaconda3\lib\site-packages\traitlets\config\application.py", line 664, in launch_instance
    app.start()
  File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 583, in start
    self.io_loop.start()
  File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 153, in start
    self.asyncio_loop.run_forever()
  File "C:\Users\B1880\Anaconda3\lib\asyncio\base_events.py", line 538, in run_forever
    self._run_once()
  File "C:\Users\B1880\Anaconda3\lib\asyncio\base_events.py", line 1782, in _run_once
    handle._run()
  File "C:\Users\B1880\Anaconda3\lib\asyncio\events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\ioloop.py", line 690, in <lambda>
    lambda f: self._run_callback(functools.partial(callback, future))
  File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\ioloop.py", line 743, in _run_callback
    ret = callback()
  File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 787, in inner
    self.run()
  File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 748, in run
    yielded = self.gen.send(value)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 365, in process_one
    yield gen.maybe_future(dispatch(*args))
  File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 209, in wrapper
    yielded = next(result)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 268, in dispatch_shell
    yield gen.maybe_future(handler(stream, idents, msg))
  File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 209, in wrapper
    yielded = next(result)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 545, in execute_request
    user_expressions, allow_stdin,
  File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 209, in wrapper
    yielded = next(result)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 300, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2858, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2886, in _run_cell
    return runner(coro)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3063, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3254, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-18-5f801bdffa6c>", line 41, in <module>
    app.placeOrder(app.nextValidOrderId,contract,order)
  File "C:\Users\B1880\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\client.py", line 847, in placeOrder
    self.wrapper.error(orderId, NOT_CONNECTED.code(), NOT_CONNECTED.msg())
  File "C:\Users\B1880\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\wrapper.py", line 56, in error
    logger.error("ERROR %s %s %s", reqId, errorCode, errorString)
Unable to print the message and arguments - possible formatting error.
Use the traceback above to help find the error.
​

I have made sure the the Enable Active and Socket Clients is checked and that the trust ip adress is 127.0.0.1 Why am I receiving this logging error?

EDIT: The new code is:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
import threading
import time


class TradingApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
 
    def nextValidId(self, orderId):
        self.nextValidId = orderId

def websocket_con():
    app.run()
    
        
app = TradingApp()
app.connect("127.0.0.1", 7497, clientId=1)

con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1)

contract=Contract()
contract.symbol = 'AMD'
contract.secType = 'STK'
contract.currency = 'USD'
contract.exchange = 'SMART'

order=Order()
order.action = 'BUY'
order.orderType = 'LMT'
order.totalQuantity = 1
order.lmtPrice = 10

app.placeOrder(app.nextValidId,contract,order)
time.sleep(5) 

Though now I am getting the error:

ERROR <bound method TradingApp.nextValidId of <__main__.TradingApp object at 0x00000136780E7508>> 504 Not connected
1

There are 1 answers

4
brian On BEST ANSWER

I think class TradingApp(EWrapper, EClient): is what you wanted to do. I see no reason to extend the Contract and Order class.

You changed the name of a method, it must be the same as the super class. Don't re-use method names as variables names.

#def nextValidOrderId(self, orderId):
def nextValidId(self, orderId):
    self.nextValidOrderId = orderId