IBKR期权交易Python程序报错:'EtradeOnly'属性不支持
IBKR Python API 下单期权报错:ERROR 1 10268 The 'EtradeOnly' order attribute is not supported
我开发了如下用于通过IBKR买入期权的Python程序:
from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.contract import Contract from ibapi.order import * import threading import time class IBapi(EWrapper, EClient): def __init__(self): EClient.__init__(self, self) def nextValidId(self, orderId: int): super().nextValidId(orderId) self.nextorderId = orderId print('The next valid order id is: ', self.nextorderId) def orderStatus(self, orderId, status, filled, remaining, avgFullPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice): print('orderStatus - orderid:', orderId, 'status:', status, 'filled', filled, 'remaining', remaining, 'lastFillPrice', lastFillPrice) def openOrder(self, orderId, contract, order, orderState): print('openOrder id:', orderId, contract.symbol, contract.secType, '@', contract.exchange, ':', order.action, order.orderType, order.totalQuantity, orderState.status) def execDetails(self, reqId, contract, execution): print('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity) def run_loop(): app.run() app = IBapi() app.connect('127.0.0.1', 7497, 123) app.nextorderId = None #Start the socket in a thread api_thread = threading.Thread(target=run_loop, daemon=True) api_thread.start() #Check if the API is connected via orderid while True: if isinstance(app.nextorderId, int): print('connected') print() break else: print('waiting for connection') time.sleep(1) #Create contract contract = Contract() contract.symbol = 'TSLA' contract.secType = 'OPT' contract.exchange = 'SMART' contract.lastTradeDateOrContractMonth = '20230120' contract.strike = 100 contract.right = 'C' contract.multiplier = '100' #Create order object order = Order() order.action = 'BUY' order.totalQuantity = 1 order.orderType = 'MKT' #Place order app.placeOrder(app.nextorderId, contract, order) time.sleep(3) app.disconnect()
运行该程序后得到如下输出:
ERROR -1 2104 Market data farm connection is OK:usfarm.nj ERROR -1 2104 Market data farm connection is OK:cashfarm ERROR -1 2104 Market data farm connection is OK:usfarm ERROR -1 2106 HMDS data farm connection is OK:euhmds ERROR -1 2106 HMDS data farm connection is OK:fundfarm ERROR -1 2106 HMDS data farm connection is OK:ushmds ERROR -1 2158 Sec-def data farm connection is OK:secdefnj waiting for connection The next valid order id is: 1 ERROR 1 10268 The 'EtradeOnly' order attribute is not supported. connected
其中前几个错误为预期内,但最后出现的错误“ERROR 1 10268 The 'EtradeOnly' order attribute is not supported”完全超出预期。我认为该错误与代码无关,查阅IBKR API指南也未找到相关信息,希望得到帮助或方向指引。感谢您的时间!
解决方向
- 显式设置订单的
etradeOnly属性:尽管代码中没有手动设置该属性,但IBKR的Order类可能存在默认值触发了这个字段。尝试在创建订单后添加order.etradeOnly = False,强制关闭该属性。 - 检查API客户端与TWS/IB网关版本兼容性:这个错误大概率是API客户端版本和你运行的TWS或IB网关版本不匹配导致的,确保两者版本一致,优先更新到最新稳定版。
- 重置TWS/IB网关的API设置:进入TWS的API配置界面,检查是否存在与Etrade相关的限制选项,尝试将API设置恢复为默认状态后重新测试。
- 使用订单工厂方法创建订单:替代手动初始化
Order对象,改用IBAPI提供的Order.create_market_order(action='BUY', totalQuantity=1)方法,该方法会生成符合IBKR标准的订单对象,避免默认属性引发的问题。
内容的提问来源于stack exchange,提问作者user20876058
相关产品推荐
相关产品推荐

