Python program that buys options using IBKR

674 views Asked by At

So I have built the following program

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()

and when I run it I get the following output:

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

the first errors are expected but the last error

 ERROR 1 10268 The 'EtradeOnly' order attribute is not supported.

is not expected at all I don't know why it does this I don't think it is connected with my code I have also looked at IBKRs API guide but found nothing.

if anyone could help or point me in the right direction I would appreciate it!

Thank you for your time!

python program that ran and traded options but it didn't

1

There are 1 answers

0
AudioBubble On

After further research and calling the IBKR support team I actually found the issue, they said that either the trade workstation or API is not updated causing the error 'TradeOnly'... . They also said to go around this error I can use the following two lines of code:

order.eTradeOnly = False
order.firmQuoteOnly = False

and well after implementing what they said it worked, hope I helped someone.

Thanks