开云体育

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

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

 

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.


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

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:


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

 

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.


Re: Custom Scanners?

 

Here is a list of available scancodes. I don't know if there are more.

TOP_PERC_GAIN,
? TOP_PERC_LOSE,
? MOST_ACTIVE,
? ALL_SYMBOLS_ASC,
? ALL_SYMBOLS_DESC,
? BOND_CUSIP_AZ,
? BOND_CUSIP_ZA,
? FAR_MATURITY_DATE,
? HALTED,
? HIGH_BOND_ASK_CURRENT_YIELD_ALL,
? HIGH_BOND_ASK_YIELD_ALL,
? HIGH_BOND_DEBT_2_BOOK_RATIO,
? HIGH_BOND_DEBT_2_EQUITY_RATIO,
? HIGH_BOND_DEBT_2_TAN_BOOK_RATIO,
? HIGH_BOND_EQUITY_2_BOOK_RATIO,
? HIGH_BOND_EQUITY_2_TAN_BOOK_RATIO,
? HIGH_BOND_NET_ASK_CURRENT_YIELD_ALL,
? HIGH_BOND_NET_ASK_YIELD_ALL,
? HIGH_BOND_NET_SPREAD_ALL,
? HIGH_BOND_SPREAD_ALL,
? HIGH_COUPON_RATE,
? HIGH_DIVIDEND_YIELD,
? HIGH_DIVIDEND_YIELD_IB,
? HIGHEST_SLB_BID,
? HIGH_GROWTH_RATE,
? HIGH_MOODY_RATING_ALL,
? HIGH_OPEN_GAP,
? HIGH_OPT_IMP_VOLAT,
? HIGH_OPT_IMP_VOLAT_OVER_HIST,
? HIGH_OPT_OPEN_INTEREST_PUT_CALL_RATIO,
? HIGH_OPT_VOLUME_PUT_CALL_RATIO,
? HIGH_PE_RATIO,
? HIGH_PRICE_2_BOOK_RATIO,
? HIGH_PRICE_2_TAN_BOOK_RATIO,
? HIGH_QUICK_RATIO,
? HIGH_RETURN_ON_EQUITY,
? HIGH_SYNTH_BID_REV_NAT_YIELD,
? HIGH_VS_13W_HL,
? HIGH_VS_26W_HL,
? HIGH_VS_52W_HL,
? HOT_BY_OPT_VOLUME,
? HOT_BY_PRICE,
? HOT_BY_PRICE_RANGE,
? HOT_BY_VOLUME,
? LIMIT_UP_DOWN,
? LOW_BOND_BID_CURRENT_YIELD_ALL,
? LOW_BOND_BID_YIELD_ALL,
? LOW_BOND_DEBT_2_BOOK_RATIO,
? LOW_BOND_DEBT_2_EQUITY_RATIO,
? LOW_BOND_DEBT_2_TAN_BOOK_RATIO,
? LOW_BOND_EQUITY_2_BOOK_RATIO,
? LOW_BOND_EQUITY_2_TAN_BOOK_RATIO,
? LOW_BOND_NET_BID_CURRENT_YIELD_ALL,
? LOW_BOND_NET_BID_YIELD_ALL,
? LOW_BOND_NET_SPREAD_ALL,
? LOW_BOND_SPREAD_ALL,
? LOW_COUPON_RATE,
? LOWEST_SLB_ASK,
? LOW_GROWTH_RATE,
? LOW_MOODY_RATING_ALL,
? LOW_OPEN_GAP,
? LOW_OPT_IMP_VOLAT,
? LOW_OPT_IMP_VOLAT_OVER_HIST,
? LOW_OPT_OPEN_INTEREST_PUT_CALL_RATIO,
? LOW_OPT_VOLUME_PUT_CALL_RATIO,
? LOW_PE_RATIO,
? LOW_PRICE_2_BOOK_RATIO,
? LOW_PRICE_2_TAN_BOOK_RATIO,
? LOW_QUICK_RATIO,
? LOW_RETURN_ON_EQUITY,
? LOW_SYNTH_ASK_REV_NAT_YIELD,
? LOW_VS_13W_HL,
? LOW_VS_26W_HL,
? LOW_VS_52W_HL,
? LOW_WAR_REL_IMP_VOLAT,
? MARKET_CAP_USD_ASC,
? MARKET_CAP_USD_DESC,
? MOST_ACTIVE_AVG_USD,
? MOST_ACTIVE_USD,
? NEAR_MATURITY_DATE,
? NOT_OPEN,
? OPT_OPEN_INTEREST_MOST_ACTIVE,
? OPT_VOLUME_MOST_ACTIVE,
? PMONITOR_AVAIL_CONTRACTS,
? PMONITOR_CTT,
? PMONITOR_IBOND,
? PMONITOR_RFQ,
? TOP_OPEN_PERC_GAIN,
? TOP_OPEN_PERC_LOSE,
? TOP_OPT_IMP_VOLAT_GAIN,
? TOP_OPT_IMP_VOLAT_LOSE,
? TOP_PRICE_RANGE,
? TOP_STOCK_BUY_IMBALANCE_ADV_RATIO,
? TOP_STOCK_SELL_IMBALANCE_ADV_RATIO,
? TOP_TRADE_COUNT,
? TOP_TRADE_RATE,
? TOP_VOLUME_RATE,
? WSH_NEXT_ANALYST_MEETING,
? WSH_NEXT_EARNINGS,
? WSH_NEXT_EVENT,
? WSH_NEXT_MAJOR_EVENT,
? WSH_PREV_ANALYST_MEETING,
? WSH_PREV_EARNINGS,
? WSH_PREV_EVENT
?


