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 GussJ, 03-04-2020, 03:11 PM
    11 responses
    3,229 views
    0 likes
    Last Post xiinteractive  
    Started by andrewtrades, Today, 04:57 PM
    1 response
    14 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by chbruno, Today, 04:10 PM
    0 responses
    7 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Started by josh18955, 03-25-2023, 11:16 AM
    6 responses
    441 views
    0 likes
    Last Post Delerium  
    Started by FAQtrader, Today, 03:35 PM
    0 responses
    12 views
    0 likes
    Last Post FAQtrader  
    Working...
    X