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

BarsArray check inside OnRender

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

    BarsArray check inside OnRender

    I'm iterating through the primary bars object and I would like to perform some calculations between the primary bars and an additional instrument I added using:
    Code:
    AddDataSeries("XLK", BarsPeriodType.Minute,1);
    How can I check that the secondary BarsArray[1] actually exists at the index before doing something with it? If I don't check I get this error:

    Code:
    Indicator 'CustomRender3': Error on calling 'OnRender' method on bar 1632: Object reference not set to an instance of an object.
    Here is the code:
    Code:
      protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
       if (ChartBars.Bars == null || Bars.Instrument == null || CurrentBar < 1) {
        return;
       }
       base.OnRender(chartControl, chartScale);
       for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex;idx++){ 
    	   double		    spy					= BarsArray[0].GetHigh(idx);
    	   double		    xlk					= BarsArray[1].GetClose(idx);
    	  
    	   //Printing a meaningless calculation for demonstration;   
    	   Print(xlk + spy);    
       }
      }
    Here is what I am trying to do:

    Code:
      protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
       if (ChartBars.Bars == null || Bars.Instrument == null || CurrentBar < 1) {
        return;
       }
       base.OnRender(chartControl, chartScale);
       for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex;){ 
    	   double		    spy					= BarsArray[0].GetHigh(idx);
    	   
               if(BarsArray[1] == null){
                 //don't advance the counter. Just return and wait for BarsArray[1] to be an object
                return;
               }else{
                 double		    xlk					= BarsArray[1].GetClose(idx);
    	     Print(xlk + spy); 
                 idx++;
    
               }
       
       }
      }
    I think the error happens because the primary bars object is available slightly before the secondary object is actually available for me to call. I just don't know what the right way would be to check for it.

    #2
    Hello swcooke,

    Generally, you would do as you already are, or check if the object is null. Are you certain BarsArray[1] is what is null?

    Have you checked all of the objects you use that can be null to see what specifically is in control of the error?

    If the primary is null, you would also need to check if that is null in addition to the second series.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by aa731, Today, 02:54 AM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by BarzTrading, Today, 07:25 AM
    0 responses
    2 views
    0 likes
    Last Post BarzTrading  
    Started by i019945nj, 12-14-2023, 06:41 AM
    5 responses
    65 views
    0 likes
    Last Post i019945nj  
    Started by ruudawakening, Today, 12:58 AM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by thread, Yesterday, 11:58 PM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X