Announcement

Collapse
No announcement yet.

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.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    81 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    68 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    55 views
    0 likes
    Last Post CarlTrading  
    Working...
    X