Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Data series dummy value for non valid data points is confusing [FYI]

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

    Data series dummy value for non valid data points is confusing [FYI]

    Hi,

    during coding of an indicator I've noticed that a data series will return the value of the last bar where data has been set even if it was long ago, if there is no data at this index of the data series.
    Later I found out that I should have checked isValidDataPointAt
    https://ninjatrader.com/support/help...atapointat.htm

    Just voicing my opinion that it would be much more intuitive to return null or 0 instead of repeating the previous value.

    Here is an example without isValidDataPointAt, relevant line is in OnBarUpdate:

    Code:
        public class VolumeThreshold : Indicator
        {
            private MIN minVol;
            private MAX maxVol;
            private Series<int> signalSeries;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Volume Threshold";
                    Name                                        = "VolumeThreshold";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = false;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = false;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    BarsRequiredToPlot        = 2;
                    AddPlot(new Stroke(Brushes.Yellow, 2), PlotStyle.Dot, "VolHighPlot");
                    AddPlot(new Stroke(Brushes.Cyan, 2), PlotStyle.Dot, "VolLowPlot");
                }
                else if (State == State.Configure)
                {
                }
                else if(State == State.DataLoaded)
                {
                    minVol = MIN(Volume, 20);
                    maxVol = MAX(Volume, 20);
                    signalSeries = new Series<int>(this, MaximumBarsLookBack.TwoHundredFiftySix);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if(CurrentBar < BarsRequiredToPlog) {
                    return;
                }
                signalSeries[0] = 0; //initializing, otherwise keeps repeating old value
    
    
                double thresholdMax = maxVol[0] * 0.8;
                double thresholdMin = minVol[0] * 1.2;
    
                if(Volume[0] >= thresholdMax) {
                    signalSeries[0] = 1;
                }
    
                if(Volume[0] <= thresholdMin) {
                    signalSeries[0] = -1;
                }
                if(signalSeries[0] == 1 && signalSeries[1] == 1) {
                    Values[0][0] = High[0] + 5 * TickSize;
                }
    
                if(signalSeries[0] == -1 && signalSeries[1] == -1) {
                    Values[1][0] = Low[0] - 5 * TickSize;
                }
            }
        }
    Last edited by MojoJojo; 03-27-2020, 11:59 AM.

    #2
    Hello MojoJojo,

    Thank you for the post.

    I can see how that could be confusing, this is only lightly touched on in the help guide. In the series documentation there is a small note about the placeholder value you see:



    Checking for Valid Values
    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.
    Setting 0 as you have would be the best option here to make sure something is plotted for each bar as a default. That needs to happen if you don't want to use IsValidDataPoint or want to make something that does not consistently plot a value on every bar such as a signal.


    I look forward to being of further assistance.

    Comment


      #3
      Hi Jesse,

      thanks for the information and your advice. The documentation is good when it comes accessing non valid data points and initializing values is easy enough.

      Have a good weekend.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      639 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      366 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      107 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      569 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      572 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X