• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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;
    
    			if (den[0].ApproxCompare(0) == 0)
    				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..


              https://randomascii.wordpress.com/20...-2012-edition/



              -=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.
                      Patrick H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by stoner, Today, 02:36 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post stoner
                      by stoner
                       
                      Started by victorsmith, Today, 02:23 PM
                      1 response
                      3 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by juju075, Today, 02:04 PM
                      0 responses
                      4 views
                      0 likes
                      Last Post juju075
                      by juju075
                       
                      Started by FiftyFib FX, Today, 01:45 PM
                      0 responses
                      4 views
                      0 likes
                      Last Post FiftyFib FX  
                      Started by researcher247, Today, 01:21 PM
                      3 responses
                      5 views
                      0 likes
                      Last Post NinjaTrader_Tyler  
                      Working...
                      X