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

Why can't I access my own Series<double>

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

    Why can't I access my own Series<double>

    I have a simple indicator which uses custom Series which simply duplicates the OHLC, but when I try to access this, I get the ''array out of range'' issue. however when i replace my series by the official OHLC, then I don't get any error.

    So
    1/ why does it do this ?
    2/ how do remove this issue in order to access my series just liek the regular OHLC ?


    here is the error:

    Print_0000000=14473 0 14473 14473
    Print_1=14473 0 14473 14473
    Print_2=14473 0 3936,5 3934,75 3936,5 3934,75
    Indicator 'MyCustomIndicator': Error on calling 'OnBarUpdate' method on bar 0: 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.


    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class MyCustomIndicator : Indicator
        {
            private Series<double> MY_CLOSE;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "MyCustomIndicator";
                    Calculate                                    = Calculate.OnEachTick;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    AddPlot(Brushes.Orange, "MY_PLOT");
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {                
                    MY_CLOSE = new Series<double>(this, MaximumBarsLookBack.Infinite);
                }
            }
    
            protected override void OnBarUpdate()
            {
                //Add your custom indicator logic here.
                MY_FILLING(MY_CLOSE);
                MY_FUNCTION(MY_CLOSE);
            }
    
            private void MY_FILLING(Series<double> MY_SERIES)
            {
                MY_SERIES[0]=Close[0];
            }
    
            private void MY_FUNCTION(Series<double> MY_SERIES)
            {
                if(CurrentBar<1)
                {
                    Print("Print_0000000="+Bars.Count+" "+CurrentBar+" "+Close.Count+" "+MY_SERIES.Count);
                };
      Print("Print_2="+Bars.Count+" "+CurrentBar+" "+Close[CurrentBar+1]+"  "+Close[CurrentBar]+" "+Close[0+1]+"  "+Close[0]);
      Print("Print_3="+Bars.Count+" "+CurrentBar+" "+Close[CurrentBar+1]+"  "+Close[CurrentBar]+" "+Close[0+1]+"  "+Close[0]+" "+MY_SERIES[CurrentBar+1]+"  "+MY_SERIES[CurrentBar]+" "+MY_SERIES[0+1]+"  "+MY_SERIES[0]);​
            }
    
            #region Properties
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> MY_PLOT
            {
                get { return Values[0]; }
            }
            #endregion
    
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private MyCustomIndicator[] cacheMyCustomIndicator;
            public MyCustomIndicator MyCustomIndicator()
            {
                return MyCustomIndicator(Input);
            }
    
            public MyCustomIndicator MyCustomIndicator(ISeries<double> input)
            {
                if (cacheMyCustomIndicator != null)
                    for (int idx = 0; idx < cacheMyCustomIndicator.Length; idx++)
                        if (cacheMyCustomIndicator[idx] != null &&  cacheMyCustomIndicator[idx].EqualsInput(input))
                            return cacheMyCustomIndicator[idx];
                return CacheIndicator<MyCustomIndicator>(new MyCustomIndicator(), input, ref cacheMyCustomIndicator);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.MyCustomIndicator MyCustomIndicator()
            {
                return indicator.MyCustomIndicator(Input);
            }
    
            public Indicators.MyCustomIndicator MyCustomIndicator(ISeries<double> input )
            {
                return indicator.MyCustomIndicator(input);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.MyCustomIndicator MyCustomIndicator()
            {
                return indicator.MyCustomIndicator(Input);
            }
    
            public Indicators.MyCustomIndicator MyCustomIndicator(ISeries<double> input )
            {
                return indicator.MyCustomIndicator(input);
            }
        }
    }
    
    #endregion
    ​

    PS: when I create a thread, I no longer have access to the toolbar to insert quotes, codes and so on. How do i get it back ?
    Last edited by alanlopen; 03-24-2023, 07:36 AM.

    #2
    Hello alanlopen,

    Thank you for your post.

    I suspect that the script you provided does not match the output you shared, as Print_1 does not appear in your script. The issue seems to be using CurrentBar as your barsAgo value. For example, if the current bar is bar 0, then trying to access either Close[CurrentBar+1] or MY_SERIES[CurrentBar+1] would result in trying to access a value from 1 bar ago. 1 bar ago does not exist at that point because you are on bar 0 of the chart. The index you are trying to access would be out of range of the series, resulting in the error message.

    I suggest reviewing the following pages in the help guide regarding using bars ago values to reference the correct bar as well as making sure you have enough bars in the series you are trying to access:As for the toolbar in your post editor, please click on the 'A' icon (in the top-right corner of the text box) shown in the screenshots below to toggle the advanced editor:
    Click to enable - https://www.screencast.com/t/zg2MKrO5290
    Enabled - https://www.screencast.com/t/GtFfnjVAeY

    Please let me know if I may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      yes everything is working now. I have no idea what went on.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Zeezee, Today, 12:45 PM
      0 responses
      4 views
      0 likes
      Last Post Zeezee
      by Zeezee
       
      Started by swjake, Today, 12:04 PM
      2 responses
      9 views
      0 likes
      Last Post swjake
      by swjake
       
      Started by Richozzy38, Yesterday, 01:06 PM
      5 responses
      24 views
      0 likes
      Last Post Richozzy38  
      Started by tradingnasdaqprueba, 05-07-2024, 03:42 AM
      13 responses
      51 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by bill2023, Yesterday, 08:21 AM
      3 responses
      18 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X