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

如何解决由于记录错误,无法连接到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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-