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 NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  70 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  143 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  76 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  47 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  51 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X