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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            149 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            84 views
            1 like
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            129 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            125 views
            1 like
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            102 views
            0 likes
            Last Post CarlTrading  
            Working...
            X