Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator creating Secondary series with BarsToLoad isn't processing historical data

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

    Indicator creating Secondary series with BarsToLoad isn't processing historical data

    I am creating an indicator which adds a secondary data series that has a daily period and a different # of bars to load than the primary data series. I create an indicator using the secondary data series, but that indicator is not getting OnBarUpdate for the historical data of the secondary data series.

    Consider the following code.

    Code:
    class SomeIndicator: Indicator
    {
        protected override void OnStateChange()
        {
            switch(State)
            {
                case State.SetDefaults:
                    //...
                    break;
                case State.Configure:
                    {
                        var barsPeriod = new BarsPeriod() { BarsPeriodType = BarsPeriodType.Day, Value = 1, MarketDataType = MarketDataType.Last };
                        AddDataSeries(Instrument.FullName, barsPeriod, 30, Instrument.MasterInstrument.TradingHours.Name, null);
    
                        sma = SMA(BarsArray[1], 14);
                    }
                    break;
            }
        }
    
        protected override void OnBarUpdate()
        {
            if(BarsInProgress == 0)
            {
                Print("Bar " + CurrentBars[0]);
    
                if(CurrentBars[1] > sma.Period)
                {
                    for(int i = 0; i < CurrentBars[1]; i++)
                    {
                        // Cannot execute this code because despite CurrentBars[1] being 27, sma[0] is not valid (let alone any additional values)
                        //Print("SMA " + sma[i]);
                    }
                }
            }
            else if(BarsInProgress == 1)
            {
                Print("Daily Bar " + CurrentBars[1]);
            }
        }
    
        SMA sma;
    }
    If I apply SomeIndicator to a 1 minute chart that loads 2 days of historical data I should see the following I do see bars printed for both series with the Daily Bar going up to 28. However I am unable to access the values for sma. If I debug OnBarUpdate of SMA I see that it gets called for the primary data series, but not daily data for the series that it is fed.

    How do I get sma to be populated with the 27 days of data that its data series has?

    #2
    Hello ntbone,

    Thanks for your post.

    From the help guide: "When adding multiple Data Series of the same instrument and the same Bar Type, the 'barsToLoad' property will only be effective on the first added series. Subsequent series with a different barsToLoad setting will not load a different number of bars then the first series." Reference: https://ninjatrader.com/support/help...dataseries.htm

    So you would need in this case the chart bars days to load to be sufficient for your daily bars.

    Comment


      #3
      I read that in the help guide. It says the "first added series". In my case the "first added series" is the daily bars with 30 days, so if I add another series I won't get more then 30 days.

      Further, if you look at the output of the print statements, all 30 bars do get processed in OnBarUpdate from the daily bar series. The problem is that the sma indicator itself has OnBarUpdate called for the primary data series, and not the Secondary Data series, even though the Secondary Data series is what is fed to it.

      Comment


        #4
        Hello ntbone,

        Thanks for your reply.

        Please instantiate the SMA in State.Data.Loaded (or State.Historical).

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        596 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        343 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        556 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        554 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X