Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StartAtmStrategy failing to attach ATM to order

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

    StartAtmStrategy failing to attach ATM to order

    NT8 Support & Dev Team,

    We're running into a particularly maddening issue where NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy fails to attach the ATM strategy to the order in some rare circumstances.

    At CrossTrade, we're running a custom NT8 add-on that's serving several thousand users and processing tens of thousands of orders per day. I mention this to give you an idea of the type of size and scale we're working with.

    A user can choose to attach an ATM strategy to an order by name, and we process it in an AddOn class as follows:

    Code:
                    Order order = myAccount.CreateOrder(
                        instrument,
                        orderAction,
                        ordType,
                        OrderEntry.Automated,
                        tif,
                        quantity,
                        limitPrice,
                        stopPrice,
                        ocoId,
                        strategy != null ? "Entry" : null,
                        Core.Globals.MaxDate,
                        null
                    );
    
                    if (order == null)
                    {
                        result["error"] = "Failed to create order";
                        return result;
                    }
    
                    if (strategy != null)
                    {
                        NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy(strategy, order);
                    }
                    else
                    {
                        myAccount.Submit(new Order[] { order });
                    }
    In almost all cases, this code executes correctly, the ATM strategy is attached, and the stop/limit orders appear on the chart, if open. But in rare circumstances, maybe < 0.05% of the order volume we do, the StartAtmStrategy code executes filling the primary order, but no owner strategy is attached. But we have enough reports from users of this happening for it to be a major concern.

    We cannot reliably reproduce this behavior.

    We've verified this is the case because a) if the ATM strategy name is not found, this code will produce an exception, and b) pulling the order history shows a filled execution with name == "Entry", but the owner strategy is null.

    So we are certain the ATM strategy exists, the name was entered properly, and we executed NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy(strategy, order) without error. But users report NT8 software enters some kind of state where ATMs fail to attach for an arbitrary amount of time, then suddenly they work again.

    We have had users perform test cases with extremely simple ATM strategies, but to no avail. We have narrowed down most general cases where an ATM would fail to attach and documented them here, but none of which solve the problem completely: https://docs.crosstrade.io/webhooks/...roubleshooting

    The only sure-fire way to fix the problem is to restart NT8.

    NT8 Support Team: I implore you to look into this and not dismiss the issue. There has to be some kind of deep timing issue, threading issue, or race condition in the NT8 internal code.
    • Is there anything else we should be aware of when executing StartAtmStrategy from add-on code?
    • Are there any other reasons we're not aware of that would cause an ATM attachment failure?
    • Is it necessary to marshal those function calls to the primary UI thread?
    • What are we missing here?

    Thank you for your time and attention.

    CrossTrade Team
    Last edited by crosstrade; 05-08-2025, 02:31 PM.
    crosstrade
    NinjaTrader Ecosystem Vendor - crosstrade

    #2
    Hello crosstrade,

    This may be due to how the overall tool is coded, unfortunately without specific details it would be impossible to say what may be happening. If this requires heavy volume to see it happen you will likely need to reduce the situation to some sort of reproducible scenario to report it so we have something to provide to development, we don't have the ability to test snarious without specific details on how to replicate a problem. I would suggest to make a test script that you can use on users PC's where they are having the problem to further trace the issue in a more isolated way.

    There are no known issues surrounding starting ATM's from NinjaScript that I could locate so if you are seeing one we would need to know the specifics around the use case along with a sample so we can observe that happening. We would likely need details about the code used, a sample, the steps used and the broker technology used at the time of the problem. If you can additionally include log and trace files through the [email protected] email that may help so we can look at the specific order in relation to the sample you provided.

    This could also relate to how you are loading the atm strategy as an instance instead of a string, I cant tell from the code if that is just a string or not. I would suggest trying to use the overload that just uses a string for the template name to let the platform load the template internally.



    Comment


      #3
      The strategy variable in this case is indeed a string, and the order is constructed as shown. We will continue our attempts to create reproducible behavior. The add-on code is extensive, but I can tell you this place order code is running inside a task on a background thread, if that matters.

      Do you have any code examples or a list of best practices for executing StartAtmStrategy from within an add-on? Most of the examples I've seen revolve around indicators.
      crosstrade
      NinjaTrader Ecosystem Vendor - crosstrade

      Comment


        #4
        1. Where exactly do you get the Atm strategy template name from?

        2. Is an Atm entry order or position already active at the time the anomaly occurs?

        3. When your Addon is running and the anomoly occurs, are any DOMs or charts open?

        4. Are any of these open DOMs/charts the same instrument as the entry order?

        5. On any of these open charts, is the Chart Trader enabled anywhere, on any chart?

        -=o=-

        Just curious as to the details of the running NinjaTrader environment that is trying
        to place the order via the AddOn. No idea if this significant, but studying the various
        similarities of your customer's operating 'execution environment' is the one way to find
        some common detail. Maybe such a common detail could prove significant.

        -=o=-

        Another thing to check, for whatever Atm strategy name you are using, the corresponding
        XML file should exist in the 'NinjaTrader 8/templates/AtmStrategy' folder. Perhaps a quick
        check to confirm that XML file's existence could rule in/out the scenario where you have
        a name stored in the variable, such that strategy != null is true, but for some reason
        the corresponding XML file is not there.

        Comment


          #5
          1. Where exactly do you get the Atm strategy template name from?
          CrossTrade accepts remote orders (mostly from TradingView) via webhook/HTTP request. The template names are user inputs into the payload message that we parse, send to the listening XT add-on, and execute the user's instructions.

          The try/catch surrounding the place order code above will return an error immediately if the XML template does not exist under the same name. So we know for certain that the name matches an existing template that NT8 recognizes.

          Again, I'd like to stress that this is not a "how do I get this working" type of question. We're processing probably 100k+ orders with ATM strategies attached each day without issue in this way. But for that 0.01% of the time, it will execute with no order attached, and that's destroying trust for people who are relying on both CrossTrade and NT8.

          2. Is an Atm entry order or position already active at the time the anomaly occurs?
          It doesn't seem to matter, anecdotally. We have seen ATMs fail under both conditions, flat and active.

          3. When your Addon is running and the anomaly occurs, are any DOMs or charts open?
          4. Are any of these open DOMs/charts the same instrument as the entry order?
          Answering both 3 & 4, the failure does seem to happen exclusively when at least one chart is open. Most of the time it's open to the account/instrument present in the order because they're watching execution.

          Our team usually runs NT8 in a sort of "headless" mode where we don't even use the charts, and just the add-on and Control Panel orders window is open since CrossTrade is handling 100% of the automation. We have never seen a failure to attach running it this way, which is why I asked the question about marshaling StartAtmStrategy to the application UI thread because maybe it's related to drawing on the chart, but who knows?

          5. On any of these open charts, is the Chart Trader enabled anywhere, on any chart?
          Usually, yes.
          crosstrade
          NinjaTrader Ecosystem Vendor - crosstrade

          Comment


            #6
            Hello crosstrade,

            Yes in this case it sounds like the tool is using more advanced C# outside of normal NinjaScript so that may play a role. You likely will have to add more debugging into the script so you know the exact sequence of events that happened when you observe the problem so you can recreate it. The only sample we have of using an ATM from an addon is the following item:

            Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


            If you are using custom threading or web requests you may be calling the code in the wrong context, we can only suggest using the standard NinjaScript overrides or events to call NinjaScript code. If you are doing anything with async/await for the web request that may be part of the problem.

            You will likely need to make a test script that can reproduce the problem as to exclude your more complex code, if you find something is not working correctly we could look at the smaller sample to see if what you are doing can be supported. We would not be able to assist with any web related code so whatever is being done would need to be reduced into a small sample that can be executed on our end for testing. That could be as simple as a button click to start a trade so long as it recreates the issue.

            Comment


              #7
              With respect, may I suggest ....

              Add code to your AddOn to provide OIF as an alternate
              method of order creation/submission.

              I have had great success submitting orders using the
              OIF interface, this alternate method might be useful
              to the few customers experiencing problems with
              your current code.

              In fact, if your AddON is doing the WebHook code
              itself, may I suggest you consider breaking this
              piece out into a non-NT based program, and then
              that separate program receives the socket msgs
              from TradingView, and it submits the orders via the
              aforementioned OIF interface.

              Just an idea, obviously you're trying to find something
              that works 100% of the time for every customer.

              My point is, the OIF interface is a viable alternative,
              and it provides you with some extra design choices
              as to who/where the order submission occurs.

              Comment


                #8
                Good suggestion, bltdavid. CrossTrade v1.0 was actually a Windows app that worked exclusively using the OIF interface before we migrated to an NT8 add-on. We migrated to an NT8 add-on to enable bidirectional communication between NT8 and our servers, which has proven to be exponentially more powerful.

                But if OIF interface is 100% reliable for executing the ATM strategy, it's worth an investigation to build it into our flow - thanks!

                NinjaTrader_Jesse we have added some more granular logging to our server-side that will go live at start of session on Sunday. We should have full visibility into actual failure rates, number of users affected, etc. and hopefully begin to see a pattern.

                If we can create a small piece of code that reliably reproduces the problem, we will.
                crosstrade
                NinjaTrader Ecosystem Vendor - crosstrade

                Comment


                  #9
                  Originally posted by crosstrade View Post
                  Good suggestion, bltdavid. CrossTrade v1.0 was actually a Windows app that worked exclusively using the OIF interface before we migrated to an NT8 add-on. We migrated to an NT8 add-on to enable bidirectional communication between NT8 and our servers, which has proven to be exponentially more powerful.
                  I'd say keep the AddOn as is, but add an option to
                  use the OIF interface for order submission instead
                  of the Account.CreateOrder method.

                  Obviously, for the long term, it's probably either one
                  submission method or the other, but for now, a user
                  option that controls that choice seems like a good
                  idea.

                  Yeah, adding that option w/OIF code seems like the
                  best approach, and I'd say the least amount of work,
                  too.

                  You might get the best of both worlds.



                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by abelsheila, 05-14-2025, 07:38 PM
                  2 responses
                  30 views
                  0 likes
                  Last Post hglover945  
                  Started by nailz420, 05-14-2025, 09:14 AM
                  1 response
                  57 views
                  0 likes
                  Last Post NinjaTrader_ChristopherJ  
                  Started by NinjaTrader_Brett, 05-12-2025, 03:19 PM
                  0 responses
                  327 views
                  1 like
                  Last Post NinjaTrader_Brett  
                  Started by domjabs, 05-12-2025, 01:55 PM
                  2 responses
                  62 views
                  0 likes
                  Last Post domjabs
                  by domjabs
                   
                  Started by Morning Cup Of Trades, 05-12-2025, 11:50 AM
                  1 response
                  81 views
                  0 likes
                  Last Post NinjaTrader_ChristopherJ  
                  Working...
                  X