Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Min?

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

    Min?

    I keep getting an error message with this line on some charts. Any idea why?

    double minLow = MIN(Low,5)[1];
    Print(minLow);

    #2
    Hello Hamrall,

    What is the error message you are getting?

    (Is this a index error? I ask because you are calling [1] one bar ago. If this is called on the first bar there won't be a bar one bar ago and this will result in an index error)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes it's an index error. Im confused as to why it works on almost all of the charts but not on a few? Isn't 1 bar ago the second to last bar?

      Comment


        #4
        Hello Hamrall,

        You are correct, 1 bar ago is the second to last bar.

        When this starts it will start with the first bar of historical data.

        The first bar will also be the last bar.

        If the last bar is the only bar, what bar is 1 bar ago?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          How do I get it to look at bars 5-1?

          Comment


            #6
            Hello Hamrall,

            I do not understand your inquiry.

            On each bar this script will look one bar previous.

            When you mention "How do I get it to look at bars 5-1?" are you asking how to get this to work backward from 5 to 1?

            To get this to work backwards you would need to wait until bar 5 and then run a loop from bars 5 to 1.

            For example:

            if (CurrentBar == 5)
            {
            for (int i=0; i>0; i--)
            {
            double minLow = MIN(Low,5)[1];
            Print(minLow);
            }
            }

            Are you also asking how to only process on bars 1 through 5?

            On bar 1 you are still going to have an issue because there is no bar 1 bar ago on the first bar.


            To prevent your script from hitting this error you need to make sure you are not calling a bar that does not exist.

            If you are calling one bar ago, ensure at least two bars are on the chart.

            if (CurrentBar < 2)
            return;
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I guess what I'm not understanding is how it counts.
              If there are 100 bars on the chart then bar 1 is the bar on the far left or far right?
              When I use the "swing" bar 100 is the far right bar. When I use current bar[0] that's the farthest right bar.

              Comment


                #8
                Hello Hamrall,

                The script has to process for every bar before you get to the last bar starting with bar 0.

                The first bar is bar 0.

                CurrentBar == 0

                However you I think you are confusing the bar number with BarsAgo. Bars ago is how many bars previous to the current bar. 0 bars ago is the current bar. This is not the first bar.

                The first bar is on the far left of the chart. The last bar is on the far right of the chart. New bars will appear on the right side of the chart.

                [0] refers to bars ago not a specific bar. With bars ago you are subtracting from the current bar.

                If the current bar is bar 100, then bars ago [1] is bar 99. Bars ago [0] is bar 100.

                The first bar would be bars ago [100].
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Ok so if I use MIN(Low,5)[10] this would go back 10 bars and check for the lowest low of bars 9,8,7,6,5?

                  Comment


                    #10
                    Hello Hamrall,

                    It depends on what bar this is processing on.

                    If OnBarUpdate() is processing on bar 19 then this will find the lowest low in bars 5 - 9.
                    (10 bars ago)

                    If OnBarUpdate() is processing on Bar 100 then this will find the lowest low in bars 85 - 90.

                    If this is processing on the first bar, then this is going to cause and error and stop the script because there are no bars 10 bars ago when the script first starts.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Ok thank you for explaining this to me. I have another question now. If I were to check for a condition and that condition was true is there a way to capture the data and stop checking for that condition once its met?
                      For example if the low of a bar breaks the low of bar[20] then condition is met even if the bar that broke the low goes back above the bar[20] and the condition is no longer met.

                      Comment


                        #12
                        Hi Hamrall,

                        Use a bool for this.

                        Once the condition is found set bool to true.

                        When you feel it can be reset set it to false.

                        For example:

                        In #region Variables:
                        private bool conditionFound = false;

                        In OnBarUpdate:
                        if (conditionFound == true /* && condition to reset the bool such as a new session here */)
                        {
                        conditionFound = false;
                        }

                        if (conditionFound == false /* && conditions you want to trigger this being true here */)
                        {
                        conditionFound = true;
                        }
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Ok so once the condition is met the bool will stay true even if the condition goes back to being false? Is there a way to get the info of the bar that changed the bool?

                          Comment


                            #14
                            Hello Hamrall,

                            The variable needs to be declared in #region Variables so it is within the class scope and not within the OnBarUpdate scope, this is so it will not reset on every bar and will only change when its set.

                            Only set this when you want to change the value.

                            When the conditions are true to reset it change it to false. When the conditions are true to change it to true, then change it to true.


                            If you want the bar number of the bar that triggered the bool change, then set a varaible to that bar number.


                            In #region Variables:
                            private bool conditionFound = false;
                            private barNumber = 0;

                            In OnBarUpdate:
                            if (conditionFound == true /* && condition to reset the bool such as a new session here */)
                            {
                            conditionFound = false;
                            }

                            if (conditionFound == false /* && conditions you want to trigger this being true here */)
                            {
                            conditionFound = true;
                            barNumber = CurrentBar;
                            }
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

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