¿ªÔÆÌåÓý

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

Placing future order with stop loss and take profit


gil zur
 

Hi all,

I'm trying to place an order with take profit and stop loss without success.

Where is my mistake?

ibcontract = IBcontract()
? ? ? ? ibcontract.secType = "FUT"
? ? ? ? ibcontract.lastTradeDateOrContractMonth = "201809"
? ? ? ? ibcontract.symbol = "ES"
? ? ? ? ibcontract.exchange = "GLOBEX"
?
? ? ? ? bracket = CustomOrder().BracketOrder(1, "Buy", 1, 2925.01, 2925.31, 2909.43)
? ? ? ? for o in bracket:
? ? ? ? ? ? self.placeOrder(o.orderId,ibcontract, o)
?
? ? def BracketOrder(self,parentOrderId: int, action: str, quantity: float,limitPrice:float, takeProfitLimitPrice: float,stopLossPrice:float):
? ? ? ? parent = Order()
? ? ? ? parent.orderId = parentOrderId
? ? ? ? parent.action = action
? ? ? ? parent.orderType = "LMT"
? ? ? ? parent.totalQuantity = quantity
? ? ? ? parent.lmtPrice = limitPrice
? ? ? ? parent.transmit = False
?
? ? ? ? takeProfit = Order()
? ? ? ? takeProfit.orderId = parent.orderId + 1
? ? ? ? takeProfit.action = "SELL" if action == "BUY" else "BUY"
? ? ? ? takeProfit.orderType = "LMT"
? ? ? ? takeProfit.totalQuantity = quantity
? ? ? ? takeProfit.lmtPrice = takeProfitLimitPrice
? ? ? ? takeProfit.parentId = parentOrderId
? ? ? ? takeProfit.transmit = False
?
? ? ? ? stopLoss = Order()
? ? ? ? stopLoss.orderId = parent.orderId + 2
? ? ? ? stopLoss.action = "SELL" if action == "BUY" else "BUY"
? ? ? ? stopLoss.orderType = "STP"
? ? ? ? stopLoss.auxPrice = stopLossPrice
? ? ? ? stopLoss.totalQuantity = quantity
? ? ? ? stopLoss.parentId = parentOrderId
? ? ? ? stopLoss.transmit = True
?
? ? ? ? bracketOrder = [parent, takeProfit, stopLoss]
? ? ? ? return bracketOrder


 

¿ªÔÆÌåÓý

Help us to help you!

?

What happens when you do this? Do you get an error message returned via the API? If so, what does it say? If not, make sure you are handling the error message callback (I don¡¯t know what it¡¯s called in Python, but there must be one and it¡¯s imperative that you handle it.

?

Richard

?

From: [email protected] <[email protected]> On Behalf Of gil zur
Sent: 29 August 2018 20:39
To: [email protected]
Subject: [TWS API] Placing future order with stop loss and take profit

?

Hi all,

I'm trying to place an order with take profit and stop loss without success.

Where is my mistake?

ibcontract = IBcontract()

? ? ? ? ibcontract.secType = "FUT"

? ? ? ? ibcontract.lastTradeDateOrContractMonth = "201809"

? ? ? ? ibcontract.symbol = "ES"

? ? ? ? ibcontract.exchange = "GLOBEX"

?

? ? ? ? bracket = CustomOrder().BracketOrder(1, "Buy", 1, 2925.01, 2925.31, 2909.43)

? ? ? ? for o in bracket:

? ? ? ? ? ? self.placeOrder(o.orderId,ibcontract, o)

?

? ? def BracketOrder(self,parentOrderId: int, action: str, quantity: float,limitPrice:float, takeProfitLimitPrice: float,stopLossPrice:float):

? ? ? ? parent = Order()

? ? ? ? parent.orderId = parentOrderId

? ? ? ? parent.action = action

? ? ? ? parent.orderType = "LMT"

? ? ? ? parent.totalQuantity = quantity

? ? ? ? parent.lmtPrice = limitPrice

? ? ? ? parent.transmit = False

?

? ? ? ? takeProfit = Order()

? ? ? ? takeProfit.orderId = parent.orderId + 1

? ? ? ? takeProfit.action = "SELL" if action == "BUY" else "BUY"

? ? ? ? takeProfit.orderType = "LMT"

? ? ? ? takeProfit.totalQuantity = quantity

? ? ? ? takeProfit.lmtPrice = takeProfitLimitPrice

? ? ? ? takeProfit.parentId = parentOrderId

? ? ? ? takeProfit.transmit = False

?

? ? ? ? stopLoss = Order()

? ? ? ? stopLoss.orderId = parent.orderId + 2

? ? ? ? stopLoss.action = "SELL" if action == "BUY" else "BUY"

? ? ? ? stopLoss.orderType = "STP"

? ? ? ? stopLoss.auxPrice = stopLossPrice

? ? ? ? stopLoss.totalQuantity = quantity

? ? ? ? stopLoss.parentId = parentOrderId

? ? ? ? stopLoss.transmit = True

?

? ? ? ? bracketOrder = [parent, takeProfit, stopLoss]

? ? ? ? return bracketOrder


 

The minimum increment on the ES mini is 0.25, so the prices would all need to be even multiples of this.


gil zur
 

On Wed, Aug 29, 2018 at 01:40 PM, Josh wrote:
So for example - instead of? 2925.01 ->? 2925.25 ?


 

I would suggest first implementing Richard's suggestions, but yes the prices needed to be rounded to multiple of 0.25 before being sent.