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

logic error returns zero

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

    logic error returns zero

    I need help, anyone, to determine why the code below returns zero when two adjacent periods are the same (ie. Weighted[0] = Weighted[1]) - and what to do to fix it, please. Thank you.

    //Calculate the WeightedCloseRatio
    double WtCRatio = 0;
    if (Weighted[0] - Weighted[1] < 0)
    {
    if (Weighted[0] - Weighted[1] != 0)
    WtCRatio = ((Weighted[0] - Weighted[1]) / Weighted[0]);
    else
    WtCRatio = -0.0001;
    }
    else
    {
    if (Weighted[0] - Weighted[1] != 0)
    WtCRatio = ((Weighted[0] - Weighted[1]) / Weighted[1]);
    else
    WtCRatio = 0.0001;
    }

    #2
    Hello billm,

    From this snippet it seems like you want to make sure you're not dividing by zero and assigning a default value based on two potential if (condition == 0) for this occurrence.

    What you will need to do is decide what to do when the difference == 0 and assign it in one place, not two.

    You can check a good implementation to avoid "divide by zero" cases with our default stochastics indicator. Click Tools > Edit NinjaScript > Indicator and open Stochastics.

    You might also need to check the floating point precision. Tips on dealing with this below:
    Floating-Point Arithmetic
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      It is possible that at the close of the bar a value for index[0] does not existin the referenced indicator, to avoid this little problem assign a static within the indicator.

      Comment


        #4
        Thank you for your reponses.

        You are correct Ryan, the calc is kaput if either the numerator or denominator returns zero as the WtCRatio being calculated is used subsequently in a denominator calculation. I have studied your suggested implementation in the 'stochastics' indicator but, do not understand how to adapt it to my issue. I have attempted it multiple ways but get errors I cannot resolve. How would this be done?

        Thank you 'Sleeping Troll' for your suggestion. Unfortunately, I am too inexperienced with Ninjascript, much less C# to know how to do this. (assign a static within the indicator) How would that be done?

        Thank you again for your responses.

        Comment


          #5
          Hello Bill,

          The Stochastics will use this line:
          Code:
           
           
          K.Set(100 * SUM(nom, Smooth)[0] / (SUM(den, Smooth)[0] == 0 ? 1.0 : SUM(den, Smooth)[0]));

          It waits until the calculation is needed in a plot before checking for zero. If true, will assign a value of 1, otherwise it will use the indicator calculation. You could try something like this.

          You could also check if (not zero) first, assign two different values depeding on whether the result is positive or negative, and then assign a third value to it if zero. Snippet of this is below:

          Code:
           
          if (Weighted[0] - Weighted[1] != 0)
          {
          if (Weighted[0] - Weighted[1] < 0)
          WtCRatio = ((Weighted[0] - Weighted[1]) / Weighted[0]);
           
          if (Weighted[0] - Weighted[1] > 0)
          WtCRatio = ((Weighted[0] - Weighted[1]) / Weighted[1]); 
          }
           
          else
          WtCRatio = 0.0001;
          This is a little bit different than your first post because I had to choose one value to assign when the difference W[0] - W[1] == 0.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Thank you for the explanatory snippet, Ryan.

            I have changed the calculation around some, away from my ancient hand calculator version. It now looks for zero first, assigns a value if zero then calculates positive or negative:

            //Calculate the WeightedCloseRatio
            double WtCRatio = 0.0001;

            if (Weighted[0] - Weighted[1] == 0)
            {
            WtCRatio = 0.0001;
            }
            else
            {
            WtCRatio = (Weighted[0] - Weighted[1]) / Math.Min(Weighted[0], Weighted[1]);
            }

            However, I now realize I should preserve the sign (+/-) and the previous WtCRatio rather than forcing it to +0.0001 if (Weighted[0] - Weighted[1] == 0). My attempts at this have not worked. Would I have to turn WtCRatio into a 'DataSeries' to look back to a prior value and thus retain the correct sequence of +/- signs?

            Thank you

            Comment


              #7
              If you reference an array(dataseries... or other related to a dataseries) by bar index at the beginning of a bar it may not yet exist, best to create a static that will always have some value, example...

              myValue = Somevar[barsAgo];

              if this is in an indicator and your strategy has just closed a bar (bar[1]), bar[0] may not yet exist, if you reference Somevar[0] it may produce a reference error or it may return null or zero.

              The solution is to assign the value to a static var within the indicator...

              SomeStatic = Somevar[0];

              Then reference that var from your strategy...

              myValue = SomeStatic;

              Comment


                #8
                Hello billm,

                I am jumping in here, so I may be off, but if you are just getting the 'last' value and plotting that in the 0 value case, why not just store it and use it again instead of going through all the data series stuff? Maybe something like this:

                if (Weighted[0] - Weighted[1] == 0)
                {
                WtCRatio = dLastWtCRatio;
                }
                else
                {
                WtCRatio = (Weighted[0] - Weighted[1]) / Math.Min(Weighted[0], Weighted[1]);
                dLastWtCRatio = WtCRatio;
                }
                Last edited by mountainclimber; 05-07-2010, 10:29 AM.

                Comment


                  #9
                  Hello Billm,

                  Yes, turning it into a data series and plotting .0001 only when W[0] - W[1] == 0 is a good approach.

                  This help guide article can help with DataSeries.

                  Declare your variable in the variables region:
                  private DataSeries WtCRatio;

                  Instantiate in the Initialize() method:
                  WtCRatio = new DataSeries(this);

                  Set the DataSeries in OnBarUpdate():
                  WtCRatio.Set((Weighted[0] - Weighted[1]) / (Math.Min(Weighted[0], Weighted[1])));



                  Before you plot, check the element that creates the divide by zero issue. Assign the value for this case otherwise use the value of your DataSeries.

                  Plot1.Set(Weighted[0] - Weighted[1] == 0 ? .0001 : WtCRatio[0]);
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    billm,

                    I replied to your PM but thought others might be interested in this as well...

                    Just put this up near your variables area so it is initialized and declared as 0.0:

                    //variables or just outside the OnBarUpdate block of code
                    double dLastWtCRatio = 0.0;

                    ...that way it gets set as 0.0 at the start but after that it always holds the most recent value. The data series stuff that RyanM put is correct and it will work too but since you only need access to the last value, it is unecessary...

                    Last edited by mountainclimber; 05-07-2010, 10:29 AM.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Rapine Heihei, 04-23-2024, 07:51 PM
                    2 responses
                    30 views
                    0 likes
                    Last Post Max238
                    by Max238
                     
                    Started by Shansen, 08-30-2019, 10:18 PM
                    24 responses
                    943 views
                    0 likes
                    Last Post spwizard  
                    Started by Max238, Today, 01:28 AM
                    0 responses
                    9 views
                    0 likes
                    Last Post Max238
                    by Max238
                     
                    Started by rocketman7, Today, 01:00 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post rocketman7  
                    Started by wzgy0920, 04-20-2024, 06:09 PM
                    2 responses
                    28 views
                    0 likes
                    Last Post wzgy0920  
                    Working...
                    X