如何解决由于记录错误,无法连接到Interactive Brokers Api
因此,我正在使用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",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 545,in execute_request
user_expressions,allow_stdin,File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py",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,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",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,order)
time.sleep(5)
尽管现在我得到了错误:
ERROR <bound method TradingApp.nextValidId of <__main__.TradingApp object at 0x00000136780E7508>> 504 Not connected
解决方法
我认为class TradingApp(EWrapper,EClient):
是您想要做的。我认为没有理由扩展Contract and Order类。
您更改了方法的名称,它必须与超类相同。 不要将方法名称重新用作变量名称。
#def nextValidOrderId(self,orderId):
def nextValidId(self,orderId):
self.nextValidOrderId = orderId
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。