由于日志记录错误,无法连接到 Interactive Brokers Api

Cannot connect to Interactive Brokers Api because of a logging error

所以我正在使用 python 交互式经纪人 api,并且我正在尝试通过交互式经纪人下限价订单。我的代码是:

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) 

此代码应该能够连接到已经创建的模拟交易账户。虽然我正在恢复错误:

--- 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.
​

我已确保启用活动客户端和套接字客户端已选中,并且信任 IP 地址为 127.0.0.1 为什么我会收到此日志记录错误?

编辑: 新代码是:

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) 

虽然现在我收到错误:

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

我认为 class TradingApp(EWrapper, EClient): 是您想要做的。我认为没有理由延长合同和订单 class。

你改了方法名,必须和super一样class。 不要 re-use 方法名称作为变量名称。

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