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

Question for the Community not exactly a support question

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

    Question for the Community not exactly a support question

    I am not a trained programmer... I just hack away knowing very little about programming till I get something to work, and often learn from other people's code.

    Often in John Ehler's various published code written in TradeStation EL, you will see A line like this to prevent division by zero.

    From Hilbert Period and indicators that use it as part:

    Code:
    If Q1 <> 0 and Q1[1] <> 0 then
                      Do Some division.....
    I don't know what TradeStation EL does when Q1 above is equal to zero, which if allowed to be evaluated would result in division by zero.

    It has always been a mystery to me what TradeStation would do in a case when Q1 is equal to zero, because Ehlers never gives code to do something different if the zero test above says we are about to divide by zero. There are never any lines for that situation. What would happen on that bar if Q1 was = 0? In a simple situation maybe nothing gets plotted for that bar,...

    but what happens on the next bar with code the references the value [1] (one bar ago?

    To make CoronaCycle plot so that it looks like Ehlers TradeStation plot I used:

    Code:
    if (denom == 0)  //this step is not in other versions, but if we don't do this test, 
                                       // The plot does not match the photo in Ehler's article
                {                
                    dc.Set(dc[1]);
                }    
                
                if (denom != 0)
                {    
                     dc.Set(.5*num/denom);    
                }
    I am saying if we are about to divide by zero, don't, instead plug in the value [1] (one bar ago).

    Today I was reading the code inside a community shared indicator of Ehler's called Smoothed Adaptive Momentum (SAM).

    The author of this code did something that is probably the correct way to handle situations like this, where you must avoid dividing by zero, but I dont understand what it does. Would someone be so kind as to explain it to me?

    The relevant section of code where the author is testing for and avoiding division by zero is:

    Code:
    if (!IsZero(Q1[0])&& !IsZero(Q1[1]))
                {
                    DeltaPhase.Set((I1[0] / Q1[0] - I1[1]/Q1[1]) / (1 + I1[0] * I1[1]/(Q1[0] * Q1[1])));
                }
                
                if (DeltaPhase[0] < 0.1) 
                {
                    DeltaPhase.Set(0.1);
                }
                else if (DeltaPhase[0] > 1.1) 
                {
                    DeltaPhase.Set(1.1);
                }
                double MedianDelta = CurrentBar > 4 ? GetMedian(DeltaPhase, 5) : 0.0;
                double DC = IsZero(MedianDelta) ? 15 : ((6.28318/MedianDelta) + 0.5);
    He then defines isZero withthis:

    Code:
    private bool IsZero(double v)
            {
                return Math.Abs(v - v) < double.Epsilon;
            }
    I have attached the complete indicator.

    I would like to know what this double.Episilon is, and how it is used here.... I'm guessing it helps avoid division by zero, and gives some alternate handling incase we are about to divide by zero, but I can't figure out how it works.
    Attached Files

    #2
    Originally posted by Crassius View Post
    He then defines isZero withthis:

    Code:
    private bool IsZero(double v)
            {
                return Math.Abs(v - v) < double.Epsilon;
            }
    I have attached the complete indicator.

    I would like to know what this double.Episilon is, and how it is used here.... I'm guessing it helps avoid division by zero, and gives some alternate handling incase we are about to divide by zero, but I can't figure out how it works.







    Lower case epsilon is used to mean an incredibly small number, that is specifically not zero, but is still atomically small. It is used in calculus to rigorously define derivatives.

    Just wait until you come across doubles that print out the same, but won't match on a compare.

    Comment


      #3
      Crassius, as further background - are you aware of this tip?

      BertrandNinjaTrader Customer Service

      Comment


        #4
        Thank to both of you

        What layman would ever have guessed that math on a computer could be so imprecise.

        Your answers were helpful and led me to this outside resource I'll post for anyone else that may come across this is future. If you feel the need to dive al the way into this subject:

        What Every Computer Scientist Should know about Floating Point Arithmetic
        http://docs.oracle.com/cd/E19957-01/..._goldberg.html

        Would someone please give me the verbose psuedocode to make sure I understand this code block:

        Code:
        private bool IsZero(double v)
                {
                    return Math.Abs(v - v) < double.Epsilon;
                }

        Comment


          #5
          Originally posted by Crassius View Post
          What layman would ever have guessed that math on a computer could be so imprecise.

          Your answers were helpful and led me to this outside resource I'll post for anyone else that may come across this is future. If you feel the need to dive al the way into this subject:

          What Every Computer Scientist Should know about Floating Point Arithmetic


          Would someone please give me the verbose psuedocode to make sure I understand this code block:

          Code:
          private bool IsZero(double v)
                  {
                      return Math.Abs(v - v) < double.Epsilon;
                  }
          The method is just a fancy way to test for zero, returning a bool. It is more precise than saying
          Code:
          if (v==0) return true;
          because of the floating-point precision issue. Frankly,
          Code:
          if (v < double.Epsilon) return true;
          would be just as good a test.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Johnny Santiago, 10-11-2019, 09:21 AM
          95 responses
          6,193 views
          0 likes
          Last Post xiinteractive  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          2 responses
          11 views
          0 likes
          Last Post xiinteractive  
          Started by Irukandji, Today, 09:34 AM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by RubenCazorla, Today, 09:07 AM
          1 response
          5 views
          0 likes
          Last Post RubenCazorla  
          Started by TraderBCL, Today, 04:38 AM
          3 responses
          25 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X