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

A very basic question: Can't get the right result from a simple operation...

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

    A very basic question: Can't get the right result from a simple operation...

    Sorry, this seems so elemental that feels embarrissing to ask (the problem of ignorance...), hence the apologies in advanced...

    I created a new indicator using the original NT's code for the ATR indicator, my aim is simply to plot ATR/Close for each bar on a chart.

    Using the original NT's code with the required adjustments to the properties region, etc. works nicely. if I set my variable to ATR it plots ATR exactly as plotted using the original NT's indicator. If I set my variable to Close[0], it plots the close correctly too. BUT, when I set my variable to ATR/Close[0] it plots wrong values.

    If I print ATR/Close[0] to the output window for debugging while my variable is set to (say...) ATR, then it prints the right values on the window, but if I set my variable to ATR/Close[0] then the output window gives the same wrong valus as the plot.

    What am I doing wrong?

    Attached is the code for my indicator. Below the key part of the OnBarUpdate:

    double trueRange = High[0] - Low[0];
    trueRange = Math.Max(Math.Abs(Low[0]... as in NT's original code
    double ATRtemp;
    ATRtemp = (((Math.Min(CurrentBar + 1, ... as in NT's original code
    double todayClose = Close[0];
    double ATRpctg = ATRtemp/todayClose;
    Print(ATRpctg);
    ATRPercentage.Set(ATRpctg);
    <--- if I set this to a different value (say Close[0]) the output window prints ATRpctg correctly

    Thank you for your help!
    Attached Files

    #2
    Welcome to the forums, you just see a full double value displayed (precision wise), the chart would round automatically, so for the output window value is the 'raw' one...try for example Math.Round on the value to make it easier readable in the ouput.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Dear Bertrand, thank you for your prompt reply.

      The incorect output doesn't look like rounding error. I tried your suggestion anyway and it didn't work.

      For example, for an ATR=0.707 and a Close=44.35 the expected value should be around 0.0159 (0.707/44.35), in the ouput window I get 0,0159506 (which is ok!)... when I set my variable value to my double ATRtemp, but if I set my variable to the double ATRpctg (=ATRtemp/todayClose) the output window prints 0,00137.... which is a long way off.

      I see the problem occurring when setting my indicator's value. Could this be related to variables definition or data series?

      Comment


        #4
        Hi Miggers, could't reproduce the incorrect value, you would see the full precision in the output window expressed in exponential manner in your case of a very small number. From what I tested, I believe your calcs hitting home.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hi Betrand,

          Attached you will find an example with the QQQQ chart (scroll to the left to show the last bar as for the 26/Jan) using the code I posted earlier.

          You will find in the attachment that for the 26/Jan the Close = 44.35, ATR = 0.707, and the MVATRPctg = 0.00137

          However but for those values 0.707/44.35 = 0.0159, the result in my chart is more than 10 times smaller.

          Are you getting the same results as in the attached picture when you run my code?
          Attached Files

          Comment


            #6
            Miggers, tried on AAPL daily and printed just the calculated ATRtemp value from you to compare to the ATR indicator, this seems off by a large degree, thus resulting in the unexpected results you get - please try with dividing the Close into the standard ATR() method we offer per default in NinjaScript.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Betrand, I tried with NT's default code, but because it is read only the canges on the code after copiling are not reflected on the chart following reload.

              So, I created yet another fresh new ATR indicator, called the input value "Period" as per the default NT's code, copied the code from the the NT's default code and pasted it in the new editor (only different bit is the name of the oscillator), then compile it, included it in a chart and plotted it below the default ATR for comparison, in this case the results are exactly as NT's default ATR. Then I set the value to what it is divided by Close[0] and got the same unexpected results again...

              This is actually getting quite fustrating, if such a simple operation gets this difficult, I can't imagine how much harder would be to create a whole new indicator full of conditionals an maths...

              What would you do if you wanted to create a new indicator to plot ATR/Close? Would you use the same approach as I did or something different?

              Comment


                #8
                Ok, I solved it!

                I created a new dataseries variable to calculate ATR, instead of using a double variable.

                Thank you for your support.

                Comment


                  #9
                  Miggers, glad to hear it's resolved for you.
                  BertrandNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by wzgy0920, 04-20-2024, 06:09 PM
                  2 responses
                  26 views
                  0 likes
                  Last Post wzgy0920  
                  Started by wzgy0920, 02-22-2024, 01:11 AM
                  5 responses
                  32 views
                  0 likes
                  Last Post wzgy0920  
                  Started by wzgy0920, Yesterday, 09:53 PM
                  2 responses
                  49 views
                  0 likes
                  Last Post wzgy0920  
                  Started by Kensonprib, 04-28-2021, 10:11 AM
                  5 responses
                  191 views
                  0 likes
                  Last Post Hasadafa  
                  Started by GussJ, 03-04-2020, 03:11 PM
                  11 responses
                  3,230 views
                  0 likes
                  Last Post xiinteractive  
                  Working...
                  X