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

When to use negative index

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

    When to use negative index

    I have an indicator that keeps getting messed up because the barsAgo index keeps switching from negative to positive. When do I need a negative barsAgo index and when do I need a positive one? ie: Opens[0][1234] or Opens[0][-1234]

    I have a button on the chart that runs a function that loops through all the data on the chart runs some calculations and then refreshes the plot. This problem occurs on both the minute and tick charts for ES.

    Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.

    #2
    You almost never need to use a negative index with NinjaScript.

    If you want to ensure you are always using a positive use Math.Abs()

    How are you calculating startIndex? This is what 'i' is set to and getting your negative number

    Comment


      #3
      Hello,

      Calonio is correct. There are cases in which you would want to use a negative bar index, but it would be rare.

      It would probably be more clear to explain when you would not want to use it. You should not use a negative barsAgo index when you are referring to any collection, such as a list, array, or data in a DataSeries object. For any collection that contains data, there will not be any data at a negative index.

      However, if you are setting up a drawing object, or doing something other than referring to a collection, then using a negative bar index can work.

      In your case, since you are looping through data series, you will not want to use a negative barsAgo index. You might consider simply looping from 0 to CurrentBar. 0 would represent the current bar, and the number returned by CurrentBar should refer to the first bar in the series when used as a bar index (for example, if CurrentBar was 200, then passing CurrentBar in as a barsAgo index would give you 200 bars ago).

      Please let me know if I can assist further.
      Dave I.NinjaTrader Product Management

      Comment


        #4
        Did you see the picture I sent? I don't want to use a negative index, but when I use a positive one ninjatrader is throwing an error.

        Opens[0].Count = 3312
        i = 3291
        Opens[0][i] => throws an error
        Opens[0][-i] = 2100.5 => this is a real value...i don't understand why this object needs a negative index to return a value. Why does this occur?

        Comment


          #5
          Originally posted by habibalex View Post
          I have an indicator that keeps getting messed up because the barsAgo index keeps switching from negative to positive. When do I need a negative barsAgo index and when do I need a positive one? ie: Opens[0][1234] or Opens[0][-1234]

          I have a button on the chart that runs a function that loops through all the data on the chart runs some calculations and then refreshes the plot. This problem occurs on both the minute and tick charts for ES.

          http://screencast.com/t/ZdlulKQwxjXo
          If your index is going negative, then the problem is in your code that is calculating the index that you are passing. That is what you should be evaluating. For example, offhand, from what little that you have disclosed, I can see a startIndx that is being reduced in steps of 20, and so which, if uncontrolled, would eventually go negative. However, I could very well be wrong, as I am only seeing what little is in the picture and do not know how else you are massaging the data. It was just an example of the kind of thing that we, each of us, all too often, overlook.

          Comment


            #6
            i = 3921; //this is a positive number
            Opens[0][3291] => throws an error, why is a positive number that is less than the size of the series throwing an error?
            Opens[0][-3921] => this is equal to 2100.5, why is -3291 returning a value?

            Comment


              #7
              Originally posted by habibalex View Post
              i = 3921; //this is a positive number
              Opens[0][3291] => throws an error, why is a positive number that is less than the size of the series throwing an error?
              Opens[0][-3921] => this is equal to 2100.5, why is -3291 returning a value?
              That is the opposite of what you are showing. Your picture is clearly showing Opens[0][-i] returning a value. That would mean that the index "i" that you have calculated has a negative value, and that your code at some point is actually trying to input negative indices. It just so happens that a double negative takes care of that particular error in that particular instance.

              No matter how it occurs, if your code is trying to pass a negative calculated index, then your code needs to be corrected to never try to pass a negative index into a collection.

              Comment


                #8
                kognam please look at the picture again. http://screencast.com/t/ZdlulKQwxjXo

                The Watch tab clearly shows that i = 3291.
                3291 is a positive number, ie it is greater than 0. -1 * 3291 =-3291 which is a negative number.

                In the picture it clearly shows that
                i = 3291
                Opens[0][-i] = Opens[0][-1 * 3291] = Opens[0][-3291] => 2100.5

                Comment


                  #9
                  Originally posted by habibalex View Post
                  kognam please look at the picture again. http://screencast.com/t/ZdlulKQwxjXo

                  The Watch tab clearly shows that i = 3291.
                  3291 is a positive number, ie it is greater than 0. -1 * 3291 =-3291 which is a negative number.

                  In the picture it clearly shows that
                  i = 3291
                  Opens[0][-i] = Opens[0][-1 * 3291] = Opens[0][-3291] => 2100.5
                  OK. My mistake.

                  At this stage you probably are going to have to disclose the details of DayObject. That means that I will have to bow out, so that if you choose to do so, you can deal with NinjaTrader directly. Your code looks involved enough that I have to presume that you would not want to share it with an anonymous stranger on a forum.

                  Comment


                    #10
                    Once more question, please, about using negative index:

                    Can we use Open[-1], Close[-1], High[-1] and Low[-1] to access non-completed bar when CalculateOnBarClose = true?
                    (I need it to set correct StopLoss).

                    In Watch window of Visual Studio, on MarketReplay connection I can see correct OHLC values with [-1] index.

                    One only embarrassing me - debugger, also, show some prices with [-2] index too.
                    Last edited by fx.practic; 05-29-2017, 04:32 PM.
                    fx.practic
                    NinjaTrader Ecosystem Vendor - fx.practic

                    Comment


                      #11
                      I ended up using Bars.GetClose(Bars.Count-1) so I didn't have to deal with ninja's weird negative index's and stuff.

                      Comment


                        #12
                        Originally posted by fx.practic View Post
                        Once more question, please, about using negative index:

                        Can we use Open[-1], Close[-1], High[-1] and Low[-1] to access non-completed bar when CalculateOnBarClose = true?
                        (I need it to set correct StopLoss).

                        In Watch window of Visual Studio, on MarketReplay connection I can see correct OHLC values with [-1] index.

                        One only embarrassing me - debugger, also, show some prices with [-2] index too.
                        Hello fx.practic,

                        Thank you for your post.

                        You can use the negative index in your example.

                        Please let me know if you have any questions.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Irukandji, Yesterday, 02:53 AM
                        2 responses
                        17 views
                        0 likes
                        Last Post Irukandji  
                        Started by adeelshahzad, Today, 03:54 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post adeelshahzad  
                        Started by CortexZenUSA, Today, 12:53 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post CortexZenUSA  
                        Started by CortexZenUSA, Today, 12:46 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post CortexZenUSA  
                        Started by usazencortex, Today, 12:43 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post usazencortex  
                        Working...
                        X