IB API Stop order question

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

  1. travis

    travis

    Ok, guys, another progress was made. I think I figured out a way to send both the real exit order and cancel the emergency exit at once.

    And also, here's another discovery - it seems that parent and child orders are not necessary for my purpose of sending orders two at a time. All that seems necessary is to set the first order you want to send to transmit "false", so it will be sent together with the other order.

    So here's our hypothesis: we are sending the first two orders at 10:00 AM. The first order is sent by our system, because of a signal, whereas the second order is sent also by our system but olny for safety reasons, to reside on the IB server in case our pc crashes or similar.

    1) IB macro "placeorder" is executed and sends our entry MKT order to IB: it's set to transmit "0", will be sent when the second one gets sent and then it will read "filled".

    2) IB macro "placeorder" is executed and sends our emergency exit TRAIL order to IB: it's set to transmit "1", and it will read "presubmitted"

    Now we are safe and no matter what happens to our pc at home - IB has our emergency exit in place. If nothing happens to our pc at home, we get to 11:30 AM and our system gives a signal for exit, so it will now have to send that signal to IB and at the same time cancel the emergency exit that was put in place before.

    1) IB macro "cancelorder" is executed and cancels our emergency exit TRAIL order: this row is set to transmit "0" and it will read "cancelled", once sent together with the following order

    2) IB macro "placeorder" is executed and sends our regular exit MKT order to IB: it's set to transmit "1" and it will read "filled".

    So, summarizing it: we have this pattern:

    1) "filled" regular entry order set to transmit "0"
    2) "presubmitted" emergency order set to transmit "1"

    then, later:

    1) "cancelled" emergency order set to transmit "0"
    2) "filled" regular exit order set to transmit "1"

    All this work to just be safe in case our pc crashes, which happened on my second day of trading and made me lose 80% of my capital on a day when my system actually would have made money.
     
    #11     Jan 19, 2008
  2. travis....Were you trading the EUR/USD when it plunged 200 pips?
     
    #12     Jan 19, 2008
  3. travis

    travis

    How did you know... yes, you're dead right.

    It was last wednesday or thursday (I will try to forget that day). I was long earlier on, but when the system should have exited my trade, it had technical problems (I said it crashed to summarize it) and it didn't exit. Then the market went against me and IB closed my EUR/USD position for lack of margin. Then I came home from work and found this wonderful surprise, and realized I had to come up with something.
     
    #13     Jan 19, 2008
  4. Educated guess

    Even without "technical" problems that move could have by passed any stops.
     
    #14     Jan 19, 2008
  5. Clym

    Clym

    I have been unable to send child orders that are connected to the parent (using IB/excel) and wondered if you are now doing it successfully.

    I have found that (following all instructions on IB forums etc and)setting the transmit field to a 1 for true or a 2 for false has no effect whatsover on whether or not the parent order is sent. (try sending a single order with it set to 0 and it will still be sent). It seems that whatever is in the extended attributes sheet (0 or 1) is what matters.

    after sending a child and parent order, it should show up on the API page of TWS with matching trailing keys (in which case canceling one child order would also cancel any other child order since they are connected). I have never been able to do this, if you have I would like to know how. Thanks for any help.
     
    #15     Mar 8, 2008
  6. travis

    travis

    I am NOT using parent and child order, because I also think that it is useless and I didn't see any differences whether I was using it or not. So all you have to do is:

    1) forget parent and child orders

    2) focus all attention on column 31 ("AE") of the "orders" sheet, using either "0" for false or "1" for true.

    3) everything else should be the same as with the simplest orders (no need to use any other columns or settings).


    Here's the code I used to make it all work:

    ---------------------

    To open a trade I use this code ("o" stands for "orders" sheet):

    Worksheets("o").Activate
    o.Cells(60, 31) = "0"
    o.Cells(60, 18) = ""
    o.Cells(60, 12) = "BUY"
    o.Cells(60, 13) = "1"
    o.Cells(60, 14) = "MKT"
    o.Cells(60, 1).Select
    o.placeOrder

    Worksheets("o").Activate
    o.Cells(61, 31) = "1"
    o.Cells(61, 18) = ""
    o.Cells(61, 12) = "SELL"
    o.Cells(61, 13) = "1"
    o.Cells(61, 14) = "TRAIL"
    o.Cells(61, 16) = "0.0050"
    o.Cells(61, 1).Select
    o.placeOrder

    Explanation:
    I SEND a MKT order (which has the "transmit" column set to "0"). This order only gets EXECUTED if the safety TRAIL exit is also successfully SENT and received by IB (which has the "transmit" column set to "1"). This I do in order to avoid situations in which something is bought, my pc gets disconnected, and I have no control over what happens. This way, a safety TRAIL exit order will ALWAYS be in place, no matter what.

    --------------------------

    To exit a trade I use this code:

    Worksheets("o").Activate
    o.Cells(61, 31) = "0"
    o.Cells(61, 1).Select
    o.cancelOrder

    Worksheets("o").Activate
    o.Cells(62, 31) = "1"
    o.Cells(62, 18) = ""
    o.Cells(62, 12) = "SELL"
    o.Cells(62, 13) = "1"
    o.Cells(62, 14) = "MKT"
    o.Cells(62, 1).Select
    o.placeOrder


    Explanation:
    If my computer is still up and running and connected (which usually should be the case), I want to cancel the SAFETY exit order previously sent, which was on row 61, and at the same time, I want to cancel it ONLY IF a REGULAR exit order is also sent and received by IB. Therefore I set the "transmit" column to "0" for the cancellation, so that only the reception of the new (regular) exit MKT order (with "transmit" column set to "1") will trigger the cancellation.

    ------

    So, summarizing it, we have these 4 steps/orders:

    1) regular entry order set to transmit "0" (which will read "filled" once received by IB)
    2) emergency exit order set to transmit "1" (which will read "presubmitted" once received by IB)

    then, if your pc didn't crash, later:

    3) emergency exit order set to transmit "0" (which will read "cancelled" once received by IB)
    4) regular exit order set to transmit "1" (which will read "filled" once received by IB)
     
    #16     Mar 9, 2008
  7. Clym

    Clym

    Thanks for the response. At first I thought you were on to something however after testing I find that entering a 1 or 0 on the orders sheet has NO EFFECT whatsoever as to whether or not an order transmits.

    You can set both your limit order and MKT to 0 and they will both transmit. You can send just the market order with it set to 0 and it will execute immediately.

    The only setting that seems to stop an order from executing is on the order attributes page, cell D10. (I have tried setting D10 to empty and 0 thinking it would be overridden, but no such luck)

    Below is your code that I modified for testing purposes.

    I think upon further testing, you will find that your MKT order is executing independently of your Trail, rather than only if it is sent.

    (where futures.xls = your workbookname.xls)

    Sub test()
    Dim o As Worksheet
    Application.enableevents = False
    Set o = Worksheets("orders")
    With o
    o.Activate
    o.Cells(5, 4) = "edemo"
    o.Cells(60, 30).ClearContents
    o.Cells(60, 1) = "ES"
    o.Cells(60, 2) = "FUT"
    o.Cells(60, 3) = "200806"
    o.Cells(60, 7) = "GLOBEX"
    o.Cells(60, 9) = "USD"

    o.Cells(60, 31) = "0"

    o.Cells(60, 18) = ""
    o.Cells(60, 12) = "BUY"
    o.Cells(60, 13) = "1"
    o.Cells(60, 14) = "MKT"
    o.Cells(60, 1).Select
    Application.Run "Futures.xls!Sheet2.placeOrder"

    o.Cells(61, 30).ClearContents
    o.Cells(61, 1) = "ES"
    o.Cells(61, 2) = "FUT"
    o.Cells(61, 3) = "200806"
    o.Cells(61, 7) = "GLOBEX"
    o.Cells(61, 9) = "USD"

    o.Cells(61, 31) = "1"

    o.Cells(61, 18) = ""
    o.Cells(61, 12) = "SELL"
    o.Cells(61, 13) = "1"
    o.Cells(61, 14) = "TRAIL"
    o.Cells(61, 16) = "1.50"
    o.Cells(61, 1).Select
    Application.Run "Futures.xls!Sheet2.placeOrder"

    End With
    Application.enableevents = True
    End Sub
     
    #17     Mar 9, 2008
  8. travis

    travis

    Wow... you got me thinking, too. For the past two months I thought I was on to something as well, but as you stated - it makes no difference whatsoever. So this is useless. I might as well not use that field, and this will work all the same.

    Now of course I will have to check if all the things I thought were being done are still being done, but I don't think they are. In other words, there is no guarantee that if 1) gets through, 2) gets through as well (same problem for steps 3 and 4).

    By the way, here you may find more advice on this problem:
    http://www.interactivebrokers.com/cgi-bin/discus/board-auth.pl?file=/2/52235.html&lm=1205087601

    ...

    You know what? It's fine. 1) and 2) get executed one after the other. The same for 3) and 4). Yes, I have no guarantee that if one of them fails, the other one will stop from executing - as a matter of fact will still execute and it will be a potential disaster.

    However one purpose that gets satisfied, which is the most important one for me is this - if the computer gets stuck between steps 2) and step 3), which supposedly will be distanced by at least one hour (in my usual trading), I will be fine because my trailing stop will be in place. Not as good as I thought, but still ok for me.

    Thanks for your help.
     
    #18     Mar 9, 2008
  9. teun

    teun

    I'm also struggling with this...

    Why can't you just manual attach a child to an order in TWS? I mean: you can attach an auto stop or a bracket order (and eventually manipulate the created order afterwards), but this doesn't work for a different asset (i.e. you can not attach a stock child order to a parent option order for instance).

    Any ideas?
     
    #19     Sep 28, 2009