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.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
When to use negative index
Collapse
X
-
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.
Tags: None
-
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
-
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
-
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.Originally posted by habibalex View PostI 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
Comment
-
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.Originally posted by habibalex View Posti = 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?
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
-
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
-
OK. My mistake.Originally posted by habibalex View Postkognam 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
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
-
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.
Comment
-
Hello fx.practic,Originally posted by fx.practic View PostOnce 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.
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 Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
574 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
333 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
553 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
551 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment