Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Bracket order with several take profit #694

@ingelrii

Description

@ingelrii

Hello,
I’m trying to set a bracket order, and I want to place a parent order with several take profits, each one with different targets. To do it, I place 3 bracket orders, but when the first one is filled, I get the error 201-cancelled order ‘you cannot have both sides of the contract…’.

this is the code I run:

def our_BracketOrder(ib, n_buy_contracts, opt_contract):
parent = Order()
parent.orderId = ib.client.getReqId()
parent.action = "BUY"
parent.orderType = "MKT"
parent.totalQuantity = n_buy_contracts
parent.transmit = False

takeProfit = Order()
takeProfit.orderId = parent.orderId + 1
takeProfit.action = "SELL"
takeProfit.orderType = "LMT"
takeProfit.totalQuantity = n_buy_contracts
takeProfit.lmtPrice = 3
takeProfit.parentId = parent.orderId
takeProfit.transmit = False

stopLoss = Order()
stopLoss.orderId = parent.orderId + 2
stopLoss.action = "SELL"
stopLoss.orderType = "STP"
stopLoss.auxPrice = 0.5
stopLoss.totalQuantity = n_buy_contracts
stopLoss.parentId = parent.orderId
stopLoss.transmit = True

BO = {'parent': parent, 'takeProfit': takeProfit, 'stopLoss':stopLoss}
return BO

def our_BracketOrder2(ib, n_buy_contracts, opt_contract):
parent2 = Order()
parent2.orderId = ib.client.getReqId()
parent2.action = "BUY"
parent2.orderType = "MKT"
parent2.totalQuantity = n_buy_contracts
parent2.transmit = False

takeProfit2 = Order()
takeProfit2.orderId = parent2.orderId + 1
takeProfit2.action = "SELL"
takeProfit2.orderType = "LMT"
takeProfit2.totalQuantity = n_buy_contracts
takeProfit2.lmtPrice = 3
takeProfit2.parentId = parent2.orderId
takeProfit.transmit = False

stopLoss2 = Order()
stopLoss2.orderId = parent2.orderId + 2
stopLoss2.action = "SELL"
stopLoss2.orderType = "STP"
stopLoss2.auxPrice = 0.5
stopLoss2.totalQuantity = n_buy_contracts
stopLoss2.parentId = parent2.orderId
stopLoss2.transmit = True

BO2 = {'parent': parent2, 'takeProfit': takeProfit2, 'stopLoss':stopLoss2}
return BO2

The lmtPrice and auxPrice should be changed once the first parent order has been filled.

I think maybe I could change the order as it follows:

def our_BracketOrder(ib, n_buy_contracts, opt_contract):
parent = Order()
parent.orderId = ib.client.getReqId()
parent.action = "BUY"
parent.orderType = "MKT"
parent.totalQuantity = n_buy_contracts
parent.transmit = False

takeProfit = Order()
takeProfit.orderId = parent.orderId + 1
takeProfit.action = "SELL"
takeProfit.orderType = "LMT"
takeProfit.totalQuantity = n_buy_contracts
takeProfit.lmtPrice = 3
takeProfit.parentId = parent.orderId
takeProfit.transmit = False

stopLoss = Order()
stopLoss.orderId = parent.orderId + 2
stopLoss.action = "SELL"
stopLoss.orderType = "STP"
stopLoss.auxPrice = 0.5
stopLoss.totalQuantity = n_buy_contracts
stopLoss.parentId = parent.orderId
stopLoss.transmit = True

parent2 = Order()
parent2.orderId = ib.client.getReqId()
parent2.action = "BUY"
parent2.orderType = "MKT"
parent2.totalQuantity = n_buy_contracts
parent2.transmit = False

takeProfit2 = Order()
takeProfit2.orderId = parent2.orderId + 1
takeProfit2.action = "SELL"
takeProfit2.orderType = "LMT"
takeProfit2.totalQuantity = n_buy_contracts
takeProfit2.lmtPrice = 3
takeProfit2.parentId = parent2.orderId
takeProfit.transmit = False

stopLoss2 = Order()
stopLoss2.orderId = parent2.orderId + 2
stopLoss2.action = "SELL"
stopLoss2.orderType = "STP"
stopLoss2.auxPrice = 0.5
stopLoss2.totalQuantity = n_buy_contracts
stopLoss2.parentId = parent2.orderId
stopLoss2.transmit = True

BO = {'parent': parent, 'takeProfit': takeProfit, 'stopLoss':stopLoss, 'parent2': parent2, 'takeProfit2': takeProfit2, 'stopLoss2':stopLoss2}
return BO

or maybe like this:

def our_BracketOrder(ib, n_buy_contracts, opt_contract):
parent = Order()
parent.orderId = ib.client.getReqId()
parent.action = "BUY"
parent.orderType = "MKT"
parent.totalQuantity = n_buy_contracts
parent.transmit = False

takeProfit = Order()
takeProfit.orderId = parent.orderId + 1
takeProfit.action = "SELL"
takeProfit.orderType = "LMT"
takeProfit.totalQuantity = n_buy_contracts/2
takeProfit.lmtPrice = 3
takeProfit.parentId = parent.orderId
takeProfit.transmit = False


takeProfit2 = Order()
takeProfit2.orderId = parent.orderId + 1
takeProfit2.action = "SELL"
takeProfit2.orderType = "LMT"
takeProfit2.totalQuantity = n_buy_contracts/2
takeProfit2.lmtPrice = 3
takeProfit2.parentId = parent.orderId
takeProfit.transmit = False

stopLoss = Order()
stopLoss.orderId = parent.orderId + 2
stopLoss.action = "SELL"
stopLoss.orderType = "STP"
stopLoss.auxPrice = 0.5
stopLoss.totalQuantity = n_buy_contracts
stopLoss.parentId = parent.orderId
stopLoss.transmit = True

BO = {'parent': parent, 'takeProfit': takeProfit, 'takeProfit2': takeProfit2, 'stopLoss':stopLoss}
return BO

Has somebody got any idea how to set this order properly?

thank you very much

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions