Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to get ATR

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

    Trying to get ATR

    Hi,

    I noticed that calling ATR(21)[0] does not return the same value as the 21 period ATR indicator.

    What am I missing? How can I get the same value as the 21 period ATR indicator?

    Thanks

    #2
    Hello Aquila_Welokk,

    Welcome to the NinjaTrader forums!

    I was not able to reproduce this. Printing the indicator value called from a host script appears to be the same as the values found visually in the data box when adding the indicator directly the chart.
    (However, note that the plot value shown in the data box (and in the price margin) is rounded)

    Below is a link to a video of the test.


    With these same steps are you seeing different behavior?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Using this code in the OnBarUpdate returns this discrepancy between the ATR indicator itself and the output:

      Print("ATR: " + ATR(21)[0]);

      Comment


        #4
        Hello Aquila_Welokk,

        Is that the only code in the script?

        Are you adding a data series?

        Is this Calculate OnBarClose?

        Are you following the steps shown in the video exactly?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          That was it. I missed it. I have the strategy Calculate OnEachTick. Setting it to OnBarClose had it match.

          Is there a way to have the ATR calculate OnBarClose while the rest of the strategy is OnEachTick?

          Comment


            #6
            Hello Aquila_Welokk,

            It would not be supported for a host script to have a different Calculate setting than a called hosted script.

            You can choose when to get the value. IsFirstTickOfBar will be true after a bar closes.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Checking for IfFirstTickOfBar still returns quite a different result.

              Comment


                #8
                Hello Aquila_Welokk,

                On the first tick of the new bar, print the previous bar's information (the fully closed bar from 1 bar ago).

                if (IsFirstTickOfBar && CurrentBar > 1)
                {
                Print(string.Format("{0} | ATR(21)[1]: ", Time[1], ATR(21)[1]));
                }
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Ah, that was the issue. I had tried doing that with ATR(21)[1], but got an index out of range error.

                  Using that CurrentBar > 1 fixed it.

                  Thank you Chelsea, you've been very helpful.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Mindset, 04-21-2026, 06:46 AM
                  0 responses
                  89 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by M4ndoo, 04-20-2026, 05:21 PM
                  0 responses
                  135 views
                  0 likes
                  Last Post M4ndoo
                  by M4ndoo
                   
                  Started by M4ndoo, 04-19-2026, 05:54 PM
                  0 responses
                  68 views
                  0 likes
                  Last Post M4ndoo
                  by M4ndoo
                   
                  Started by cmoran13, 04-16-2026, 01:02 PM
                  0 responses
                  119 views
                  0 likes
                  Last Post cmoran13  
                  Started by PaulMohn, 04-10-2026, 11:11 AM
                  0 responses
                  69 views
                  0 likes
                  Last Post PaulMohn  
                  Working...
                  X