Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to trigger realworld price levels when NT steps in prices with 17 decimal places?

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

    How to trigger realworld price levels when NT steps in prices with 17 decimal places?

    Hello

    I have an Indicator with an OnBarUpdate() being called on an Indicator.Calculate = Calculate.OnPriceChange.

    I wish to store indicator identified price levels as keys in a Dictionary so that when price returns to those levels I can run logic and move an drawing objects anchor value to an adjacent tradeable price value each time price hits it.

    However Close[0] returns a double type value which may or may not be an actual tradeable price value. The value can be anywhere up to 17 decimal places.
    All price values it seems are stored as doubles, which I have found very often can't be rounded to actual prices without conversion to decimal number types.

    My first thought is to try and control actual price levels by convert the Close[0] double to a decimal and Math.Round it to 5 decimal places (Forex pips/pippetts prices).
    But a rounding error of only 0.00001 or 1 pipette is potentially enough to make it possible for an OnPriceChange to fail to match and step straight past a value save in the Dictionary key.

    Then at this point this indicator only works for forex, i would have to run logic for every different instrument to determine how many decimal places to round to... at this point its seems like its getting way too convoluted just to work with real world price values and I feel like I am totally missing something obvious that is already built in to Ninja Trader.

    I would rather not have to iterate every key in the dictionary every price change to see if a dictionary key has been crossed. I have seen FormatPrice() but that returns a string value with a backtick in it so it can't be simply cast back to a double and applied as a chartAnchor value.(But I will use it if that really is the only option)

    I essentially wish to use OnPriceChange's latest price to identify if it is crossing a ChartAnchor's price and if so have a ChartAnchor update with it and save a dictionary key value for the price level next to it to be used to test when price steps past the current ChartAnchor's price.

    What I (mistakenly?) think I need is what value should be stored as a dictionary key to a reliably triggered by price passing it, that avoids having to have 12 decimal places worth of dictionary keys to insure a 0.00001 move isn't stepped over?

    I am sure however I must be missing something staring me straight in the face. Sorry

    #2
    Hello SmartArtsStudio,

    Thank you for your post.

    Sounds like you're running into issues with floating point arithmetic. An overview of some approaches that may help with this issue can be found in our help guide here:



    You can also try using ApproxCompare():



    Please let us know if we may be of further assistance to you.

    Comment


      #3
      If comparing something to price (Close[0]) you can eliminate the extra decimal places of price by rounding to the tick size of the instrument.
      Instrument.MasterInstrument.RoundToTickSize(Close[0])
      eDanny
      NinjaTrader Ecosystem Vendor - Integrity Traders

      Comment


        #4
        Thanks Kate and eDanny

        Does RoundToTickSize() look like its doing what its suppose to? It doesn't look like it does anything?

        Click image for larger version  Name:	Capture.PNG Views:	0 Size:	19.7 KB ID:	1159194

        Or does this just the result of the same floating point arithmetic issues mentioned.

        Is there a page that explains TickSize in detail, its role in price and how it can be used? I seem to only find methods references that assume this knowledge.
        I have a game engine background which I think is confusing my understanding of the tick... games would use the "tick size" to invent prices in between Close[0] and Close[1].
        Last edited by SmartArtsStudio; 06-07-2021, 07:56 PM. Reason: Removed price question which was of course totally caused by double casting.

        Comment


          #5
          Hello SmartArtsStudio,

          Try printing the values to the NinjaScript Output window to understand the behavior.

          For example the print:
          Code:
          Print(string.Format("{0} | {1} {2} {3} | Close[0]: {4}, mySMA[0]: {5}, RoundToTickSize(mySMA[0]): {6}", Time[0], Instrument.FullName, BarsPeriod.Value, BarsPeriod.BarsPeriodType, Close[0], mySMA[0], Instrument.MasterInstrument.RoundToTickSize(mySMA[0])));
          Produces the output:
          6/8/2021 7:48:00 AM | AUDUSD 1 Minute | Close[0]: 0.77347, mySMA[0]: 0.773634285714286, RoundToTickSize(mySMA[0]): 0.77363
          Below is a link to a forum post that demonstrates using prints to understand behavior.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks Chelsea

            Also very beneficial for understanding the "tick":
            "Multi-Time Frame & Instruments"

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            579 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
            554 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