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

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 Haiasi, 04-25-2024, 06:53 PM
            2 responses
            17 views
            0 likes
            Last Post Massinisa  
            Started by Creamers, Today, 05:32 AM
            0 responses
            5 views
            0 likes
            Last Post Creamers  
            Started by Segwin, 05-07-2018, 02:15 PM
            12 responses
            1,786 views
            0 likes
            Last Post Leafcutter  
            Started by poplagelu, Today, 05:00 AM
            0 responses
            3 views
            0 likes
            Last Post poplagelu  
            Started by fx.practic, 10-15-2013, 12:53 AM
            5 responses
            5,407 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Working...
            X