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 Hwop38, 05-04-2026, 07:02 PM
            0 responses
            160 views
            0 likes
            Last Post Hwop38
            by Hwop38
             
            Started by CaptainJack, 04-24-2026, 11:07 PM
            0 responses
            307 views
            0 likes
            Last Post CaptainJack  
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            244 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            348 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            178 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Working...
            X