Ib Api

Discussion in 'Automated Trading' started by rverheyen, Apr 5, 2011.

  1. My strategy creates, at a certain time and for a certain stock, 2 opposite orders. It's a breakout strategy that work in both directions.

    Both sides have 3 orders:

    The Long Entry order (STP LMT).
    + Long Stop order (STP) bracketed to the Long Entry order
    + Long Target order (LMT) bracketed to the Long Entry order

    The Short Entry order (STP LMT).
    + Short Stop order (STP) bracketed to the Short Entry order
    + Short Target order (LMT) bracketed to the Short Entry order

    Both the long and short brackets work great on their own. They keep in sync with fills and cancel each other when needed.

    My strategy doesn't permit to "reverse". Once a side is filled the opposite site is canceled.

    Currently I do this in code. If the Long Entry order is filled (with at leased 1 share) then the Short order is canceled (the bracket orders get canceled automatically).

    This works but it is a 'risk' factor. My application needs to be connected with TWS & TWS with the server @ IB). If I lose my connection or TWS or my app crashes or gets closed the orders don't get canceled.

    I would like to cancel the opposite order in TWS (using order parameters).

    But is this possible?
  2. Mr_RC


    Not as far as I know.

    If the orders are NOT just a few ticks away, you could create the orders but not submit them to the broker. Once price gets close submit the bracket it gets close to. When price moves away again, disable the bracket again / or cancel and re-create. This way you have a maximum of one bracket on the line.

    Note that:

    (a) with IB this costs a little bit but will be remedied by a future execution unless you send/cancel/change many times
    (b) according to the exchange order matching algo you may loose your queue position for the execution
  3. Create an OCA group for the entry orders. When one gets hit the other cancels (together with its bracket).
  4. Tnx Ron

    When I was driving home from work (arg, traffic jam) I thought about this as well.

    Will try this.
  5. Works!

    Looks like TWS can handle nested bracket orders.

    I have set both Entry orders (long & short) with the same ocaGroup and when one fires the other one is canceled and when canceled the stop & target orders get canceled as well.

    I used ocaType = 1 to cancel the complete order (and not reduce size if partially filled).
  6. aripplet


    OCA, read all supported order types documentation since you came to this level.

    But I don't think it's not that easy to do such scalping this way, it's very tricky to set up an optimal stop loss order. I think it will often be taken advantage of by market markers as they can identify such type which has high degree of commitment.
    There surely is no way to backtest, nor paper account testing it.
    But it's a trade-off, unless your program could set up an loss stop mechanism yourself and submit it fast enough.

    regarding the connection/power issue, ppl use remote server from amazon or some company like that with reasonable cost.