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

IsValidDataPoint - best practices

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

    IsValidDataPoint - best practices

    Hello,

    I'm calling indicators into another indicator. Some are built-in, some are custom.

    in the top end, I have the following:

    Code:
    //variables
    private SMA sma;
    Code:
    protected override void OnStateChange()
    		{...other stuff
    			if (State == State.SetDefaults)
    			{... other stuff
    }
    
    else if (State == State.DataLoaded)
    			{
    sma = SMA(20);
    }
    }
    Is it wise or preferred practice to use "IsValidDataPoint " to ensure the new indicator receives the data correctly? Like this:

    Code:
    //OnBarUpdate section...
    double smaValue = (SMA(20).Value.IsValidDataPoint(0)) ? SMA(20).Value[0] : 0;//Input[0];
    //similar for custom indicators...
    The reason I'm asking is I'm calling the calling indicator into a strategy. Sometimes, the error messages refer to a custom indicator name as if there's a problem...


    Code:
    //error messages from log:
    //Strategy
    Strategy 'kzCongestion100newstart': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.
    //Indicator called into strategy
    Indicator 'kzChartGridv04': Error on calling 'OnBarUpdate' method on bar 29: Object reference not set to an instance of an object.
    //sub Indicator called into Indicator
    Indicator 'ConnorsRSIv01': Error on calling 'OnBarUpdate' method on bar 31: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    Thanks for any help or direction you can provide.

    #2
    Hello zeller4,

    Thanks for your post.

    In most cases, the best practice would be to ensure that the indicator plots some meaningful value for each new data point of the primary data series.

    For indicators where that is not the case, you could use IsValidDataPoint() or IsValidDataPointAt() to see if there is a valid value at that index location. I'll include documentation links to these resources below for the thread's reference.

    IsValidDataPoint() - https://ninjatrader.com/support/help...ddatapoint.htm
    IsValidDataPointAt() - https://ninjatrader.com/support/help...atapointat.htm

    Also related and worth mentioning, these methods should be used if an indicator calls Reset() on the plot's Series.

    Reset() - https://ninjatrader.com/support/help...n-us/reset.htm

    Pleas let us know if there is anything else we can do to help.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks for the links (I've already studied them).
      plots some meaningful value
      You say this but didn't explain how to get a meaningful value...

      Will this solve the error messages I posted?



      Regarding, Reset() can you please provide a way to do that if the IsValid...returns false?

      ie (is this correct?):
      Code:
      if(MyPlot.IsValidDataPoint(0))...
      
      else//false
      MyPlot.Reset();

      Comment


        #4
        Hello zeller4,

        By meaningful value, I just mean assigning a value to the indicator plot for every iteration on the primary data series. For example, if you assigned plot values on another BarsInProgress interation, it would not be guaranteed that the Plot's Series will always have a value since the value would be assigned on a different time frame.

        In that case, setting a temporary variable to be assigned on the primary data series (BarsInProgress == 0) would be recommended so that the plot will have a meaningful value for every data point.

        As for Reset(), This would be used by the plotting indicator to reset a plot so it does not have a value for that data point. If the plotting indicator calls Reset() instead of assigning a 0 or some value that can be interpreted by the hosting NinjaScript, IsValidDataPoint would be recommended to use in the hosting NinjaScript since the plotting indicator would not guarantee valid data points at all indexes.

        So for your example, Reset() would be used in the indicator, and then IsValidDataPoint would be used to protect the hosting NinjaScript from errors from invalid data points.

        Code:
        protected override void OnBarUpdate()
        {
        	// Set Plot value
        	Values[0][0] = Close[0];           
        
        	//reset MyPlot every 10 bars
        	if(CurrentBar % 10 == 0)
        		Values[0].Reset();   
        
        	// Only use Values[0][0] if it has a valid data point and is not reset. This ensures that the second plot does not have a value assigned since the plot it is based on has been reset
        	if(Values[0].IsValidDataPoint(0))
        		Values[1][0] = Values[0][0] + Close[0]; 
        }
        The errors you are hitting I would approach differently than using IsValidDataPoint. You already know which scripts the errors reside as well as which method the errors are occurring. I would suggest adding debugging prints to identify the lines of code that are throwing these errors. Once identified, I would suggest treating the "Object reference not set to an instance of an object." errors with null checks and I would recommend treating the "You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart." errors with checks for enough bars or by making sure you are not referencing indexes for arrays that do not exist.

        Please let us know if you we can be of further assistance.
        Last edited by NinjaTrader_Jim; 09-21-2018, 09:41 AM.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by nandhumca, Today, 03:41 PM
        0 responses
        4 views
        0 likes
        Last Post nandhumca  
        Started by The_Sec, Today, 03:37 PM
        0 responses
        3 views
        0 likes
        Last Post The_Sec
        by The_Sec
         
        Started by GwFutures1988, Today, 02:48 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by ScottWalsh, 04-16-2024, 04:29 PM
        6 responses
        33 views
        0 likes
        Last Post ScottWalsh  
        Started by frankthearm, Today, 09:08 AM
        10 responses
        36 views
        0 likes
        Last Post frankthearm  
        Working...
        X