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

Indicator Error on calling 'OnBarUpdate' : Index was outside the bounds of the array.

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

    Indicator Error on calling 'OnBarUpdate' : Index was outside the bounds of the array.

    I am currently struggling to understand BarCount, CurrentBars and a general strategy to understand when a my data is safe to read / write. Can someone help me out? I have tried adding sanity checks as described in the docs / here but I am still running into errors.

    > Indicator 'SpikeVolume': Error on calling 'OnBarUpdate' method on bar 5: Index was outside the bounds of the array.


    PHP Code:
    using NinjaTrader.NinjaScript.Indicators.Nic;
    using System.Windows.Media;
    using Nic.Utilities;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel;
    using System.Xml.Serialization;
    
    namespace NinjaTrader.NinjaScript.Indicators.Nic
    {
        public class SpikeVolume : Indicator
        {
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Order = 0)]
            public int Period { get; set; }
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Order = 1)]
            public int MinPeriod { get; set; }
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Order = 2)]
            public double SigmaThreshold { get; set; }
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Order = 3)]
            public double SigmaThreshold2 { get; set; }
    
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Volume1 { get { return Values[0]; } }
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Volume2 { get { return Values[1]; } }
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Volume3 { get { return Values[2]; } }
    
            private StdDevCalc _stdDevEth;
            private StdDevCalc _stdDevRth;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Nic Spike Volume";
                    Name = "Nic Spike Volume";
                    Calculate = Calculate.OnEachTick;
                    BarsRequiredToPlot = 0;
                    IsAutoScale = false;
                    IsOverlay = false;
                    DrawOnPricePanel = false;
    
                    Period = 50;
                    MinPeriod = 10;
                    SigmaThreshold = 2;
                    SigmaThreshold2 = 3;
                }
                else if(State == State.SetDefaults)
                {
                    AddPlot(Brushes.DarkGray, "Volume");
                    AddPlot(Brushes.LightYellow, "Volume Sigma 1");
                    AddPlot(Brushes.Yellow, "Volume Sigma 2");
                }
                else if (State == State.Configure)
                {
    
                }
                else if (State == State.DataLoaded)
                {
                    _stdDevEth = new StdDevCalc(Period);
                    _stdDevRth = new StdDevCalc(Period);
                }
            }
    
            protected override void OnBarUpdate()
            {
                base.OnBarUpdate();
    
                 // Attempted Fix Here !
                if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5 || CurrentBars[3] < 5) return;
                if (CurrentBar < 2) return;
    
                var stdDev = this.IsRTH()  ? _stdDevRth : _stdDevEth;
    
                if (IsFirstTickOfBar)
                {
                    stdDev.Update(Volume[1]);
                }
    
                var volume = Volume[0];
    
                if (stdDev.Count() < MinPeriod)
                {
                    // FAILS HERE !
                    Volume1[0] = volume;
                    return;
                }
    
                var sigma = stdDev.Average();
    
                if (volume >= sigma * SigmaThreshold2)
                {
                    Volume3[0] = volume;
                }
                else if (volume >= sigma * SigmaThreshold)
                {
                    Volume2[0] = volume;
                }
                else
                {
                    Volume1[0] = volume;
                }
            }
        }
    }&#8203; 
    

    #2
    User error. My mistake was here using `else if(State == State.SetDefaults)​` twice.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by crewtrades, 06-13-2024, 10:50 AM
    4 responses
    30 views
    0 likes
    Last Post crewtrades  
    Started by kingsteven, Today, 08:34 PM
    1 response
    3 views
    0 likes
    Last Post bltdavid  
    Started by biegea, Today, 08:54 PM
    0 responses
    3 views
    0 likes
    Last Post biegea
    by biegea
     
    Started by rene69851, Today, 08:36 PM
    0 responses
    2 views
    0 likes
    Last Post rene69851  
    Started by chartish, 04-11-2019, 12:40 PM
    15 responses
    3,832 views
    0 likes
    Last Post joselube001  
    Working...
    X