IB API Stop order question

Discussion in 'Automated Trading' started by travis, Jan 16, 2008.

  1. travis

    travis

    I have had an accident on my excel sheet today. My automated system failed to close a position, and as a consequence my loss far exceeded the maximum drawdown expected for the system.

    I realized that fixing the excel sheet is not enough, and that I must find a way to send a stoploss order to IB, preventing such huge losses in case of any accidents.

    I am trying to figure it out but I don't want to send an order to open the position, and then an ensuing stop order. I want to send it all at once. Is it possible? Maybe I have to use the trailing stop? How do I do that? Thank you.
     
  2. Defintely possible with java auto api and manually with TWS.

    Dont know about the Excel API, would have thought it was possible, you need to refer to the API docs.
     
  3. travis

    travis

    Yes, I knew that, because I saw it even on the web interface and used it. You set a trailing stop when you give the order to open the position. Then you are done. Your connection could go down and you would be fine.

    I tried looking at the documents, but unfortunately there are no examples. Just 1 example, on how to send a LMT order. Nothing else. I spent years working on this system, and my (small) account was almost wiped out today because the trade was right but it didn't get executed, since the excel sheet didn't send the order to close the position.

    It certainly taught me a lesson. Now I need to find a way so that no matter what happens on my computer/connection, IB still closes the position.
     
  4. Jachyra

    Jachyra

    You can use the IB TWS API to submit bracket orders that IB holds server side, that way your broker is shouldering the majority of the burden of executing your orders as per your original intentions, which is good since they probably have the best chance of not screwing it up. They still could screw it up, and I'm sure that the account documentation you signed agreed to hold them harmless in the event they do mess something up, but its probably a lot less likely than excel not working as expected or suddenly losing internet connectivity.
     
  5. lindq

    lindq

    IMHO, it is dangerous to rely on any system to set stops. I use a very reliable system interfaced via API with TWS, but even so I always go right to TWS to set hard and trailing stops, and double check that they are in place.

    If you are in a position that you must rely on Excel, or any software resident on your drive to protect you, you'll always be in some jeopardy.

    Sorry for your loss. Lessons in this business are often expensive.
     
  6. travis

    travis

    Thank you, I will try bracket orders, even though it sounds complicated. I hoped that a trailing order would do, but I don't know if it has to be separate or if you can use the trailing order to also open the position (just like you can do on the web interface).

    You see, the advantage of such a trailing order (if it is indeed just one order for the whole thing) is that it is simple and that it works both as a stoploss and as a traling stop (I would make it really wide so it doesn't conflict with my system). At the same time such a trailing stop would cease to exist if my trade was closed by the excel system at home.

    Actually, my problem is very similar to the problem described by Tony Qiu on the IB board - TWS API section:
    http://www.interactivebrokers.com/cgi-bin/discus/board-auth.pl?file=/2/42983.html

    "...I know what you mean, I can send trailling stop separately, but i need to waiting for limit order is filled first, then i can send trailliing stop order to TWS afer i received Orderfiled event. sometime, because network or application issue, client lose connect to TWS, if limit order is filled, client cannot send trailling stop order to TWS, It is too risk, that is reason i want to send limit order ticket with trailing stop order together using TWS api."
     
  7. travis

    travis

  8. travis

    travis

    Ok, I think I am getting there... maybe:

    Below the "parent order", I need to place a "child order", which in my case will be a trailing stop.

    So, on the first line, the "parent order line", everything will be just as now, except for the "Transmit (0/1)" line, which will have to be "false" and therefore set to "0".

    The attached order will need its parentId set to the id of the parent order, and the "Transmit (0/1)" line will have to be "true" and therefore set to "1".

    If we will send the entry order with "false" on the "Transmit (0/1)" cell, then our trailing stop order won't be activated until the entry order fills. If we send first our trailing stop order with "true" on the transmit line, then I think we might have a problem.
     
  9. travis

    travis

    Ok, I have no idea if it will work because since my account was wiped out, momentarily I still can't test this code.

    Here's what I have figured out:
    http://www.interactivebrokers.com/cgi-bin/discus/board-auth.pl?file=/2/34260.html

    The IB expert Richard King says:
    "If you create a bracket order via the api, it behaves exactly the way you want. If either the target or the stop order is executed, then the other is cancelled. Setting the parent id on the target and stop orders to the id of the entry order implicitly places the target and stop orders in an OCA group. You could explicitly set the ocaGroup property for the target and stop orders if you want, but it's not necessary."

    OK, so this means that I will divide my orders like this:

    1) hypothetically at 10 AM: entry "parent" MKT order gets sent with Transmit = false

    2) hypothetically at 10 AM: "child" TRAIL (emergency exit in case my pc crashes) gets sent with parent id of entry "parent" order and with transmit = true

    Now, the two orders above were sent simultaneously to IB's server and no matter what happens at home, I should be safe.

    3) hypothetically at 11 AM: if my computer didn't crash or similar by then (which will be the case 99% of time), the automated trading system decides that it is time to close the position and sends to IB's server an exit "child" MKT order with parent id of entry "parent" order and with transmit = true.

    At this point, correct me if I am wrong, Richard King says that this order should, by being executed, cancel the other "child" order.

    Ok, if no one else helps out, I will have to risk another few dollars to test this out next week.
     
  10. travis

    travis

    Ok, I solved the problem, and it was easier than I thought. I had forgotten that there is a "CancelOrder" macro within the IB TWS API Excel sheet.

    So, all I had to do is use these 4 instructions:

    1) send the parent entry order with transmit "false"
    2) send the child TRAIL order with transmit "true"

    This so far makes sure that the entry is not made unless there is a secure emergency exit in place. At this point even if my pc crashes, the connection is lost or there is an earthquake, my trade will still be closed by the broker (and not be closed due to insufficient margin like it happened a few days ago). If no accidents happen the instructions will continue:

    3) exit the trade by sending a MKT order (with transmit "true")
    4) cancel the emergency exit with IB's cancelorder macro (on orders sheet)

    There still is a possible mishap, even though very remote. If you exit the trade and, one second later, get disconnected before you can cancel the emergency exit, that could still execute against you (even if you are flat a trailing order can be executed). If you instead cancel the trailing order and, one second later, don't manage to send the system's regular exit order, then you're leaving your trade open.

    Unfortunately I didn't find a way to send both the real exit order and cancel the emergency exit at once.
     
    #10     Jan 19, 2008