Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get #contracts in ATM template

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

    How to get #contracts in ATM template

    I'm having trouble coding a strategy that uses ATM template to manage the trade. What I want to do is, once the order is filled, have the strategy immediately reset the stoploss orders to a certain value. The problem is that I don't know the 'name' or quantity of the stop orders to change.

    For example, if I open an order with an ATM template with 1 contract, I know that the stoploss order name is "STOP1". However, if I have a template with more than 1 contract, I don't know how to tell which contract or stoploss order name to change the stop target. Here is the code I am running to attempt to reset the stoploss:

    Code:
                    // Adjust initial stoploss price if this is the first OnBarUpdate() call since the order was filled
                    if (ATMStrategyID != string.Empty && GetAtmStrategyMarketPosition(ATMStrategyID) != MarketPosition.Flat)
                    {
                        for (int i = 1 ; i <= GetAtmStrategyPositionQuantity(ATMStrategyID) ; i++)
                        {
                             AtmStrategyChangeStopTarget(0, StopPrice, "STOP" + i.ToString(), ATMStrategyID);
                        }
                    }
    I have a ATM template with 3 contracts, but when I execute this code on the OnBarUpdate() when the order is filled, I get this error:

    **NT** AtmStrategyChangeStopTarget() method error: OrderName 'STOP2' is invalid
    **NT** AtmStrategyChangeStopTarget() method error: OrderName 'STOP3' is invalid

    How can I tell how many contracts an ATM template has and how to reset the stops when the order fills?

    Thanks!
    Bryan

    #2
    Hi Bryan,

    There can only be 3 stop levels per ATM strategy. They're labeled STOP1, STOP2, and STOP3. All 3 are only available if you define an ATM strategy with 3 stop loss levels. You can't split up the orders within a level by referencing quantity.

    Attached screenshot showing how the 3 levels are defined within an atm strategy.
    Attached Files
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      Hi Bryan,

      There can only be 3 stop levels per ATM strategy. They're labeled STOP1, STOP2, and STOP3. All 3 are only available if you define an ATM strategy with 3 stop loss levels. You can't split up the orders within a level by referencing quantity.

      Attached screenshot showing how the 3 levels are defined within an atm strategy.
      Yes, I understand multiple stop levels. OK, so I took out the look in that code, assuming 3 contracts with only one stoploss level, I should be able to change the stop price, right? But when I try it, I get this error now:

      **NT** AtmStrategyChangeStopTarget() method error: OrderName 'STOP1' does not exist

      But I think I see what's happening in the Log tab. It's getting partial fills. It seems that the stoploss order does not get placed until all 3 contracts are filled. So when I try to change the STOP1 order on the next tick after the first contract fills, it still doesn't have a stoploss in place yet. Hmmm....How do I know when all possible contracts have been filled then? Or is there a method I can use to check whether the ATM has placed its stop order yet? Would it be in using:

      string[,] orders = GetAtmStrategyStopTargetOrderStatus("STOP1", "idValue");

      Comment


        #4
        Hello,

        NinjaTrader will place stop orders based on partial fills. But it does take milliseconds for NinjaTrader to get confirmation of the partial fill then submit the stop order for the partially filled amount.

        My guess is you are changing it before this time or before the partial fill occurs.

        Therefor what I would do is check the status of the order before changing it to prevent this error and GetAtmStrategyStopTargetOrderStatus("STOP1", "idValue") will do the trick on this.

        I look forward to assisting you further.
        BrettNinjaTrader Product Management

        Comment


          #5
          Originally posted by NinjaTrader_Brett View Post
          Hello,

          NinjaTrader will place stop orders based on partial fills. But it does take milliseconds for NinjaTrader to get confirmation of the partial fill then submit the stop order for the partially filled amount.

          My guess is you are changing it before this time or before the partial fill occurs.

          Therefor what I would do is check the status of the order before changing it to prevent this error and GetAtmStrategyStopTargetOrderStatus("STOP1", "idValue") will do the trick on this.

          I look forward to assisting you further.
          Brett, so the logic should be something like this?

          Code:
                              string[,] orders = GetAtmStrategyStopTargetOrderStatus("STOP1", ATMStrategyID);
                              if (orders.Length > 0 && orders[0, 2].ToString() == "Accepted") 
          { set stoploss }
          1) What is the first subscript in the orders array?
          2) Is there a way to know how many stop levels the ATM strategy has so I know to look for STOP2 and STOP3 if needed?
          3) Is "Accepted" the proper status to look for to know that all contracts in the order have filled?

          Thanks!

          Comment


            #6
            A string[,] - multi-dimensional array holding three dimensions that represent average fill price, filled amount and order state. The length (number of elements) represents the number of orders that represent the specified name.

            Yes, your snippet looks good for checking order state on ATM stop loss orders. Order states could be considered either working or accepted state, so you may consider modifying:

            if (orders.Length > 0 && (orders[0, 2].ToString() == "Accepted" || orders[0, 2].ToString() == "Working)")

            You could also consider checking the fill state of the entry order directly with:


            The number of stop loss levels is dependent on how the strategy is defined. Unfortunately I'm not aware of a way to expose this programatically and there's not a supported technique for it.
            Ryan M.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X