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

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.
          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Harry, 05-02-2018, 01:54 PM
          10 responses
          3,203 views
          0 likes
          Last Post tharton3  
          Started by cre8able, Yesterday, 01:16 PM
          3 responses
          11 views
          0 likes
          Last Post cre8able  
          Started by ChartTourist, Today, 08:22 AM
          0 responses
          6 views
          0 likes
          Last Post ChartTourist  
          Started by LiamTwine, Today, 08:10 AM
          0 responses
          2 views
          0 likes
          Last Post LiamTwine  
          Started by Balage0922, Today, 07:38 AM
          0 responses
          5 views
          0 likes
          Last Post Balage0922  
          Working...
          X