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

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.
    JesseNinjaTrader Customer Service

    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 haas88, 03-21-2024, 02:22 AM
      18 responses
      207 views
      0 likes
      Last Post haas88
      by haas88
       
      Started by Board game geek, Today, 02:20 AM
      0 responses
      1 view
      0 likes
      Last Post Board game geek  
      Started by knighty6508, Today, 01:20 AM
      2 responses
      12 views
      0 likes
      Last Post knighty6508  
      Started by franatas, Today, 01:53 AM
      0 responses
      2 views
      0 likes
      Last Post franatas  
      Started by knighty6508, Today, 01:17 AM
      0 responses
      9 views
      0 likes
      Last Post knighty6508  
      Working...
      X