Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Divisions of integers not plotting

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

    Divisions of integers not plotting

    Hi, I'm using two integers to count the occurrences of a event and I'm trying to plot the %.

    What I do is the following (simplified):

    //variables declarations;
    int event1 = 0;
    int event2 = 0;

    //on bar update
    if (condition) {event1++;}
    else {event2++;}

    Plot0.Set(100/(event2+event1)*event1);


    If i try to plot just event1 or event2, or their sum, it plots correctly. But that division don't plot anything.

    Is there a reason for this? Am I doing something wrong?

    #2
    Originally posted by TND-storm View Post
    Hi, I'm using two integers to count the occurrences of a event and I'm trying to plot the %.

    What I do is the following (simplified):

    //variables declarations;
    int event1 = 0;
    int event2 = 0;

    //on bar update
    if (condition) {event1++;}
    else {event2++;}

    Plot0.Set(100/(event2+event1)*event1);


    If i try to plot just event1 or event2, or their sum, it plots correctly. But that division don't plot anything.

    Is there a reason for this? Am I doing something wrong?
    Print to the output window to see the value that is trying to be plotted:






    Print ( "Result=" + (100/(event2+event1)*event1 ) );

    Comment


      #3
      Originally posted by TND-storm View Post
      Hi, I'm using two integers to count the occurrences of a event and I'm trying to plot the %.

      What I do is the following (simplified):

      //variables declarations;
      int event1 = 0;
      int event2 = 0;

      //on bar update
      if (condition) {event1++;}
      else {event2++;}

      Plot0.Set(100/(event2+event1)*event1);


      If i try to plot just event1 or event2, or their sum, it plots correctly. But that division don't plot anything.

      Is there a reason for this? Am I doing something wrong?
      Whenever you divide, you MUST account for a possible division by zero. You have not done so.

      Given that you initialize both events to zero, but event2 gets immediately updated, your denominator will be zero until event1 occurs. The easiest thing to do would be to use an "if" filter to ensure that your possibly errant division cannot occur, unless and until the constituents of the denominator are not zero.

      Comment


        #4
        Thanks guys, I was dividing by 0 indeed, a awful logical error.

        Now I have another problem: it initially plots the correct value but then both values drop to 0 and stay there.
        Do you have any idea of why it happens?

        Here's the function:

        Code:
        protected override void OnBarUpdate()
                {
        			if (Bars.BarsSinceSession > 0) 
        			{
        				if (Close[0] >= Close[1])
        				{
        					if (Close[0] > (Close[1] + (TickSize * closeSpread)))
        					{upHigher++;}
        					else {upMiddle++;}
        				}
        				else 
        				{
        					if (Close[0] < (Close[1] - (TickSize * closeSpread)))
        					{downLower++;}
        					else {downMiddle++;}
        				}
        				
        				
        				Plot0.Set(100/(upHigher+upMiddle)*upHigher);
        				Plot1.Set(100/(downLower+downMiddle)*downLower);
        				//Plot2.Set(downMiddle);
        				
        				Print ( "Result=" + 100/(upHigher+upMiddle)*upHigher);
        			}
                }
        And here's the resulting plot:
        Attached Files

        Comment


          #5
          Originally posted by TND-storm View Post
          Thanks guys, I was dividing by 0 indeed, a awful logical error.

          Now I have another problem: it initially plots the correct value but then both values drop to 0 and stay there.
          Do you have any idea of why it happens?

          Here's the function:

          Code:
          protected override void OnBarUpdate()
                  {
          			if (Bars.BarsSinceSession > 0) 
          			{
          				if (Close[0] >= Close[1])
          				{
          					if (Close[0] > (Close[1] + (TickSize * closeSpread)))
          					{upHigher++;}
          					else {upMiddle++;}
          				}
          				else 
          				{
          					if (Close[0] < (Close[1] - (TickSize * closeSpread)))
          					{downLower++;}
          					else {downMiddle++;}
          				}
          				
          				
          				Plot0.Set(100/(upHigher+upMiddle)*upHigher);
          				Plot1.Set(100/(downLower+downMiddle)*downLower);
          				//Plot2.Set(downMiddle);
          				
          				Print ( "Result=" + 100/(upHigher+upMiddle)*upHigher);
          			}
                  }
          And here's the resulting plot:
          You are dividing integers into other integers. That means that any result < 1 is identically 0. If you want to see fractional values, cast your variables to double, or just use 100.0 instead of 100. In other words, make the numerator an explicit double value, instead of an integer.

          Comment


            #6
            And that solves the problem! Thank you!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            566 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            330 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            547 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            548 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X