Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple instrument out of range

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

    Multiple instrument out of range

    Suppose I make an indicator where I simply plot the close of a second instrument as an indicator. This is of course easy to do with:

    Add( <instrument>, PeriodType.Day, 1 );

    and

    protected override void OnBarUpdate()
    {
    plot0.Set(Closes[1][0]);
    }

    This works fine until I select a range on my chart where the primary instrument doesn't have any data. For example, if I have data for MSFT from 1/1/1990 and IBM from 1/1/1980. I use MSFT as my primary instrument and I plot IBM as an indicator using the method above. As long as I don't choose an earlier startdate than 1/1/1990 for my chart there is no problem. However if I choose for example 1/1/1989 as startdate I get an error message: "You are accessing an index with a value that is invalid since its out of range".

    If I have only the primary instrument on my chart without the indicator then there is no problem. Even if I select the chart to start on 1/1/1989, it will simply start from 1/1/1990 because it has no earlier data. However when using the indicator based on a second instrument it gives an error. It apparently gets an invalid index.

    How can I prevent this from happening in my code? I want to make it robust, so maybe I need to somehow check if the primary series exists before doing plot0.Set(Closes[1][0])? How can I do that?

    #2
    FireFly, please try including a CurrentBars check for all involved series - for example :

    if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
    return;

    Comment


      #3
      @NinjaTrader_Bertrand: that doesn't solve the problem.

      BUT it seems I have already solved the problem by simply using

      if (BarsInProgress == 0)
      {

      }

      How stupid that I didn't think about that earlier!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      42 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      25 views
      0 likes
      Last Post PaulMohn  
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      162 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      98 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      157 views
      2 likes
      Last Post CaptainJack  
      Working...
      X