Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator crashes

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

    Indicator crashes

    Could someone help me with this my indicator keeps crashing and I don't know why. I load it onto a forex chart and it crashes. Thanks for advice.

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// Lookback period finder
        /// </summary>
        [Description("Lookback period finder")]
        public class PeriodFinder : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int stdevPeriod = 20; // Default setting for StdevPeriod
                private int minlen = 7; // Default setting for Minlen
                private int maxlen = 28; // Default setting for Maxlen
            // User defined variables (add any user defined variables below)
                
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                
                
                if (minlen > maxlen)
                {
                    Print("Minimum length must be less than maximum length.");
                }
                
                Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Period"));
                
                   CalculateOnBarClose = true;
                Overlay                = false;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                if (CurrentBar < Math.Max(stdevPeriod, maxlen))
                {
                    return;
                }
                
                DataSeries std_data = new DataSeries(StdDev(stdevPeriod));
                double v1 = StdDev(stdevPeriod)[0];
                double v2 = MAX(std_data, stdevPeriod)[0];
                double v3 = MIN(std_data, stdevPeriod)[0];
                double v4 = ((v1-v3)/(v2-v3));
                double currlen = minlen + (maxlen - minlen) * (1-v4);
                
                Period.Set(currlen);
            }
    
            #region Properties
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Period
            {
                get { return Values[0]; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int StdevPeriod
            {
                get { return stdevPeriod; }
                set { stdevPeriod = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int Minlen
            {
                get { return minlen; }
                set { minlen = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int Maxlen
            {
                get { return maxlen; }
                set { maxlen = Math.Max(1, value); }
            }
            #endregion
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        public partial class Indicator : IndicatorBase
        {
            private PeriodFinder[] cachePeriodFinder = null;
    
            private static PeriodFinder checkPeriodFinder = new PeriodFinder();
    
            /// <summary>
            /// Lookback period finder
            /// </summary>
            /// <returns></returns>
            public PeriodFinder PeriodFinder(int maxlen, int minlen, int stdevPeriod)
            {
                return PeriodFinder(Input, maxlen, minlen, stdevPeriod);
            }
    
            /// <summary>
            /// Lookback period finder
            /// </summary>
            /// <returns></returns>
            public PeriodFinder PeriodFinder(Data.IDataSeries input, int maxlen, int minlen, int stdevPeriod)
            {
                if (cachePeriodFinder != null)
                    for (int idx = 0; idx < cachePeriodFinder.Length; idx++)
                        if (cachePeriodFinder[idx].Maxlen == maxlen && cachePeriodFinder[idx].Minlen == minlen && cachePeriodFinder[idx].StdevPeriod == stdevPeriod && cachePeriodFinder[idx].EqualsInput(input))
                            return cachePeriodFinder[idx];
    
                lock (checkPeriodFinder)
                {
                    checkPeriodFinder.Maxlen = maxlen;
                    maxlen = checkPeriodFinder.Maxlen;
                    checkPeriodFinder.Minlen = minlen;
                    minlen = checkPeriodFinder.Minlen;
                    checkPeriodFinder.StdevPeriod = stdevPeriod;
                    stdevPeriod = checkPeriodFinder.StdevPeriod;
    
                    if (cachePeriodFinder != null)
                        for (int idx = 0; idx < cachePeriodFinder.Length; idx++)
                            if (cachePeriodFinder[idx].Maxlen == maxlen && cachePeriodFinder[idx].Minlen == minlen && cachePeriodFinder[idx].StdevPeriod == stdevPeriod && cachePeriodFinder[idx].EqualsInput(input))
                                return cachePeriodFinder[idx];
    
                    PeriodFinder indicator = new PeriodFinder();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    indicator.Maxlen = maxlen;
                    indicator.Minlen = minlen;
                    indicator.StdevPeriod = stdevPeriod;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    PeriodFinder[] tmp = new PeriodFinder[cachePeriodFinder == null ? 1 : cachePeriodFinder.Length + 1];
                    if (cachePeriodFinder != null)
                        cachePeriodFinder.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cachePeriodFinder = tmp;
                    return indicator;
                }
            }
        }
    }
    
    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
        public partial class Column : ColumnBase
        {
            /// <summary>
            /// Lookback period finder
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.PeriodFinder PeriodFinder(int maxlen, int minlen, int stdevPeriod)
            {
                return _indicator.PeriodFinder(Input, maxlen, minlen, stdevPeriod);
            }
    
            /// <summary>
            /// Lookback period finder
            /// </summary>
            /// <returns></returns>
            public Indicator.PeriodFinder PeriodFinder(Data.IDataSeries input, int maxlen, int minlen, int stdevPeriod)
            {
                return _indicator.PeriodFinder(input, maxlen, minlen, stdevPeriod);
            }
        }
    }
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        public partial class Strategy : StrategyBase
        {
            /// <summary>
            /// Lookback period finder
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.PeriodFinder PeriodFinder(int maxlen, int minlen, int stdevPeriod)
            {
                return _indicator.PeriodFinder(Input, maxlen, minlen, stdevPeriod);
            }
    
            /// <summary>
            /// Lookback period finder
            /// </summary>
            /// <returns></returns>
            public Indicator.PeriodFinder PeriodFinder(Data.IDataSeries input, int maxlen, int minlen, int stdevPeriod)
            {
                if (InInitialize && input == null)
                    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
    
                return _indicator.PeriodFinder(input, maxlen, minlen, stdevPeriod);
            }
        }
    }
    #endregion

    #2
    OnBarUpdate() is called with each incoming tick, and each time you try to create a new DataSeries.

    You should declare your DataSeries object in the region Variables and then initialize the DataSeries in the Initialize section(),
    Last edited by Harry; 03-05-2015, 09:41 AM.

    Comment


      #3
      Originally posted by Harry View Post
      OnBarUpdate() is called with each incoming tick, and each time you try to create a new DataSeries,

      You should declare your DataSeries object in the region Variables and then initialize the DataSeries in the Initialize section(),
      ok thank you.

      Comment

      Latest Posts

      Collapse

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