开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Can a bracket order have two take profit orders to exit a position at two different profit levels?


 

I would like to know if there is a mechanism that would allow me to place a position opening order for N contracts, together with two take profit orders, each of size N/2 but at different price levels.
If I create a bracket order with these 3 orders, when the position opening order fills the two take profit orders will be activated, but they will belong to the same OCA group, so that when the first one fills, the other will get cancelled automatically, which is not what I want.


 

开云体育

No, there's no easy way to do that.

?

Obviously you can program this so that you create a bracket order with a stop-loss but no take-profit: as soon as the entry order fills, create the two take-profit orders, and then monitor the stop-loss so that if it fills, you can cancel the take-profits. Easy enough, provided you use a stop order for your stop-loss rather than a stop-limit, which then gives you potential complications with partial fills. And of course ?your program has to keep running to ensure that the take-profits are cancelled if need be.

?

A possible alternative that I haven't tried but seems like it should work is to create your initial bracket order with stop-loss and a single take-profit, and then when the entry order fills, halve the size of the OCA group containing the stop-loss and take-profit, and then create another OCA group containing another half-size stop-loss and the other take-profit. Once that's done you can walk away, as it were. The only question in my mind is whether you can actually change the size of an OCA group that was once attached to a parent order, but I can't see why it wouldn't be allowed. I'll try this when I've got a few spare minutes.

?

?

From: [email protected] <[email protected]> On Behalf Of Levente
Sent: 28 April 2021 22:48
To: [email protected]
Subject: [TWS API] Can a bracket order have two take profit orders to exit a position at two different profit levels?

?

I would like to know if there is a mechanism that would allow me to place a position opening order for N contracts, together with two take profit orders, each of size N/2 but at different price levels.
If I create a bracket order with these 3 orders, when the position opening order fills the two take profit orders will be activated, but they will belong to the same OCA group, so that when the first one fills, the other will get cancelled automatically, which is not what I want.


 

开云体育

I tried out my suggestion and it seems to work fine. I bought 1000 shares of TSCO at market at the LSE, with a stop-loss STP order at 215 and a take-profit LMT order at 225.

?

After the entry market order filled, I modified both the stop-loss and take-profit orders to a size of 500 and set the parentId to 0 in both.

?

Then I create a new OCA group with a sell 500 STP order at 215 and a sell 500 LMT at 223.

?

The TWS API tab now looks like the attached screenshot. Note that this shows a Position of 3200 – that's because I already had 2200 TSCO in the portfolio.

?

Because there are four PLACE_ORDER operations to do after the entry order has filled, there is still a small risk of getting into trouble if you lose your TWS connection for any reason during that sequence: but given that you can just bang these operations in within a very short time, you'd have to be very unlucky.

?

Also some refinement would presumably be needed if the entry order had partial fills: you'd have to monitor the executions until the entry order was fully filled.

?

?

?

?

?

?

From: [email protected] <[email protected]> On Behalf Of Richard L King
Sent: 29 April 2021 10:18
To: [email protected]
Subject: Re: [TWS API] Can a bracket order have two take profit orders to exit a position at two different profit levels?

?

No, there's no easy way to do that.

?

Obviously you can program this so that you create a bracket order with a stop-loss but no take-profit: as soon as the entry order fills, create the two take-profit orders, and then monitor the stop-loss so that if it fills, you can cancel the take-profits. Easy enough, provided you use a stop order for your stop-loss rather than a stop-limit, which then gives you potential complications with partial fills. And of course ?your program has to keep running to ensure that the take-profits are cancelled if need be.

?

A possible alternative that I haven't tried but seems like it should work is to create your initial bracket order with stop-loss and a single take-profit, and then when the entry order fills, halve the size of the OCA group containing the stop-loss and take-profit, and then create another OCA group containing another half-size stop-loss and the other take-profit. Once that's done you can walk away, as it were. The only question in my mind is whether you can actually change the size of an OCA group that was once attached to a parent order, but I can't see why it wouldn't be allowed. I'll try this when I've got a few spare minutes.

?

?

From: [email protected] <[email protected]> On Behalf Of Levente
Sent: 28 April 2021 22:48
To: [email protected]
Subject: [TWS API] Can a bracket order have two take profit orders to exit a position at two different profit levels?

