Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple order handling bugs in AddOn code when connecting to TDA?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Multiple order handling bugs in AddOn code when connecting to TDA?


    My trading engine connects to many tickers at once and trades them all simultaneously at high speed. So I execute a lot of trades.

    I'm seeing a handful of different kinds of errors repeating in my logs when I trade live against TDA that don't appear to be me at this point. I'm not sure if they are NT8, TDA, or some interaction between the two. I do not see them when running with the simulator. They happen sporadically and appear to be timing-related on the backend. The ticker itself does not appear to be relevant.


    Bug 1: Fills after I cancel.
    . I cancel an order.
    . I get back an OrderState.Cancelled via OnOrderUpdate.
    . I then get an OrderState.Filled on the order, with the shares filled.
    . I then get an OrderState.Filled on the order with zero shares filled and an error code/msg: OrderFailedCancel: Too late to cancel order.

    Bug 2: Spurious OrderFailedChanged errors.
    . I have a live limit order.
    . I update its price, via Account.Change().
    . I get an OrderState.Working with "OrderFailedChange: Order cannot be replaced while in status CANCELED."
    . At no point have I canceled the order when I get this response.
    . If I cancel the order after I get this error, at some later time, the order cancels normally.

    Bug 3: Spurious OrderFailedCancel errors.
    . I cancel an order.
    . Then I get back OrderState.Accepted.
    . Then I get back OrderState.Working with the error: "OrderFailedCancel: Order in state CANCELED cannot be canceled."
    . Then OrderState.Accepted.
    . Then OrderState.Working.
    . Then OrderState.Canceled, as normal.


    Bugs 2 + 3 are "harmless", other than that they create numerous misleading error dialogs I cannot turn off. I have a separate thread that looks for these dialogs and kills them as soon as they happen, but it would be better if we didn't get these errors in the first place.

    Bug 1 is more serious, b/c I make decisions after an order has been confirmed as canceled. For the order to randomly resurrect and fill later is awkward to deal with. Ideally, we would only get a cancel confirmation when the order is actually canceled with certainty.


    Questions:

    1. Are these known or expected behaviors somehow? Are there known strategies for dealing with them?
    2. Is there something that I am or am not doing on my side that might be causing these behaviors?
    3. Is there any chance they can be fixed? If not in the current NT8/TDA interface, then in the one that is being written for the post-Schwab merger? Bug 1 is a genuine problem. I can probably work around it at some point, but it will add needless complexity and I shouldn't have to.


    I'd also like to make another plea for the ability to turn off these error pop up dialogs. No user is being helped by the dialogs in my scenario. The errors are false. And the user is me, and I have elaborate logging to very accurately characterize any errors I see, dialogs or not. Needing to have a separate thread that kills 100s (soon to be 1000s) of misleading dialogs a day is a weird place to be as a developer.


    Thanks in advance for any help you can provide.

    Cheers,
    Peter
    Last edited by carnitron; 01-20-2024, 01:42 AM.

    #2
    Any update on this? I logged it last Friday afternoon and I think it may have gotten pushed onto page three over the weekend, where maybe it has been missed.

    Comment


      #3
      Hello Peter,

      Thanks for your post.

      When creating scripts for use with TD Ameritrade, there are some important limitations to keep in mind.

      For the first item, "Too late to cancel order", just because you try to cancel an order doesn't mean that it cannot be filled before the cancel is received.

      For the second item, you would need to contact TDA to make sure the actions you are doing are valid at that time in relation to that specific order ID of the order. If TDA is reporting that the order is canceled that may have been due to some other order event like OCO or some action you did.

      TD Ameritrade connections are particularly strict with NinjaScripts, especially with Native OCO enabled on the connection. TDA is also strict on the order in which they receive orders of different Order Actions. I.E. receiving a Buy before a Buy To Cover is received will result in a rejection.

      Once an OCO pair is submitted, it must be canceled and replaced and TD Ameritrade will not allow modifying the orders. Set method orders cannot be canceled with CancelOrder(). Exit methods will not use OCO and can work around this limitation. (ExitLong(), for example).

      For the third item, it sounds like you may have called cancel twice or the order was already in a canceled state before you called canceled.​

      For all of these items, you would need to reach out to TDA with the order IDs to see exactly what sequence of events TDA had observed and what actions you submitted to know why the behavior is happening. In general, TDA is very strict and there are many limitations surrounding TDA. The only way to avoid those would be to submit orders exactly as TDA suggests to avoid rejections or to use a different broker

      When using Account.Change(), the order may become rejected depending on the change you are trying to do and if it is OCO related as noted above.

      Here is a forum thread detailing more information on this topic: https://forum.ninjatrader.com/forum/...65#post1115965
      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        Hi Brandon, thank you for your response.

        1. I am well aware an order can be filled while a cancel is in flight. If you reread my statement of Bug 1, you will notice that I am getting confirmation that the cancel succeeded before the fill. That seems wrong to me, b/c it means the API lies sometimes, which is hard to deal with if you can't know when it is lying.

        2. I am not using OCO orders at all at this point. So using OCO orders can't be the cause of bug 2.

        3. I have not called cancel twice, nor is the order in a canceled state at the time of the cancel. I have been watching these errors over multiple logs and multiple tickers. My logging has been battle tested for 1.5+ years now and if I was canceling twice, I would see it in the logs.

        To be clear, I am not getting rejections. In bug 1, I am getting a false cancel confirmation. In bugs 2 + 3, I am getting spurious errors. Bug 1 is rare. Bugs 2 + 3 are very common.

        I am also getting "broken" orders that I cannot cancel and do not go away unless I disconnect and then reconnect to TDA.

        I suspect there are bugs in NT8 here. NT8 has to maintain its own order state in order to display what is happening in the charts and to show various stats. I.e. it's not just passing through traffic from TDA to me opaquely and being unaware of what is happening. I am pursuing this with TDA also, but I am hoping we can investigate on the NT8 side in parallel.

        Cheers,
        Peter
        Last edited by carnitron; 01-24-2024, 06:53 PM.

        Comment


          #5
          Hello Peter,

          Thanks for your notes.

          For changing orders when connected to TDA, you would need to cancel and resubmit any orders you need to change instead of calling Account.Change()

          Reversing orders may not work since NinjaTrader is multi-threaded and it is possible for a Buy for a reversal to be submitted before the Buy To Cover order when reversing from a short position.

          There is also an ongoing issue where TDA has a delay in sending the Execution of an order after it is filled.

          The above limitations are all specific to TD Ameritrade.​

          Ultimately, you would need to reach out to TDA with the order IDs to see exactly what sequence of events TDA had observed and what actions you submitted to know why the behavior is happening. In general, TDA is very strict and there are many limitations surrounding TDA. The only way to avoid those would be to submit orders exactly as TDA suggests to avoid rejections or to use a different broker
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Hi Brandon, thank you for your reply.

            1. Why do I need to cancel and resubmit orders instead of calling Account.Change()? Change appears to work every time, in terms of changing the order (barring sometimes being too late, which is normal). It's just that sometimes it gives these spurious OrderFailedChange errors. Canceling and resubmitting is doable, ofc, but it is solidly more complicated and awkward, so I'd want to understand specifically why are you saying this.

            2. I'm not even aware of a reverse call in the AddOn API. What are you referring to here? Regardless, it is not something I am doing.

            3. To repeat, I'm not having issues with order rejections, so I don't think continuing to talk about avoiding them is relevant.

            I have an email out to TDA's API support. They are slow to respond so I'm not sure when I'll get an answer back. But I will update this thread when I have more info from them.

            Beyond that, I'd like to move past enumerating lists of things that don't work with TDA that, while interesting and helpful for providing broader context, are nevertheless not actually relevant to the issues at hand.

            What would be better is some kind of path forward. Is there a way to communicate with NT8 development? For the sake of argument, say I have identified real bugs in NT8. What would NT8 normally do in this case?
            Last edited by carnitron; 01-25-2024, 12:04 PM.

            Comment


              #7
              Hello Peter,

              Thanks for your notes.

              Account.Change() could be used in an AddOn script to change orders if you find this is working without issues.

              Reversing a position would be going from a long position to a short position, for example. The long position would have to be closed and then an order would need to be submitted to enter a short position.

              TDA is very strict and particular about how orders are submitted. Multiple orders cannot be submitted at the same time as this could cause errors.

              The orderAction would have to be exactly as TDA requires to avoid receiving errors when running the script to place orders. Based on the information you provided the errors would occur if you tried to cancel an order that has already been canceled.

              I understand you have contacted TDA regarding the errors. TDA should be able to provide you with exactly what sequence of events TDA had observed and what actions you submitted to know why the behavior/errors are happening.

              If you would like us to review the orders in question, please write in to support[at]ninjatrader[dot]com and follow the steps below to attach your Log and Trace files to your email. Also, send us the order ID of the order(s) in question when the error(s) occurred and the time the error occurred. In the subject of your email include ATTN: Brandon H and in the body of the email include a link to this forum post.

              Follow the steps below to manually attach your log and trace files to your email.
              • Open your NinjaTrader folder under, "Documents" (sometimes called, "My Documents")
              • Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
              • Send the 2 compressed folders as attachments to this email.
              • Once complete, you can delete these compressed folders.
              ​We look forward to assisting further.

              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                Ok, I just sent you email that includes log files for Bugs 2 + 3. I will have to wait to catch Bug 1 again, b/c it is rare. Thank you for looking at these and I hope they are illuminating.

                Thanks for the clarification on what you meant by reversing positions. That is actually useful to know for the work I need to do this upcoming week.

                Have a great weekend.

                Cheers,
                Peter
                Last edited by carnitron; 01-27-2024, 12:13 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by ageeholdings, Today, 07:43 AM
                0 responses
                6 views
                0 likes
                Last Post ageeholdings  
                Started by pibrew, Today, 06:37 AM
                0 responses
                4 views
                0 likes
                Last Post pibrew
                by pibrew
                 
                Started by rbeckmann05, Yesterday, 06:48 PM
                1 response
                14 views
                0 likes
                Last Post bltdavid  
                Started by llanqui, Today, 03:53 AM
                0 responses
                6 views
                0 likes
                Last Post llanqui
                by llanqui
                 
                Started by burtoninlondon, Today, 12:38 AM
                0 responses
                12 views
                0 likes
                Last Post burtoninlondon  
                Working...
                X