IB api - closing only orders (equities)

Discussion in 'App Development' started by qlai, Jan 12, 2019.

  1. qlai

    qlai

    Hello, I use multiple trailing stops and profit taking orders without linking them. So for example, let's say I have 300 shares position and I have 3x100 trailing stop orders and 3x100 share profit taking orders active.
    What happens from time to time is that I inadvertently get my position reversed when the market gets spikey. For example, one of my profit orders gets filled and I'm left with 200 position but 300 shares worth of trailing stop orders. I need to cancel one of the trailing stops.

    Is there a way to do one of below?
    1. Indicate that the order is a closing only order? I didn't find anything in API like that.
    2. Configure TWS so that it will reject a sell order if it's not explicitly marked as 'sell short' but would result in a short position? Or at least prevent me from being able to flip position with a single order.

    I assume none of the above, so do you guys link the orders to achieve this or track the position yourself?

    Thanks
     
  2. southall

    southall

    What is the reason for not linking each profit taking exit with the corresponding stop loss by using a oca_group id?
     
    d08 likes this.
  3. jufils

    jufils

    You should be able to do what you want with the One-Cancels-All (OCA) order type.

    Remaining orders within the same OCA group will all be cancelled if one of them is filled. So you had a take profit order and a trailing stop order in the same OCA group, then if the take profit order is filled it would then automatically cancel the trailing stop (or vice versa).
     
    tommcginnis likes this.
  4. qlai

    qlai

    Yes I could do the linking but I would prefer to not have that dependency ... Have them independent of each other yet not oversell/overbuy. It's not a big deal, but would be nice to be able to specify "close only"
     
  5. You can tag every order in the api. But then you need to handle all the logic yourself.

     
    tommcginnis likes this.
  6. It would be easiest if each profit taker order was linked to a trailing stop order as a OCA pair. In that case you can "fire and forget" these order pairs.
    If your software is constantly monitoring your open position you would have some other possibilities. In that case you don't need to use a "fire and forget" strategy for your orders, but can place them when the exit conditions are met. One of these exit conditions could then be to "close only" and not go into the opposite direction.
    The API also has "conditional orders" available. But I don't think that the current position size can be used as a condition.
     
    tommcginnis likes this.