Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ToCompare vs ApproxCompare

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

    ToCompare vs ApproxCompare

    Dear Support,

    NT8 uses ApproxCompare in default Stochastics indicator to ensure a series is not divided by zero. However, the ApproxCompare dose not seem to be supported in NT8 since it will genrate an error if used for a custom script in a similar manner.

    The closest supported feature I have found is "CompareTo." However it is not clear if it functions the same as ApproxCompare. So, I am trying to find the equivalent supported NT8 code for the following line from default Stochastics.

    Code:
    		protected override void OnBarUpdate()
    		{
    			double min0 = min[0];
    			nom[0]		= Close[0] - min0;
    			den[0]		= max[0] - min0;
    
    [COLOR="Magenta"]			if (den[0].ApproxCompare(0) == 0)[/COLOR]
    				fastK[0] = CurrentBar == 0 ? 50 : fastK[1];
    			else
    				fastK[0] = Math.Min(100, Math.Max(0, 100 * nom[0] / den[0]));
    
    			// Slow %K == Fast %D
    			K[0] = smaFastK[0];
    			D[0] = smaK[0];
    		}
    All I really want is to make sure den[0] is a non-zero value.

    The "if (den[0].CompareTo(0) == 0)" does not work.

    Many Thanks.
    Last edited by aligator; 08-31-2018, 09:02 AM.

    #2
    Originally posted by aligator View Post
    All I really want is to make sure den[0] is a non-zero value.

    The "if (den[0].ToCompare(0) == 0)" does not work.
    If that's all you want, why is this not sufficient?

    Code:
    if (den[0] != 0)

    Comment


      #3
      Originally posted by bltdavid View Post
      If that's all you want, why is this not sufficient?

      Code:
      if (den[0] != 0)
      You cannot compare doubles that way. Rounding considerations mean that you have to compare doubles by reference to a delta.

      Comment


        #4
        Originally posted by koganam View Post
        You cannot compare doubles that way. Rounding considerations mean that you have to compare doubles by reference to a delta.
        Thanks,

        Yes, I know I could use " if (den[0] != 0)" .

        However, I am tying to understand why and how Stochastic indicator uses ApproxCompare without throwing an error.

        Thanks.

        Comment


          #5
          Originally posted by aligator View Post
          However, I am tying to understand why and how Stochastic indicator uses ApproxCompare without throwing an error..
          Make sure you are including this using statement


          using NinjaTrader.Core.FloatingPoint;


          Documentation for ApproxCompare in Help Guide Best Practices Here


          https://ninjatrader.com/support/help..._practices.htm


          1st thing under Misc ...



          -=Edge=-
          NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

          Comment


            #6
            Originally posted by -=Edge=- View Post
            Make sure you are including this using statement


            using NinjaTrader.Core.FloatingPoint;


            Documentation for ApproxCompare in Help Guide Best Practices Here


            https://ninjatrader.com/support/help..._practices.htm


            1st thing under Misc ...



            Thank you Edge,

            I had noticed in best practices that ApproxCompare() is a Custom Extension Method. However, any reference to "Using declaration" was missing, and that was the key.

            I had assumed that the CompareTo() is the equivalent method but there was no explanations.

            Thanks a bunch.

            Comment


              #7
              Originally posted by aligator View Post
              I had assumed that the CompareTo() is the equivalent method but there was no explanations..
              This might also help..

              I have this link tagged as very informative information on working with floating point numbers..


              This post is a more carefully thought out and peer reviewed version of a floating-point comparison article I wrote many years ago. This one gives solid advice and some surprising observations about…




              -=Edge=-
              NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

              Comment


                #8
                Originally posted by koganam View Post
                You cannot compare doubles that way. Rounding considerations mean that you have to compare doubles by reference to a delta.
                Of course you can. Maybe not always. It depends upon what you're doing.

                (I know what you're referring to -- subtracting and comparing to double.epilson.
                For example, see here.)

                If you really want to know if a double value is not zero (because, say, it was manually
                set to zero to indicate a starting value) this works fine.

                In cases where you have a calculated value, this may or may not work. You have to understand the calculations being performed before reference to a delta becomes truly necessary.

                Comment


                  #9
                  Originally posted by bltdavid View Post
                  If you really want to know if a double value is not zero
                  Thanks,

                  This was exactly what I wanted to do, dividing a double value by the difference between two other double series and wanted to avoid dividing by a possible zero.

                  Many thanks.

                  Comment


                    #10
                    Originally posted by bltdavid View Post
                    Of course you can. Maybe not always. It depends upon what you're doing.

                    (I know what you're referring to -- subtracting and comparing to double.epilson.
                    For example, see here.)

                    If you really want to know if a double value is not zero (because, say, it was manually
                    set to zero to indicate a starting value) this works fine.

                    In cases where you have a calculated value, this may or may not work. You have to understand the calculations being performed before reference to a delta becomes truly necessary.
                    "cannot" is not being used in the sense of impossibility: it is being used in the sense of not being defensive coding practice. Defensive coding requires using a delta to compare doubles, precisely because it obviates all issues, while also handling the particular instance that you cite of checking a value that was previously set, an instance where it is admittedly unnecessary to use a delta comparison.
                    Last edited by koganam; 09-04-2018, 01:06 AM.

                    Comment


                      #11
                      Hello aligator,

                      Thank you for your post.

                      The 'using NinjaTrader.Core.FloatingPoint;' statement should be included by default when creating an indicator through the NinjaScript Editor. Do you have a use case where this using statement was not included?
                      Originally posted by aligator View Post
                      Thank you Edge,

                      I had noticed in best practices that ApproxCompare() is a Custom Extension Method. However, any reference to "Using declaration" was missing, and that was the key.

                      I had assumed that the CompareTo() is the equivalent method but there was no explanations.

                      Thanks a bunch.
                      Information on CompareTo() can be found at the following link: https://docs.microsoft.com/en-us/dot...ramework-4.7.2

                      Please let me know if you have any questions.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      166 responses
                      2,233 views
                      0 likes
                      Last Post sidlercom80  
                      Started by thread, Yesterday, 11:58 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post thread
                      by thread
                       
                      Started by jclose, Yesterday, 09:37 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post jclose
                      by jclose
                       
                      Started by WeyldFalcon, 08-07-2020, 06:13 AM
                      10 responses
                      1,414 views
                      0 likes
                      Last Post Traderontheroad  
                      Started by firefoxforum12, Yesterday, 08:53 PM
                      0 responses
                      11 views
                      0 likes
                      Last Post firefoxforum12  
                      Working...
                      X