Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Series<double> [0] is not starting from default value of 0 on start of new bar

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

    Series<double> [0] is not starting from default value of 0 on start of new bar

    Hello,
    I am experiencing an unexpected behavior during market playback, with chart data series using Tick Replay and calculate = OnEachTick and IsSuspendedWhileInactive=false;


    I am expecting when a new bar starts, the default (unset) value of the series at index [0] will be 0.0 , the default value of a double, since the bar is new and a seriesT is supposed to have a new element (array row) for each bar on the chart.

    In code, at start of the indicator class, I define the var for a private Series<double>
    when state = dataLoaded, I instantiate the series using MySeries = new Series<double>(this);

    The code block uses mySeries[0] ++ to increment the value of the series element


    When I debug, using the output to write the value of the series at index [0] it's value seems to be a random number, and it is not equal to the value of the previous bar in the series ie [1].
    I test for IsFirstTickOfBar and then print the value of the series at [0]


    In this debug segment, I am using string "prc chg up" to debug the value of the series.
    This is a chart using bars built every 5 seconds. (but I get the same behavior on 1 minute bars)

    Code:
    11:59:54.429000, prc chg up=200, prc chg dn=189  <= represents values set in code, timestamp is from OnMarketData .Time value, formatted to milliseconds
    11:59:54.488000 OnMarketData event , Bar# 7248, event type: Last Bid=8203.5, Ask=8203.75, Price=8203.5, Volume=1
    11:59:54.488000, prc chg up=200, prc chg dn=190  <= code incremented the value of series debugged as "prc chg dn"
    
    OBU first tick of bar  <= a new bar has started based on a new OMD "Last" event rcvd with timestamp outside of the bar time constraint.
    
    11:59:55.210000 OnMarketData event , Bar# 7249, event type: Last Bid=8203.5, Ask=8203.75, Price=8203.75, Volume=1
    
    7250 series count <= .count of series, shows it has the same number of bars on the chart.
    series 0 val=182 <= shows the random number ??? it is not 0.0 nor is it the last value of the previous series element
    series[1] val=200  <= is as expected, the last value from the previous bar of the series debugged as "prc chg up"
    
    before incr182
    after incr183 <= testing the ++ increment method
    11:59:55.210000, prc chg up=183, prc chg dn=211
    
    11:59:55.214000 OnMarketData event , Bar# 7249, event type: Last Bid=8203.5, Ask=8204, Price=8204, Volume=1
    before incr183
    after incr184
    11:59:55.214000, prc chg up=184, prc chg dn=211
    
    11:59:55.226000 OnMarketData event , Bar# 7249, event type: Last Bid=8204, Ask=8204.25, Price=8204.25, Volume=1
    before incr184
    after incr185
    
    ad infinitum ...

    I can change this behavior to do what I expect, i.e. to have the default value of [0] at the start of a new bar to be 0, by using an if (IsFirstTIckOfBar) mySeries[0] = 0.0;
    But I was expecting a series to have it's newest element to start with a 0.0 value, and it would not require this additional step.

    Is my assumption incorrect, or is something wrong under the hood with the way NinjaTrader sets the default value of a series newest element ?

    Thank you.
    Last edited by balltrader; 04-14-2020, 05:38 PM.

    #2
    It's not obvious but indexes in a series that have not been set should not be accessed.
    https://ninjatrader.com/support/help...8/?seriest.htm
    It is possible that you may use a Series<T> object but decide not to set a value for a specific bar. However, you should not try to access a Series<T>value that has not been set. Internally, a dummy value does exists, but you want to check to see if it was a valid value that you set before trying to access it for use in your calculations. Please see IsValidDataPoint() more information.

    I guess it's because an indicator should have the possibility to be invisible where it has no value. If it would be 0 for non value period you'd get a connected line to 0 on a line plot and auto scaling is disabled otherwise price action would almost be unreadable.
    Unfortunately this behavior also cost me quite a bit of time. Funny enough initializing with double.NaN crashes NT .
    Last edited by MojoJojo; 04-14-2020, 06:02 PM.

    Comment


      #3
      Thanks Mojo.

      Internally, a dummy value does exists
      Faceplam, "WELL THAT IS DUMB"

      But wait, if a dummy value exists, and a new series is created when you call AddPlot(), but if you don't explicitly set the elements of Values[0][0], your plot does not show a random set of values from the internal dummy values? So maybe there is another internal function that runs isvaliddatapoint() before each plot is rendered?

      Anyway, this is not my expected behavior, I understand your point that if it has a default value of 0 it would screw up data values which were unset, but if it has a dummy value, which is then incremented by my code using ++ then the elements value becomes the dummy value + 1. So somehow, NinjaTrader internally knows the dummy value is not real, and therefore NT does not plot the dummy value etc.


      NT Support, please create a feature request to the Development Team, requesting the internal dummy value of a series element be internally set to the default value of the series type, i.e. "double" default value = 0.0 , so if it is not explicitly set, but a increment ++ or decrement -- operation is applied to the initial dummy value, the counter starts from 0.

      Until then, I will explicitly set the valued to 0.0 in the OBU IsFirstTickOfBar conditional.

      Thank you.

      Comment


        #4
        You're right, setting the default value to 0.0 would be expected behavior and also that NT must check if it's a valid data point before plotting.
        Another solution would be to throw an exception to let the user know that an invalid value is accessed.

        Comment


          #5
          What is the programmatic pathway to get access to all series elements declared inside the current indicator (this) ?
          I'm hoping to avoid needing to type each series to set the value of [0] = 0.0

          instead loop through the collection of series,
          check if it is using data type == double,
          and possibly check it's name property to avoid a specific series,
          then set it's value of new element [0] = 0.0

          Comment


            #6
            Hello balltrader,

            MojoJojo had provided the correct information in post #2, what you are seeing is expected. You need to check if the datapoint is valid before trying to use it if you did not set a value. This allows for complex scenarios with series which can be combined with the Reset() method. A default value is not expected to be applied unless you specifically want it to be applied, otherwise just a placeholder is set and IsValidDataPoint is false.



            If you are expecting a 0 starting value specifically that is something you would need to set when you run OnBarUpdate to configure the series as you need it.


            NT Support, please create a feature request to the Development Team, requesting the internal dummy value of a series element be internally set to the default value of the series type, i.e. "double" default value = 0.0 , so if it is not explicitly set, but a increment ++ or decrement -- operation is applied to the initial dummy value, the counter starts from 0.
            You can do that already, you could call Reset which resets to the default value. Alternatively a better way is to just use the plot/series directly and set a value of 0 as that is what you are expecting in your calculations for every bar.

            What is the programmatic pathway to get access to all series elements declared inside the current indicator (this) ?
            You can use the Values collection for any Plots, Series are not held in a collection so if you are using Series<T> specifically you would need to hold references to your variables and use those.


            I look forward to being of further assistance.

            Comment


              #7
              can you explain why a placeholder is set if it is not a valid data point?
              why have a placeholder dummy value at all?


              the series exist in memory, is there any way to access them so I can loop thru them all? NinjaTrader.Data.Series ???


              Comment


                #8
                Hello balltrader,

                can you explain why a placeholder is set if it is not a valid data point?
                These questions are asking for more of the internal mechanics behind how Series works but I won't be able to answer those type of questions. This is how development chose to implement this system.
                Setting a datapoint causes IsValidDataPoint to become true, I would suspect that the placeholder has something to do with that part of the series logic.

                why have a placeholder dummy value at all?
                A placeholder is needed because because that is how it works internally, we are asking about internal logic and I would not be able to provide the reasoning development made this system use placeholders in this way.

                the series exist in memory, is there any way to access them so I can loop thru them all? NinjaTrader.Data.Series ???
                You would need to store instances to variables for your Series, these are held in your scripts memory and not some collection like the Values collection which holds all Plots. Plots specifically use a method "AddPlot" which adds them to a collection in the base class. When you make a random series in your script you would have to collect that yourself if you want to know it exists. You could make your own list of series using C# objects.

                I look forward to being of further assistance.



                Comment

                Latest Posts

                Collapse

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