Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Attempted to divide by zero

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

    Attempted to divide by zero

    Hi,

    I received an error when loading strategy saying Attempted to divide by zero when I had the following in my script:

    upPercentage = ((upBar)/(upBar + dnBar))*100;

    dnPercentage = ((dnBar)/(upBar + dnBar))*100;

    How do I resolve this?

    Thanks

    #2
    Hello AgriTrdr,

    Thank you for your post.

    You will need to debug the script to find where in the logic it is attempting to divide by zero and why.

    Below is a support article on debugging using prints.

    Comment


      #3
      Check (upBar + dnBar) > 0 before dividing.
      eDanny
      NinjaTrader Ecosystem Vendor - Integrity Traders

      Comment


        #4
        Thank you eDanny.

        Comment


          #5
          Hi,

          I did the following and it removed the error, but I'm still getting 0% majority of the time. I did use Print to verify that my upBar and dnBar are working correctly and they are which leads me to believe there is an issue with how I'm checking to see the upBar > 0 and dnBar > 0

          if (upBar > 0)
          {
          upPercentage = ((upBar)/(upBar + dnBar))*100;
          Print(Times[0][0] + "|" + Instrument.FullName + "|" + "UP%" + "|" + upPercentage);
          }

          if (dnBar > 0)
          {
          dnPercentage = ((dnBar)/(upBar + dnBar))*100;
          Print(Times[0][0] + "|" + Instrument.FullName + "|" + "DN%" + "|" + dnPercentage);
          }

          Comment


            #6
            What are your values for upBar and dnBar? Is dnBar producing a negative number cancelling out upBar? What type of value is upPercentage? Int? Double?

            Comment


              #7
              Hi,

              Here's the entire logic. upBar and dnBar are private int. upPercentage and dnPercentage are private double. I was able to solve the Attempted to divide by zero error by changing upBar = 1 and dnBar = 1;

              But the percentage is still showing up as 0.

              Code:
              if ((Bars.IsFirstBarOfSession) || (SRLine[0] != SRLine[1]))
              {
              upBar = 1;
              dnBar = 1;
              }
              else if (Closes[0][0] > Opens[0][0])
              {
              upBar += 1;
              }
              else if (Closes[0][0] < Opens[0][0])
              {
              dnBar += 1;
              }
              ​
              upPercentage = ((upBar)/(upBar + dnBar))*100;
              dnPercentage = ((dnBar)/(upBar + dnBar))*100;
              
              Print(Times[0][0] + "|" + Instrument.FullName + "|" + upPercentage + "|" + dnPercentage);
              ​
              
              if ((upPercentage-dnPercentage) > Percentage)
              {
              BarBrushes[0] = buyCandle;
              }
              else if ((dnPercentage-upPercentage) > Percentage)
              {
              BarBrushes[0] = sellCandle;
              }
              else
              {
              BarBrushes[0] = BarBrushes[1];
              }

              Comment


                #8
                It's because you are using int. I just tested it on MYM. Notice how I used Prints to debug?

                Print("upBar: " + upBar);
                Print("dnBar: " + dnBar);
                Print("upBar + dnBar: " + (upBar + dnBar));
                Print("(upBar / (upBar + dnBar)): " + ((double)upBar / ((double)upBar + (double)dnBar)));
                upPercentage = ((double)upBar / ((double)upBar + (double)dnBar)) * 100;
                dnPercentage = ((double)upBar / ((double)upBar + (double)dnBar)) * 100;
                Print("upPercentage: " + upPercentage);
                Print("dnPercentage: " + dnPercentage);​

                Output:
                upBar: 151
                dnBar: 185
                upBar + dnBar: 336
                (upBar / (upBar + dnBar)): 0.449404761904762
                upPercentage: 44.9404761904762
                dnPercentage: 44.9404761904762
                3/12/2025 10:55:45 AM|MYM 03-25|44.9404761904762|44.9404761904762​

                Because you are adding both bars and using that to divide, that number you get is under 0. Int is essentially cutting off anything after the decimal. I even tried to hard code upBar to 300. Even when the combination of both hits 500, I still get 0, which means it's cutting off and not rounding, which is normal behavior for Int. You don't need to change upBar and dnBar from int to double, but you would at least need to convert them to double before it divides so you can get a number other than zero.
                Last edited by rockmanx00; 03-12-2025, 01:00 PM.

                Comment


                  #9
                  Thank you! That works great.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Today, 05:17 AM
                  0 responses
                  48 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  126 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  66 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  42 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  46 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X