Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem creating $ATR Indicator

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

    Problem creating $ATR Indicator

    Hi,

    I am trying to create a $ATR Indicator, for which I'm using the formula:

    $ATR = ATR * PointValue



    However I'm running into some problems that I haven't been able to figure out. I thought I'd just use the existing ATR indicator and multiply it's Value[0] with the PointValue, like this:


    Code:
    public class DollarATR : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                    = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionATR;
                    Name                        = "$ATR";
                    IsSuspendedWhileInactive    = true;
                    Period                        = 14;
    
                    AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameATR);
                }
            }
    
            protected override void OnBarUpdate()
            {
                double PointValue    = Instrument.MasterInstrument.PointValue;
                double high0    = High[0];
                double low0        = Low[0];
            
                
                if (CurrentBar == 0)
                    Value[0] = (high0 - low0) * PointValue;
                else
                {
                    double close1        = Close[1];
                    double trueRange    = Math.Max(Math.Abs(low0 - close1), Math.Max(high0 - low0, Math.Abs(high0 - close1)));
                    Value[0]            = (((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, Period)) * PointValue;
                    Print("Point Value: " + PointValue + " $ATR: " + Value[0]);
                }
            }​
    However, the value I get is something that is growing exponentially:

    Click image for larger version

Name:	image.png
Views:	114
Size:	44.0 KB
ID:	1288868
    From the Output I see that it seems to grow at a rate of something like 33 ^ x:

    The first lines:

    Click image for larger version

Name:	image.png
Views:	82
Size:	62.0 KB
ID:	1288869

    Does anyone know why this error occurs and how I might solve it? Is there a better way to code this indicator?

    Sincerely,
    Axel​

    #2
    Hello axelbaws,

    That is because you are setting the result to the plot which the equation uses the previous bar value in its math.

    The easiest way to do this would be to make a new indicator and reference the existing ATR.

    In a new indicator add a plot and then you could just use the ATR return value like the following:

    Code:
    double atrValue= ATR(20)[0];
    Value[0] = atrValue * Instrument.MasterInstrument.PointValue;

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello axelbaws,

      That is because you are setting the result to the plot which the equation uses the previous bar value in its math.

      The easiest way to do this would be to make a new indicator and reference the existing ATR.

      In a new indicator add a plot and then you could just use the ATR return value like the following:

      Code:
      double atrValue= ATR(20)[0];
      Value[0] = atrValue * Instrument.MasterInstrument.PointValue;
      Hello Jesse,


      That is a smart and nice solution, which works perfectly!

      Thank you for solving my problem.


      Best regards,
      Axel

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      581 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      338 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      103 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      554 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      552 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X