Re: Can a bracket order have two take profit orders to exit a position at two different profit 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).

--


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

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.


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

 

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?


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

 

开云体育

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.


Custom Scanners?

 

Hey guys, I've been looking through this page and it seems the ability to create scanners in Python is very limited. To my understanding, we cannot pull our already made scanners in TWS into Python? And that the scanners "are being made in python" and use the basic list on that page and this page??which isnt much. I'm trying to make a gap scanner


Re: 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.


Re: Inconsistent behaviour of WHATIF orders

 

I will test it again this weekend. Since it is now working flawlessly there is nothing to debug :-)?


Re: Inconsistent behaviour of WHATIF orders

 

I guess yes, as I'm workaholic... But no matter, if it works fine when markets are open, then it works fine!


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.


Re: Inconsistent behaviour of WHATIF orders

 

@Francois: There is no error message. Everything is returned as it should just that both change in initial margin and change in maintenance margin are returned as zero.

I tested it now all week and since the market opened on Monday it works perfectly. No problems at all. It just did not work during the weekend and Monday morning before the market opened. Have you tested your code on a weekend?


Re: Options contract volume and open interest

 

To get the current day's volume, a simple snapshot request via reqMktData(reqId, contract, "", true)?is sufficient, and you'll receive the volume as one of the returned data fields.
If you also want the open interest, then you need to use streaming quotes with a GenericTickList of 101 (see ?i.e.?reqMktData(reqId, contract, "101", false). Do not forget?to cancel the streaming quotes request after you receive the data!


Options contract volume and open interest

 

Hello.

What is the best way to get volume and open interest for an option contract?

Regards


Re: accountSummaryEnd only called once

 

Yes, you'll only see the "End" called when your initial download is complete.? Subsequent updates don't have an end per se, so it's up to you to manage how you trigger off the values changing over time.??

-Peter


Re: Inconsistent behaviour of WHATIF orders

 

This is on a paper trading account, and WHATIF orders always worked fine.


Re: Inconsistent behaviour of WHATIF orders

 

Despair, I just tried now (07:29 AM UTC) markets closed, and it works fine, either for single leg or combo whatif orders.
See attached screenshot, one is for single legs, one is for combo. I use WHATIF orders to get the margin requirements before I transmit the order.
Did you track error messages? There can be errors code 110: 'The price does not conform to the minimum price variation for this contract'.
I remember when coding my app that this error forced me to manage 'minimum price increment' through marketRuleId, for normal orders AND whatif orders.





Model Portfolios functionality in TWS API

 

Hello guys, my client needs the following functionality for their platform:
  • Create a model portfolio through the API (not so important since they could create them through TWS which they can
  • Invest clients in the model: this is the most important feature they ask. They want to be able to assign a client investor subaccount to an already created Model Portfolio. I could not find the way to do it through the API . I was wondering if you guys know how to achieve this.
  • Rebalancing models: I know this is not possible through the API as the says

Model Portfolio functionality?not?available in the TWS API:

  • Portfolio Model Creation
  • Portfolio Model Rebalancing
  • Portfolio Model Position or Cash Transfer
Basically I would like to programatically achieve what is described in the following article:


Thank you!