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

Emas value. Decimal excess

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

    Emas value. Decimal excess

    Hi. This message is written with google translator. Excuse me.
    I have an indicator with three emas
    When ema 1> ema 2 and ema 3 a condition occurs.
    But I have noticed this problem:
    ema 1 its value is for example 175.75
    ema 2 = 175.75
    ema3 = 175.74
    The condition is met
    printing the value of the emas I get that
    ema 1 = 175.750000001
    ema 2 = 175.750000000

    There is a way that the value of the emas is limited to the first five figures, for example 175.75 (xxx.xx)
    Thank you.

    #2
    Hello julifro,

    Thank you for your note.

    You can certainly restrict the number of decimals when printing the values. The following link goes over this in detail:

    https://ninjatrader.com/support/helpGuides/nt7/formatting_numbers.htm

    Please let us know if we may be of further assistance to you.
    Last edited by NinjaTrader_Kate; 07-17-2020, 01:55 PM. Reason: put link for NT8 instead of NT7
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Excuse me, I have not explained myself well
      I don't need to print the EMA value
      I need the value of all emas to be two decimal places
      With that value of two decimals I compare if ema1 is greater than the other emas. I don't want to compare the value of the emas with as many decimals as it appears by default in ninjatrader
      For NT 7 please. Thank you.

      Comment


        #4
        Hi julifro,
        Does this help?
        RoundedValue = Math.Round(value, 2);
        NT-Roland

        Comment


          #5
          Originally posted by NT-Roland View Post
          Hi julifro,
          Does this help?
          RoundedValue = Math.Round(value, 2);
          NT-Roland
          This might be better,

          Code:
          RoundedValue = Instrument.MasterInstrument.[URL="https://ninjatrader.com/support/helpGuides/nt7/?masterinstrument_ro.htm"]Round2TickSize[/URL](value);

          Comment


            #6
            Hi bltdavid,
            I use this quite a bit, but I'm not convinced. Is that really what julifro asked for?
            @julifro: Have a look at the attached to see the difference.
            If you just want to round to two decimals, use my suggestion. If you want to round to ticksize, go ahead with the snippet from bltdavid.
            NT-Roland
            Attached Files

            Comment


              #7
              Originally posted by NT-Roland View Post
              Hi bltdavid,
              I use this quite a bit, but I'm not convinced. Is that really what julifro asked for?
              @julifro: Have a look at the attached to see the difference.
              If you just want to round to two decimals, use my suggestion. If you want to round to ticksize, go ahead with the snippet from bltdavid.
              NT-Roland
              You are correct.

              Disregard my Round2TickSize suggestion -- that's more like the sledgehammer
              when doing comparisons, I can easily agree a finer touch is probably better.

              But ...
              IMHO, the OP should always compare the EMA values using the full extent of
              the calculated value -- meaning all (or most) decimal values in the EMA should
              be considered valid during comparisons -- or at the very least, certainly more
              than 2 decimals should be considered valid.

              If the OP still wants to use Round, then I'd recommend,

              Code:
              RoundedValue = Math.Round(value, 2, MidPointRounding.AwayFromZero);
              But still, rounding is simply not recommended.

              I believe the OP is trying to achieve the effect of "I only want to compare EMA
              values to maximum of 2 decimal values" and he thinks rounding is the way to achieve
              that.

              That would be incorrect.

              He should use the undocumented Compare method.
              (See @Stochastics.cs for a usage example.)

              Let's study in detail what this function does. First off, the undocumented Compare
              method is probably doing something like this,

              Code:
              public static int Compare(this double A, double B, double Tolerance)
              {
                  if (A.IsAlmostEqual(B, Tolerance))
                      return 0;
                  else if (A > B)
                      return 1;
                  else /* if (A < B) */
                      return -1;
              }
              Where IsAlmostEqual is the real secret sauce, and it does this,

              Code:
              public static bool IsAlmostEqual(this double A, double B, double Tolerance)
              {
                  return Math.Abs(A - B) < Tolerance;
              }
              The critical understanding here is to look at what Tolerance means.

              The idea is that if A and B are 'close enough' to each other, then they're
              considered equal -- which assumes a certain 'tolerance' value (also called
              the Epsilon value) for how many decimal digits are considered valid.

              How does that work?
              Well, the Tolerance value is a very small number, let's use 0.0001, which
              is 4 decimal places, so if A minus B is less than 0.0001 ... well, that means
              A and B are so close in value that we can consider them 'close enough' to
              be equal, at least within those 4 decimal places -- meaning digits beyond
              those 4 decimal places we don't care about. Make sense?

              Now let's go back.

              The code in @Stochastics.cs starts at line 59. Notice how den[0] is being
              compared to zero with a specified tolerance,

              Code:
              59            if (den[0].Compare(0, 0.000000000001) == 0)
              60                fastK.Set(CurrentBar == 0 ? 50 : fastK[1]);
              61            else
              62                fastK.Set(Math.Min(100, Math.Max(0, 100 * nom[0] / den[0])));
              Note how the tolerance value (aka Epsilon value) is hardcoded for 12 decimal places,

              Code:
                123456789012
              0.000000000001
              I'd recommend you setup your own constants for various tolerance values, for ex,

              Code:
              public const double Epsilon4     = 0.0001;       // tolerance is 4 decimal places
              public const double Epsilon8     = 1E-8;         // tolerance is 8 decimal places
              public const double Epsilon10    = 1E-10;        // tolerance is 10 decimal places (also common)
              public const double Epsilon12    = 1E-12;        // tolerance is 12 decimal places
              public const double Epsilon      = Epsilon12;    // standard default tolerance
              And then use NinjaTrader's own Compare function to compare to the desired
              number of decimal places,

              Code:
              // using 4 decimal places
              if (EMA(50)[0].Compare(Close[0], Epsilon4) > 0)
                  Print("EMA(50) above Close");
              Or capture the return value and use it repeatedly,

              Code:
              // using 8 decimal places
              int rc = EMA(50)[0].Compare(Close[0], Epsilon8);
              if (rc > 0)
                  Print("EMA(50) above Close");
              else if (rc < 0)
                  Print("EMA(50) below Close");
              else if (rc == 0)
                  Print("EMA(50) equals Close");
              In summary, in C# the 'Epsilon' concept is the only correct way to compare doubles
              (esp doubles that hold calculated values) -- this technique guarantees a specified
              tolerance for how many decimal digits are valid.

              Good reading here, here, and here.

              Also, when comparing EMAs against price values, check out this method. On that
              page, you should read 'price value' as anything on the Y-axis of the main chart
              panel
              , which EMAs certainly are (but something like the CCI is not).

              Btw,
              NinjaTrader 8 has exposed this undocumented Compare function, but the
              tolerance value is fixed at 19 decimals ... oh, and the name was changed.
              Last edited by bltdavid; 07-19-2020, 07:19 PM. Reason: Add link to information on correct way to compare doubles.

              Comment


                #8
                Hello julifro,

                Thank you for your reply.

                Also, thank you NT-Roland and bltdavid for your input.

                bltdavid has covered this fairly thouroughly, however, there's more information on comparing floating point numbers in our help guide here:



                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Despite what the above help guide says, do NOT use double.Epsilon for your Epsilon value.

                  Using double.Epsilon is simply flat wrong.

                  The C# designers regretfully chose that name to represent a different concept,
                  and that concept is simply not compatible with this comparison technique.

                  Why?
                  It's too damn small. Nothing is smaller than double.Epsilon, so how in the
                  heck is Math.Abs(A - B) < double.Epsilon ever supposed to work?

                  Think about it.

                  Comment


                    #10
                    Thank you. Overwhelmed by your great help. I haven't had time to study his proposal yet. The Ninjatrader community is very valuable. Thank you very much.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by futtrader, 04-21-2024, 01:50 AM
                    5 responses
                    56 views
                    0 likes
                    Last Post NinjaTrader_Eduardo  
                    Started by PeakTry, Today, 10:49 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post PeakTry
                    by PeakTry
                     
                    Started by llanqui, Today, 10:32 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post llanqui
                    by llanqui
                     
                    Started by StockTrader88, 03-06-2021, 08:58 AM
                    45 responses
                    3,992 views
                    3 likes
                    Last Post johntraderuser2  
                    Started by TAJTrades, Today, 09:46 AM
                    0 responses
                    8 views
                    0 likes
                    Last Post TAJTrades  
                    Working...
                    X