¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Trying to place two sets of orders


 

Hello all.?
?

I am trying to place two option orders simultaneously (each with different quantity and target profit, but same entry price and stop loss) using ib_sync and xlwings on Interactive Brokers TWS. Below is the python script I am using. The orders get sent to TWS, but for some reason, one group of the orders just get cancelled a few seconds later without triggering!

Screenshot below shows the orders on TWS, with order group 17, 17.2 and 17.1 getting cancelled a few seconds later for some reason!

?


 

Below is the script I am using:
?
from ib_insync import *
import xlwings as xw


def sendOptionsOrders():
"""
Places options orders with profit target and stop loss using parameters from Excel.
Uses bracket orders to ensure proper handling of profit target and stop loss.
"""
# Create connection
ib = IB()
ib.connect(host='127.0.0.1', port=7496, clientId=1)

wb = xw.Book("SendOrders(Pairs).xlsm")
ws1 = wb.sheets["Options"]

# Get values from Excel
symbol = ws1['B2'].value # Stock ticker symbol
right = ws1['C2'].value # 'C' for Call, 'P' for Put
strike = float(ws1['D2'].value) # Strike price
expiry = ws1['E2'].value # Format: 'YYYYMMDD'
quantity = int(ws1['F2'].value) # Number of contracts
quantity2 = int(ws1['F3'].value)
entry_price = float(ws1['G2'].value) # Limit price for entry
profit_target = float(ws1['H2'].value) # Profit target price
profit_target2 = float(ws1['H3'].value)
stop_loss = float(ws1['I2'].value) # Stop loss price


# Create the option contract
option_contract = Option(symbol, expiry, strike, right, 'SMART', tradingClass=symbol)
ib.qualifyContracts(option_contract)

# Create bracket order
bracket = createBracketOrder(
orderId=ib.client.getReqId(),
action='BUY',
quantity=quantity,
limitPrice=entry_price,
takeProfitPrice=profit_target,
stopLossPrice=stop_loss
)

# Place all orders in the bracket
for order in bracket:
trade = ib.placeOrder(option_contract, order)
#print(f"Order placed: {order.action} {quantity} {symbol} {right} {strike} @ {expiry}")
ib.sleep(1)

# Create the option contract
option_contract = Option(symbol, expiry, strike, right, 'SMART', tradingClass=symbol)
ib.qualifyContracts(option_contract)

# Create bracket order
bracket2 = createBracketOrder(
orderId=ib.client.getReqId(),
action='BUY',
quantity=quantity2,
limitPrice=entry_price,
takeProfitPrice=profit_target2,
stopLossPrice=stop_loss
)

# Place all orders in the bracket
for order in bracket2:
trade = ib.placeOrder(option_contract, order)
#print(f"Order placed: {order.action} {quantity} {symbol} {right} {strike} @ {expiry}")
ib.sleep(0.5)



# Update status in Excel
ws1['J2'].value = "Success!"
ws1['A2'].value = ws1['B2'].value
#print("Bracket order placed successfully!")

ib.sleep(2)
ws1['J2'].value = ""
ws1['B2'].value = ""


def createBracketOrder(orderId, action, quantity, limitPrice, takeProfitPrice, stopLossPrice):
"""
Creates a bracket order consisting of:
1. A limit order to enter the position
2. A take-profit limit order
3. A stop-loss order

Args:
orderId: Unique order ID
action: 'BUY' or 'SELL'
quantity: Number of contracts
limitPrice: Entry price
takeProfitPrice: Target price for profit
stopLossPrice: Price to exit at loss

Returns:
List of orders comprising the bracket
"""
# Determine opposite action for exit orders
exitAction = 'SELL' if action == 'BUY' else 'BUY'

# Parent order (entry order)
parent = Order()
parent.orderId = orderId
parent.action = action
parent.orderType = 'LMT'
parent.totalQuantity = quantity
parent.lmtPrice = limitPrice
parent.transmit = False # Important: Don't transmit until all child orders are prepared

# Take profit order (limit order)
takeProfit = Order()
takeProfit.orderId = orderId + 1
takeProfit.action = exitAction
takeProfit.orderType = 'LMT'
takeProfit.totalQuantity = quantity
takeProfit.lmtPrice = takeProfitPrice
takeProfit.parentId = orderId
takeProfit.transmit = False

# Stop loss order
stopLoss = Order()
stopLoss.orderId = orderId + 2
stopLoss.action = exitAction
stopLoss.orderType = 'STP'
stopLoss.totalQuantity = quantity
stopLoss.auxPrice = stopLossPrice
stopLoss.parentId = orderId
stopLoss.transmit = True # Transmit all orders now

bracketOrder = [parent, takeProfit, stopLoss]
return bracketOrder



 

Okay done more research. Looks like IB does not allow such groups of orders on options!


 

¿ªÔÆÌåÓý

You would have simultaneous buy & sell open orders on the same option contract. That's not allowed. Not an IBKR limitation, but a limitation of US option markets where only market makers can have simultaneous buy & sell orders open on the same option contract.
?
Since both your orders are at same entry price, you can just have 1 open order for the total qty, and then have 2 different close orders, each with its qty, profit & stop loss targets, and then just link them together in an OCA group so you get the overfill protection.

rgds, Bart
On May 5, 2025 at 2:19?PM -0700, nesbitcrab via groups.io <nesbitcrab@...>, wrote:

Okay done more research. Looks like IB does not allow such groups of orders on options!


 

Thanks Bart. I appreciate your help. If it is not too much trouble, could you show me how to implement your suggestion? Thank you.