?

I would like to know if there is a mechanism that would allow me to place a position opening order for N contracts, together with two take profit orders, each of size N/2 but at different price levels.
If I create a bracket order with these 3 orders, when the position opening order fills the two take profit orders will be activated, but they will belong to the same OCA group, so that when the first one fills, the other will get cancelled automatically, which is not what I want.


 

Why not create two identical parent orders at the same price with 1/2 of total amounts each and just have two brackets at different stops/limits?


Nick
 

I don't know the commission impact or if it fits with your strategy but perhaps you could place two brackets each for N/2 shares. This would leave you always protected and you wouldn't have to deal with possible outages.

There's no perfect solution so it's a matter of picking the least evil alternative.

On 4/28/2021 5:47 PM, Levente wrote:
I would like to know if there is a mechanism that would allow me to place a position opening order for N contracts, together with two take profit orders, each of size N/2 but at different price levels.


 

I recommend that you create two sets of orders with size N/2. Each set includes a parent order and the bracket order (stop loss and target limit).

--


 

Unfortunately I did not have the luxury (time) to try this out myself, so a BIG thank you to Richard.
However, I managed to call IB API support and was suggested the following:

Create 5 (!!!) orders
1. Position opening order (assume ID is 1)
2. Stop loss order for the full amount (assume ID is 2) + set parent to 1
3. Take profit order for HALF of the amount?(assume ID is 3) + set parent to 1
4. Take profit order for HALF of the amount at the other price level (assume ID is 4)? + set parent to 3
5. Stop loss order for HALF of the amount (assume ID is 5) + set parent to 3

When position opening order fills, orders with ID 2 and 3 will get activated.
If stop loss order 2 fills, the take profit 3 will get cancelled and orders 4 and 5 will never activate.
If take profit order 3 fills, the stop loss order 2 will get cancelled, and orders 4 and 5 will get activated and will belong to a NEW OCA group.

I will try this out and will post my findings.


Nick
 

Ruh roh, the forum may have gotten pwned by IP support.

On 4/29/2021 11:22 AM, Levente wrote:
However, I managed to call IB API support and was suggested the following:


 

I actually tried that (and slight variations) when I saw your original post last night, but it did not work for me.

What happens is that IB automatically sets the quantity for #3 to the same full amount of #1. I guess the basic idea is that the profit and stop legs of a bracket order should protect the entire position.

If you just place orders #1, #3, and #4 (no stop loss orders in the brackets) you can actually adjust quantities to Full/Half/Half right after the three orders are placed. Both via API or TWS. But once #1 was filled, IB again adjusted quantity of? #3 to the actually filled quantity. Again it was possible to adjust the quantity of #3 (the now active order) to Half via API or TWS.

When placing all five orders, quantity changes failed via API and the fields were greyed out in TWS.

JR


On Thu, Apr 29, 2021 at 10:22 AM, Levente wrote:
Unfortunately I did not have the luxury (time) to try this out myself, so a BIG thank you to Richard.
However, I managed to call IB API support and was suggested the following:

Create 5 (!!!) orders
1. Position opening order (assume ID is 1)
2. Stop loss order for the full amount (assume ID is 2) + set parent to 1
3. Take profit order for HALF of the amount?(assume ID is 3) + set parent to 1
4. Take profit order for HALF of the amount at the other price level (assume ID is 4)? + set parent to 3
5. Stop loss order for HALF of the amount (assume ID is 5) + set parent to 3

When position opening order fills, orders with ID 2 and 3 will get activated.
If stop loss order 2 fills, the take profit 3 will get cancelled and orders 4 and 5 will never activate.
If take profit order 3 fills, the stop loss order 2 will get cancelled, and orders 4 and 5 will get activated and will belong to a NEW OCA group.

I will try this out and will post my findings.


 

Forgot to attached a couple screen grabs.

First the five orders. Please note that 23.1 was placed with a quantity of 500.via the API.



Attempt to change quantity of 23.1 via API fails without error and TWS quanitty field is greyed out.


 

Yeah, AFAIK it's not possible to set different sizes in a simple bracket order so it's pretty perplexing that IB support would suggest that. It is possible with the pair-trade variety of the bracket order but even then the sizes are not set independently but using a beta-ratio.