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

Data serie size

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

    Data serie size

    Hi, say my strategy has Maximum bars look back = 256 and Bars required to trade = 256, is there any reason why NinjaScript would spit out this error when I try to access Close[250] (or Open, Low, etc.)? Please note that I do this in DataLoaded state.
    Code:
    Error on calling 'OnStateChange' method: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    Is there anything special I need to do to access Close, Open etc. from ISeries?

    #2
    Hello amaltais,

    Thanks for your post.

    You can't make a BarsAgo reference in State.DataLoaded because the script has not begun to process bars yet. You could use literal bar indexes with Series.GetValueAt, though.

    For example:

    Code:
    else if (State == State.DataLoaded)
    {
        for(int i = 0; i < Bars.Count-1; i++)
            Print(Close.GetValueAt(i));
    }
    Let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      What is the difference between [ ] and getValueAt?

      Comment


        #4
        Hello amaltais,

        Brackets are used for BarsAgo references when GetValueAt is used for literal indexes.

        BarsAgo indexes go from the last bar in the data series to the first bar, while literal indexes go from the first bar to the last bar.

        You may open the Data Box on a chart, right click, then select "Show Bar Indexes" and Show BarsAgo indexes" to visualize.

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Ok I see, so with Maximum bars look back = 256, am I accessing the 256 bars ago with GetValueAt(0)?

          Comment


            #6
            Hello amaltais,

            Not exactly, MaximumBarsLookback turns each Series object into a circular ring buffer. If you set something up like the following, you will see matching prints.

            Code:
            public class MyCustomIndicator12 : Indicator
            {
                private Series<double> MySeries;
            
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description = @"Enter the description for your new custom Indicator here.";
                        Name = "MyCustomIndicator12";
                        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                    }
                    else if (State == State.Realtime)
                    {
                        Print(MySeries[0] + " " + MySeries.GetValueAt(CurrentBar-256));
                    }
                    else if (State == State.DataLoaded)
                    {
                        MySeries = new Series<double>(this);
                    }
                }
            
                protected override void OnBarUpdate()
                {
                    MySeries[0] = Close[0];
                }
            }
            Please see below for complete information.

            https://ninjatrader.com/support/help...rslookback.htm
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by zstheorist, Today, 07:52 PM
            0 responses
            5 views
            0 likes
            Last Post zstheorist  
            Started by pmachiraju, 11-01-2023, 04:46 AM
            8 responses
            150 views
            0 likes
            Last Post rehmans
            by rehmans
             
            Started by mattbsea, Today, 05:44 PM
            0 responses
            6 views
            0 likes
            Last Post mattbsea  
            Started by RideMe, 04-07-2024, 04:54 PM
            6 responses
            33 views
            0 likes
            Last Post RideMe
            by RideMe
             
            Started by tkaboris, Today, 05:13 PM
            0 responses
            6 views
            0 likes
            Last Post tkaboris  
            Working...
            X