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

bar index needs to be greater than 0

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

    bar index needs to be greater than 0

    I am just starting on this indicator. I am familar with mql4, but doing my first indicator in NT7. So I am learnig a few of the new things about ninjascript. so I apprecaite the guidance.

    I have the below code that works well until I get to the last line, where I am trying to use the Time. I then get the error saying that "at bar 30, the bar index needs to be greater than 0"

    What am I doing wrong? Is it wrong how I am applying the Time[] in the last line? The only thing I can figure is I am doing that wrongly. I already have filters in the beginning to make sure there are enough bars, etc.

    Thanks for your assistance.

    Code:
       public class JSMissedPivotsv1 : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int pivotValidFor = 6; // Default setting for PivotValidFor
    		    private int NumMPValidfor = 7;
    			private int cnt = 0, TF=60, HrlineCnt=1, i = -1, ThisHour05, NextHour05, HrBeginShift, HrEndShift, TotalBars;
    			private double p;
    			private bool ArrayFull = false, PrevPivsBuffed = false, phit = false, DrawMissedPivotsOnly = true;
    			private DateTime PrevHourTime, BarTime; 
    			
    		    private double[]HourMissedPivot;
    		    private DateTimeSeries[]HourMPTimeOpen;
    			private DateTimeSeries[]HourMPTimeClosed;
    		
            // 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()
            {
    			Overlay				= 	true;
    			CalculateOnBarClose	=	true;
    			
    			//Add(PeriodType.Minute, 60);
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "MissedPivot1"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Peru), PlotStyle.Line, "MissedPivot2"));
    			
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			if (Bars.Count == 0) return;
    			if (CurrentBar < (60/Bars.Period.Value) || CurrentBar > (Bars.Count-(60/Bars.Period.Value))) return;
    			
    			i = Bars.Count - CurrentBar;
    			
    			Print(Bars.Count+ " " + i + " " + CurrentBar);  //At this point they are the same numbers //up until this point everything Print's correctly.
    			Print(Time[i] + " " + Time[CurrentBar]);//this is where the error comes

    #2
    Hello stearno,

    Thank you for your post.

    CurrentBar is an int representing the current bar you are processing on and will be a number representing the bar count being processed. This number is going to increase as you go forward, if you try to call it for Time[CurrentBar] and the you are using BarsRequired = 20 (this is the default) you will see such an error. Try setting BarsRequired to 0 in your Initialize() method: http://www.ninjatrader.com/support/h...srequired2.htm

    Comment


      #3
      The bar processing logic of NinjaTrader is completely different from MetaTrader.

      Please throw everything away that you have doen with MetaTrader and do not apply it here.

      The best way to understand the bar processing logic is to edit a few NinjaTrader indicators and modify them.

      Comment


        #4
        @Patrick,
        Okay, I added BarsRequired = 0 into the intialize method. It gives same error.

        I also tried BarsRequired = 30 and hide the first two lines that checks for CurrentBar starting at bar 30. But still same error.

        I appreciate more advice on what I should fix.

        @Harry,
        Thanks for that insight. You are completely correct. I realized two differences rather quickly yesterday. I realized CurrentBar was counting from 0 from the left side and that OnBarUpdate was going through every bar in the chart. MT4 you start on right side and you have to force it to calculate the number of bars you want. So I appreciate your advice.

        -Stearno

        Comment


          #5
          stearno, the last bit should only work

          if (CurrentBar > i)

          as otherwise you try accessing an index that just won't exist on your chart (think about going from left to right). You see this as you access a too high i too early, meaning you attempt to reference over the left start chart side boundary basically.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Okay, I finally got it to work. Thanks.

            -stearno

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,607 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            9 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            19 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            6 views
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            15 views
            0 likes
            Last Post Javierw.ok  
            Working...
            X