Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Double division not working as expected

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

    Double division not working as expected

    I am trying to calculate the body to bar range ratio, my code works with most assets, but for YM and BTC the code is doing integer division instead, so I'm getting 1 or 0 for calculations:

    BarHeight, BodyHeight and BodyBarRatio are being stored as Series<double>

    Code:
    BarHeight[0] = High[0] - Low[0];
    BodyHeight[0] = Math.Abs(Open[0] - Close[0]);
    BodyBarRatio[0] = BodyHeight[0] / BarHeight[0];​
    Is there some nuances to these assets that make this sort of calculation incorrect?
    Last edited by andrewduffey; 11-11-2023, 11:32 AM.

    #2
    How do you know the code is doing integer arithmetic?

    For YM and BTC, you're getting a '0' or '1' for every bar?

    That seems pretty odd.

    Looking at your simple calculations, the only time this
    should occur is for Doji bars. Integer arithmetic is not
    the culprit (and is not being used), something else is
    happening.

    -=o=-

    A normal Doji bar is Open == Close, right?

    As you can obviously see, all normal Doji bars will have
    BodyHeight and BodyBarRatio calculated as zero.

    But what about a 'super' Doji bar?

    That is,
    Open == Close == High == Low.

    This can happen on time-based bars during low volume
    periods, like in the middle of the night -- ie, no price
    movement whatsoever for the entire bar.

    When you have a super Doji bar, BarHeight is calculated
    as zero, and the division by zero for BodyBarRatio should
    cause an exception during execution.

    -=o=-

    Who is your data provider?

    Have you tried deleting the cache folder and reloading
    all your historical data manually?

    What do your 1-minute YM and BTC charts look like?
    I mean, do you have valid bars on a basic chart?

    Are you using any secondary data series?

    What is BarType and Period on the chart you are
    using for your calculations?

    Can you create a minimal script that reproduces
    what you're seeing and post it here?

    Comment


      #3
      You might also consider using RoundToTickSize.

      Comment


        #4
        It looks like it is being calculated correctly in the series, but all the values in the databox for those instruments are being converted to integers, so the values less that 0.5 is rounded to 0 and above are being rounded to 1.

        Comment


          #5
          Hello andrewduffey,

          Thanks for your notes.

          Have you added prints to the script that print out the calculated values to the NinjaScript Output window to see how they are evaluating?

          If not, please add prints to the script that print out the BarHeight[0], BodyHeight[0], BodyBarRatio[0], and the Time[0] to see how those values are evaluating in the script.

          When you mention the Data Box values are being converted, are these values being assigned to a plot in the script?

          If so, are you comparing the plot value in the Data Box to the prints in the Output window?

          Please send us a simple test script that demonstrates the behavior along with the steps and settings you are using to reproduce the behavior so we may look into this matter further.
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          576 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          334 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
          553 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X