Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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:	61
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:	40
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;
    JesseNinjaTrader Customer Service

    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 burtoninlondon, Today, 12:38 AM
      0 responses
      10 views
      0 likes
      Last Post burtoninlondon  
      Started by AaronKoRn, Yesterday, 09:49 PM
      0 responses
      14 views
      0 likes
      Last Post AaronKoRn  
      Started by carnitron, Yesterday, 08:42 PM
      0 responses
      11 views
      0 likes
      Last Post carnitron  
      Started by strategist007, Yesterday, 07:51 PM
      0 responses
      14 views
      0 likes
      Last Post strategist007  
      Started by StockTrader88, 03-06-2021, 08:58 AM
      44 responses
      3,983 views
      3 likes
      Last Post jhudas88  
      Working...
      X