使用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.

根本不被期望,我不知道为什么它这样做,我不认为它与我的代码有关,我也看了IBKRs API指南,但没有发现任何东西。

如果有人能帮助我或给我指出正确的方向,我将不胜感激!感谢您的宝贵时间!运行和交易期权的python程序,但它没有

在进一步研究并打电话给IBKR支持团队后,我实际上发现了这个问题,他们说交易工作站或API没有更新,导致错误'TradeOnly'... .他们还说,要解决这个错误,我可以使用以下两行代码:

order.eTradeOnly = False
order.firmQuoteOnly = False

并且很好地实现了他们所说的工作,希望我能帮助到别人。

感谢

相关内容

  • 没有找到相关文章

最新更新