Why does BarsBack throw the error? Thanks!
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!
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
NinjaTrader
Mode from a Series double
Collapse
X
-
Hello PaulMohn,
This is no longer the script I gave you and the error is not with the code I provided.
Use prints to find out why your custom logic is causing an error.
The error is an index error.
What is the size of the rng0Rng1 series at the time the error? (hint its the size of CurrentBar)
What is the invalid index that was used as a barsAgo value?Chelsea B.NinjaTrader Customer Service
Comment
-
Thanks Chelsea for the reply.
I modified post #60 1st snippet as below to reflect the correct BarsBack index.
PHP Code:double sum = 0; for(int i=0; i<BarsBack; i++) { sum += rng0rng1[i]; } double var3 = (sum / BarsBack); double var03 = Instrument.MasterInstrument.RoundTo TickSize(var3 / TickSize);
Building upon your print file example,
I tested printing with the following prints statements and it does print, as long as the Rng0Rng1 index is below the CurrentBar index
(in this case the CurrentBar index = 1038, and the prints max Rng0Rng1 = 21)
PHP Code:Print("CurrentBar : " + CurrentBar); Print(" Rng0Rng1[0] " + Rng0Rng1[0]); Print(" Rng0Rng1[1] " + Rng0Rng1[1]); Print(" Rng0Rng1[2] " + Rng0Rng1[2]); Print(" Rng0Rng1[3] " + Rng0Rng1[3]); Print(" Rng0Rng1[4] " + Rng0Rng1[4]); Print(" Rng0Rng1[5] " + Rng0Rng1[5]); Print(" Rng0Rng1[6] " + Rng0Rng1[6]); Print(" Rng0Rng1[7] " + Rng0Rng1[7]); Print(" Rng0Rng1[8] " + Rng0Rng1[8]); Print(" Rng0Rng1[9] " + Rng0Rng1[9]); Print(" Rng0Rng1[10] " + Rng0Rng1[10]); Print(" Rng0Rng1[11] " + Rng0Rng1[11]); Print(" Rng0Rng1[12] " + Rng0Rng1[12]); Print(" Rng0Rng1[13] " + Rng0Rng1[13]); Print(" Rng0Rng1[14] " + Rng0Rng1[14]); Print(" Rng0Rng1[15] " + Rng0Rng1[15]); Print(" Rng0Rng1[16] " + Rng0Rng1[16]); Print(" Rng0Rng1[17] " + Rng0Rng1[17]); Print(" Rng0Rng1[18] " + Rng0Rng1[18]); Print(" Rng0Rng1[19] " + Rng0Rng1[19]); Print(" Rng0Rng1[20] " + Rng0Rng1[20]); Print(" Rng0Rng1[21] " + Rng0Rng1[21]); //Print("\n Rng0Rng1[2000] " + Rng0Rng1[2000]); double sum = 0; for(int i=0; i<CurrentBar; i++) { sum += Rng0Rng1[i]; } double var3 = (sum / CurrentBar); double var03 = Instrument.MasterInstrument.RoundToTickSize(var3 / TickSize);
But when I try to print for a Rng0Rng1 index > the CurrentBar (1038), for example Rng0Rng1[2000]
with
PHP Code:Print("CurrentBar : " + CurrentBar); Print(" Rng0Rng1[0] " + Rng0Rng1[0]); Print(" Rng0Rng1[1] " + Rng0Rng1[1]); Print(" Rng0Rng1[2] " + Rng0Rng1[2]); Print(" Rng0Rng1[3] " + Rng0Rng1[3]); Print(" Rng0Rng1[4] " + Rng0Rng1[4]); Print(" Rng0Rng1[5] " + Rng0Rng1[5]); Print(" Rng0Rng1[6] " + Rng0Rng1[6]); Print(" Rng0Rng1[7] " + Rng0Rng1[7]); Print(" Rng0Rng1[8] " + Rng0Rng1[8]); Print(" Rng0Rng1[9] " + Rng0Rng1[9]); Print(" Rng0Rng1[10] " + Rng0Rng1[10]); Print(" Rng0Rng1[11] " + Rng0Rng1[11]); Print(" Rng0Rng1[12] " + Rng0Rng1[12]); Print(" Rng0Rng1[13] " + Rng0Rng1[13]); Print(" Rng0Rng1[14] " + Rng0Rng1[14]); Print(" Rng0Rng1[15] " + Rng0Rng1[15]); Print(" Rng0Rng1[16] " + Rng0Rng1[16]); Print(" Rng0Rng1[17] " + Rng0Rng1[17]); Print(" Rng0Rng1[18] " + Rng0Rng1[18]); Print(" Rng0Rng1[19] " + Rng0Rng1[19]); Print(" Rng0Rng1[20] " + Rng0Rng1[20]); Print(" Rng0Rng1[21] " + Rng0Rng1[21]); Print("\n Rng0Rng1[2000] " + Rng0Rng1[2000]); double sum = 0; for(int i=0; i<CurrentBar; i++) { sum += Rng0Rng1[i]; } double var3 = (sum / CurrentBar); double var03 = Instrument.MasterInstrument.RoundToTickSize(var3 / TickSize);
I get the error thrown
Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 21: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
I think I can understand the why of the error.
It is because in this case I'm asking to print the value of the 2000th bar, but since there are only 1038 bar (the CurrentBar) available on the chart ta this time, then it cannot return the value of the 2000th bar because it's yet to be populated (in 62 bars from now).
What I don't understand, using the same reasoning, using now the BarsBack in the loop instead of the CurrentBar as
PHP Code:double sum = 0; for(int i=0; i<BarsBack; i++) { sum += Rng0Rng1[i]; } double var3 = (sum / BarsBack); double var03 = Instrument.MasterInstrument.RoundToTickSize(var3 / TickSize);
BarsBack = 20
CurrentBar = 1038 (number of Bars currently available for calculation on the chart)
Rng0Rng1 = CurrentBar = 1038 (number of Bars currently available for calculation on the chart)
For i = 0 is 0 < 20 ? Yes, then add Rng0Rng1[0] double value to the sum variable
For i = 1 is 1 < 20 ? Yes, then add Rng0Rng1[1] double value to the sum variable
For i = 2 is 2 < 20 ? Yes, then add Rng0Rng1[2] double value to the sum variable
For i = 3 is 3 < 20 ? Yes, then add Rng0Rng1[3] double value to the sum variable
For i = 4 is 4 < 20 ? Yes, then add Rng0Rng1[4] double value to the sum variable
For i = 5 is 5 < 20 ? Yes, then add Rng0Rng1[5] double value to the sum variable
For i = 6 is 6 < 20 ? Yes, then add Rng0Rng1[6] double value to the sum variable
For i = 7 is 7 < 20 ? Yes, then add Rng0Rng1[7] double value to the sum variable
For i = 8 is 8 < 20 ? Yes, then add Rng0Rng1[8] double value to the sum variable
For i = 9 is 9 < 20 ? Yes, then add Rng0Rng1[9] double value to the sum variable
For i = 10 is 10 < 20 ? Yes, then add Rng0Rng1[10] double value to the sum variable
For i = 11 is 11 < 20 ? Yes, then add Rng0Rng1[11] double value to the sum variable
For i = 12 is 12 < 20 ? Yes, then add Rng0Rng1[12] double value to the sum variable
For i = 13 is 13 < 20 ? Yes, then add Rng0Rng1[13] double value to the sum variable
For i = 14 is 14 < 20 ? Yes, then add Rng0Rng1[14] double value to the sum variable
For i = 15 is 15 < 20 ? Yes, then add Rng0Rng1[15] double value to the sum variable
For i = 16 is 16 < 20 ? Yes, then add Rng0Rng1[16] double value to the sum variable
For i = 17 is 17 < 20 ? Yes, then add Rng0Rng1[17] double value to the sum variable
For i = 18 is 18 < 20 ? Yes, then add Rng0Rng1[18] double value to the sum variable
For i = 19 is 19 < 20 ? Yes, then add Rng0Rng1[19] double value to the sum variable
For i = 20 is 20 < 20 ? No, then do not add Rng0Rng1[20] double value to the sum variable and stop the loop
What's the contrast with the previous example you see?
I think I got it working by using
PHP Code:if (CurrentBar <= BarsBack) return;
PHP Code:if (CurrentBar < BarsBack) return;
PHP Code:if (CurrentBar < 21) return;
And making sure the second CurrentBar Check preceeds the snippet as follows
PHP Code:if (CurrentBar <= BarsBack) return; Print("CurrentBar : " + CurrentBar); Print(string.Format("{0} | listOfSeriesValuesBarsBack.Count(): {1} - BarsBack: {2} = {3} | listOfSeriesValues.Count() ): {4} ", Time[0], listOfSeriesValuesBarsBack.Count(), BarsBack, (listOfSeriesValuesBarsBack.Count() - BarsBack), listOfSeriesValues.Count() )); listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(0, BarsBack); double sum = 0; for(int i=0; i<BarsBack; i++) { sum += Rng0Rng1[i]; } double var3 = (sum / BarsBack); double var03 = Instrument.MasterInstrument.RoundToTickSize(var3 / TickSize); Print("var03 " + var03);
I'm not sure yet why it works and I'll do some more testing to find out. Thanks!Last edited by PaulMohn; 03-11-2022, 11:54 AM.
Comment
-
Hello PaulMohn,
I think you may be understanding.
If there are 5 apples on the table, and someone asks you for the 9th apple, what do you give them?
If there are 1038 bars, there is no bar 2000 bars ago. It does not exist so what can the computer provide you.
The same is true in loops and with list.Range(). You can only ask for the element indexes that exists.
If list.Count is 1, mean you have 1 element in it, and you ask for element 20 you get an index error.
This means you want to start adding elements to the list when CurrentBar is 0 (or 1) and then check back after 20 bars have passed, CurrentBar is 20 (or 21), and 20 elements have been added to the list, to get the range of the last 20 elements.
Below is a link to a forum post on index errors.
https://ninjatrader.com/support/foru...13#post1048513Chelsea B.NinjaTrader Customer Service
Comment
-
Ok, but when you say
If list.Count is 1, mean you have 1 element in it, and you ask for element 20 you get an index error.
This means you want to start adding elements to the list when CurrentBar is 0 (or 1) and then check back after 20 bars have passed, CurrentBar is 20 (or 21), and 20 elements have been added to the list, to get the range of the last 20 elements.
using that reasoning
For i = 0 is 0 < 20 ? Yes, then add Rng0Rng1[0] double value to the sum variable
For i = 1 is 1 < 20 ? Yes, then add Rng0Rng1[1] double value to the sum variable
For i = 2 is 2 < 20 ? Yes, then add Rng0Rng1[2] double value to the sum variable
For i = 3 is 3 < 20 ? Yes, then add Rng0Rng1[3] double value to the sum variable
For i = 4 is 4 < 20 ? Yes, then add Rng0Rng1[4] double value to the sum variable
For i = 5 is 5 < 20 ? Yes, then add Rng0Rng1[5] double value to the sum variable
For i = 6 is 6 < 20 ? Yes, then add Rng0Rng1[6] double value to the sum variable
For i = 7 is 7 < 20 ? Yes, then add Rng0Rng1[7] double value to the sum variable
For i = 8 is 8 < 20 ? Yes, then add Rng0Rng1[8] double value to the sum variable
For i = 9 is 9 < 20 ? Yes, then add Rng0Rng1[9] double value to the sum variable
For i = 10 is 10 < 20 ? Yes, then add Rng0Rng1[10] double value to the sum variable
For i = 11 is 11 < 20 ? Yes, then add Rng0Rng1[11] double value to the sum variable
For i = 12 is 12 < 20 ? Yes, then add Rng0Rng1[12] double value to the sum variable
For i = 13 is 13 < 20 ? Yes, then add Rng0Rng1[13] double value to the sum variable
For i = 14 is 14 < 20 ? Yes, then add Rng0Rng1[14] double value to the sum variable
For i = 15 is 15 < 20 ? Yes, then add Rng0Rng1[15] double value to the sum variable
For i = 16 is 16 < 20 ? Yes, then add Rng0Rng1[16] double value to the sum variable
For i = 17 is 17 < 20 ? Yes, then add Rng0Rng1[17] double value to the sum variable
For i = 18 is 18 < 20 ? Yes, then add Rng0Rng1[18] double value to the sum variable
For i = 19 is 19 < 20 ? Yes, then add Rng0Rng1[19] double value to the sum variable
For i = 20 is 20 < 20 ? No, then do not add Rng0Rng1[20] double value to the sum variable and stop the loop
if Rng0Rng1 = 1038
and
BarsBack = 20
then
there must be enough Rng0Rng1 bars/double values up to index [19] to fill the requirement of the
PHP Code:for(int i=0; i<BarsBack; i++)
Or is simply the index [20] that's causing the error (instead of just stopping the loop)?
Or is it otherwise? Thanks!
Comment
-
Hello PaulMohn,
I'm not understanding what you are asking.
The index [index] must be less than the size of the collection.
If CurrentBar is 1038, then any index less than this is fine to use for an index of a Series (which has one element for every bar on the chart).Chelsea B.NinjaTrader Customer Service
Comment
-
Chelsea, I got the mode apparently working in image attached
with
PHP Code:Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index]; listOfSeriesValues.Add(RangeTopRangeBottom[0]); if (CurrentBar <= BarsBack) return; listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(0, BarsBack); // Initialize the return value double mode = 1; double max = 0; if (listOfSeriesValuesBarsBack != null && listOfSeriesValuesBarsBack.Count() > 0) { // for (int element = 0; element < listOfSeriesValuesBarsBack.Count(); element++) // { // Print("CurrentBar : " + CurrentBar); //// Print("listOfSeriesValuesBarsBack[element] : " + listOfSeriesValuesBarsBack[element]); // Print("listOfSeriesValuesBarsBack.Count() : " + listOfSeriesValuesBarsBack.Count()); // } // Store the number of occurences for each element Dictionary<double, double> counts = new Dictionary<double, double>(); // Add one to the count for the occurence of a character foreach (double element in listOfSeriesValuesBarsBack) { if (counts.ContainsKey(element)) counts[element]++; else counts.Add(element, 1); } // Loop through the counts of each element and find the // element that occurred most often foreach (KeyValuePair<double, double> count in counts) { if (count.Value > max) { // Update the mode mode = count.Key; max = count.Value; } } Print("\n=== mode | Max | Count | CurrentBar ===\n"); Print("mode : " + mode); Print("max : " + max); Print("listOfSeriesValuesBarsBack.Count() : " + listOfSeriesValuesBarsBack.Count()); Print("CurrentBar : " + CurrentBar); Print("\n=== listOfSeriesValuesBarsBack Double values for Indexes 0-19 ===\n"); Print(" listOfSeriesValuesBarsBack[0] " + listOfSeriesValuesBarsBack[0]); Print(" listOfSeriesValuesBarsBack[1] " + listOfSeriesValuesBarsBack[1]); Print(" listOfSeriesValuesBarsBack[2] " + listOfSeriesValuesBarsBack[2]); Print(" listOfSeriesValuesBarsBack[3] " + listOfSeriesValuesBarsBack[3]); Print(" listOfSeriesValuesBarsBack[4] " + listOfSeriesValuesBarsBack[4]); Print(" listOfSeriesValuesBarsBack[5] " + listOfSeriesValuesBarsBack[5]); Print(" listOfSeriesValuesBarsBack[6] " + listOfSeriesValuesBarsBack[6]); Print(" listOfSeriesValuesBarsBack[7] " + listOfSeriesValuesBarsBack[7]); Print(" listOfSeriesValuesBarsBack[8] " + listOfSeriesValuesBarsBack[8]); Print(" listOfSeriesValuesBarsBack[9] " + listOfSeriesValuesBarsBack[9]); Print(" listOfSeriesValuesBarsBack[10] " + listOfSeriesValuesBarsBack[10]); Print(" listOfSeriesValuesBarsBack[11] " + listOfSeriesValuesBarsBack[11]); Print(" listOfSeriesValuesBarsBack[12] " + listOfSeriesValuesBarsBack[12]); Print(" listOfSeriesValuesBarsBack[13] " + listOfSeriesValuesBarsBack[13]); Print(" listOfSeriesValuesBarsBack[14] " + listOfSeriesValuesBarsBack[14]); Print(" listOfSeriesValuesBarsBack[15] " + listOfSeriesValuesBarsBack[15]); Print(" listOfSeriesValuesBarsBack[16] " + listOfSeriesValuesBarsBack[16]); Print(" listOfSeriesValuesBarsBack[17] " + listOfSeriesValuesBarsBack[17]); Print(" listOfSeriesValuesBarsBack[18] " + listOfSeriesValuesBarsBack[18]); Print(" listOfSeriesValuesBarsBack[19] " + listOfSeriesValuesBarsBack[19]); // Print(" listOfSeriesValuesBarsBack[20] " + listOfSeriesValuesBarsBack[20]); // Print(" listOfSeriesValuesBarsBack[21] " + listOfSeriesValuesBarsBack[21]); // Print("\n listOfSeriesValuesBarsBack[2000] " + listOfSeriesValuesBarsBack[2000]); }
However there is still the issue of sometimes the mode = 0 zero.
for example with
PHP Code:Print("\n=== mode | Max | Count | CurrentBar ==="); Print("\nmode : " + mode); Print("max : " + max); Print("listOfSeriesValuesBarsBack.Count() : " + listOfSeriesValuesBarsBack.Count()); Print("CurrentBar : " + CurrentBar); Print("\n=== listOfSeriesValuesBarsBack Double values for Indexes 0-19 ==="); Print(" \nlistOfSeriesValuesBarsBack[0] " + listOfSeriesValuesBarsBack[0]); Print(" listOfSeriesValuesBarsBack[1] " + listOfSeriesValuesBarsBack[1]); Print(" listOfSeriesValuesBarsBack[2] " + listOfSeriesValuesBarsBack[2]); Print(" listOfSeriesValuesBarsBack[3] " + listOfSeriesValuesBarsBack[3]); Print(" listOfSeriesValuesBarsBack[4] " + listOfSeriesValuesBarsBack[4]); Print(" listOfSeriesValuesBarsBack[5] " + listOfSeriesValuesBarsBack[5]); Print(" listOfSeriesValuesBarsBack[6] " + listOfSeriesValuesBarsBack[6]); Print(" listOfSeriesValuesBarsBack[7] " + listOfSeriesValuesBarsBack[7]); Print(" listOfSeriesValuesBarsBack[8] " + listOfSeriesValuesBarsBack[8]); Print(" listOfSeriesValuesBarsBack[9] " + listOfSeriesValuesBarsBack[9]); Print(" listOfSeriesValuesBarsBack[10] " + listOfSeriesValuesBarsBack[10]); Print(" listOfSeriesValuesBarsBack[11] " + listOfSeriesValuesBarsBack[11]); Print(" listOfSeriesValuesBarsBack[12] " + listOfSeriesValuesBarsBack[12]); Print(" listOfSeriesValuesBarsBack[13] " + listOfSeriesValuesBarsBack[13]); Print(" listOfSeriesValuesBarsBack[14] " + listOfSeriesValuesBarsBack[14]); Print(" listOfSeriesValuesBarsBack[15] " + listOfSeriesValuesBarsBack[15]); Print(" listOfSeriesValuesBarsBack[16] " + listOfSeriesValuesBarsBack[16]); Print(" listOfSeriesValuesBarsBack[17] " + listOfSeriesValuesBarsBack[17]); Print(" listOfSeriesValuesBarsBack[18] " + listOfSeriesValuesBarsBack[18]); Print(" listOfSeriesValuesBarsBack[19] " + listOfSeriesValuesBarsBack[19]); Print(" listOfSeriesValuesBarsBack[20] " + listOfSeriesValuesBarsBack[20]); Print(" listOfSeriesValuesBarsBack[21] " + listOfSeriesValuesBarsBack[21]); Print(" listOfSeriesValuesBarsBack[22] " + listOfSeriesValuesBarsBack[22]); Print(" listOfSeriesValuesBarsBack[23] " + listOfSeriesValuesBarsBack[23]); Print(" listOfSeriesValuesBarsBack[24] " + listOfSeriesValuesBarsBack[24]); Print(" listOfSeriesValuesBarsBack[25] " + listOfSeriesValuesBarsBack[25]); Print(" listOfSeriesValuesBarsBack[26] " + listOfSeriesValuesBarsBack[26]); Print(" listOfSeriesValuesBarsBack[27] " + listOfSeriesValuesBarsBack[27]); Print(" listOfSeriesValuesBarsBack[28] " + listOfSeriesValuesBarsBack[28]); Print(" listOfSeriesValuesBarsBack[29] " + listOfSeriesValuesBarsBack[29]); Print(" listOfSeriesValuesBarsBack[30] " + listOfSeriesValuesBarsBack[30]); Print(" listOfSeriesValuesBarsBack[31] " + listOfSeriesValuesBarsBack[31]); Print(" listOfSeriesValuesBarsBack[32] " + listOfSeriesValuesBarsBack[32]); Print(" listOfSeriesValuesBarsBack[33] " + listOfSeriesValuesBarsBack[33]); Print(" listOfSeriesValuesBarsBack[34] " + listOfSeriesValuesBarsBack[34]); Print(" listOfSeriesValuesBarsBack[35] " + listOfSeriesValuesBarsBack[35]); Print(" listOfSeriesValuesBarsBack[36] " + listOfSeriesValuesBarsBack[36]); Print(" listOfSeriesValuesBarsBack[37] " + listOfSeriesValuesBarsBack[37]); Print(" listOfSeriesValuesBarsBack[38] " + listOfSeriesValuesBarsBack[38]); Print(" listOfSeriesValuesBarsBack[39] " + listOfSeriesValuesBarsBack[39]); Print(" listOfSeriesValuesBarsBack[40] " + listOfSeriesValuesBarsBack[40]); Print(" listOfSeriesValuesBarsBack[41] " + listOfSeriesValuesBarsBack[41]); Print(" listOfSeriesValuesBarsBack[42] " + listOfSeriesValuesBarsBack[42]); Print(" listOfSeriesValuesBarsBack[43] " + listOfSeriesValuesBarsBack[43]); Print(" listOfSeriesValuesBarsBack[44] " + listOfSeriesValuesBarsBack[44]); Print(" listOfSeriesValuesBarsBack[45] " + listOfSeriesValuesBarsBack[45]); Print(" listOfSeriesValuesBarsBack[46] " + listOfSeriesValuesBarsBack[46]); Print(" listOfSeriesValuesBarsBack[47] " + listOfSeriesValuesBarsBack[47]); Print(" listOfSeriesValuesBarsBack[48] " + listOfSeriesValuesBarsBack[48]); Print(" listOfSeriesValuesBarsBack[49] " + listOfSeriesValuesBarsBack[49]); Print(" listOfSeriesValuesBarsBack[50] " + listOfSeriesValuesBarsBack[50]); Print(" listOfSeriesValuesBarsBack[51] " + listOfSeriesValuesBarsBack[51]); Print(" listOfSeriesValuesBarsBack[52] " + listOfSeriesValuesBarsBack[52]); Print(" listOfSeriesValuesBarsBack[53] " + listOfSeriesValuesBarsBack[53]); Print(" listOfSeriesValuesBarsBack[54] " + listOfSeriesValuesBarsBack[54]); Print(" listOfSeriesValuesBarsBack[55] " + listOfSeriesValuesBarsBack[55]); Print(" listOfSeriesValuesBarsBack[56] " + listOfSeriesValuesBarsBack[56]); Print(" listOfSeriesValuesBarsBack[57] " + listOfSeriesValuesBarsBack[57]); Print(" listOfSeriesValuesBarsBack[58] " + listOfSeriesValuesBarsBack[58]); Print(" listOfSeriesValuesBarsBack[59] " + listOfSeriesValuesBarsBack[59]);
Please see 2nd image attached.
2 PhotosLast edited by PaulMohn; 03-11-2022, 11:47 AM.
Comment
-
How would you suggest getting whatever the 3 most frequent modes ?
I thought of adding a new list to store the modes within it with the count.Key when count.Value == max
(from insight gathered from
Finding the mode of a list
How do I initialize an empty array in C#?
All possible array initialization syntaxes
Adding values to a C# array )
as
PHP Code:foreach (KeyValuePair<double, double> count in counts) { if (count.Value == max) { // Update the mode modes.Add(count.Key); modesValues = modes; } } Print("\n=== modes | arrayOfModes | modesValues ==="); Print("\nmodes : " + modes); Print("arrayOfModes : " + arrayOfModes); Print("modesValues : " + modesValues);
in this reduced script
PHP Code:namespace NinjaTrader.NinjaScript.Indicators { public class Testa : Indicator { private List<double> modes; private List<double> modesValues; protected override void OnStateChange() { if (State == State.SetDefaults) { } else if (State == State.Configure) { } else if (State == State.DataLoaded) { modes = new List<double>(); modesValues = new List<double>(); } listOfSeriesValues.Add(RangeTopRangeBottom[0]); if (CurrentBar <= BarsBack) return; listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(0, BarsBack); // Initialize the return value double mode = 1; double max = 1; if (listOfSeriesValuesBarsBack != null && listOfSeriesValuesBarsBack.Count() > 0) { Dictionary<double, double> counts = new Dictionary<double, double>(); foreach (double element in listOfSeriesValuesBarsBack) { if (counts.ContainsKey(element)) counts[element]++; else counts.Add(element, 1); } foreach (KeyValuePair<double, double> count in counts) { if (count.Value > max) { // Update the mode mode = count.Key; max = count.Value; } } foreach (KeyValuePair<double, double> count in counts) { if (count.Value == max) { // Update the mode modes.Add(count.Key); modesValues = modes; } } Print("\n=== modes | arrayOfModes | modesValues ==="); Print("\nmodes : " + modes); Print("arrayOfModes : " + arrayOfModes); Print("modesValues : " + modesValues);
but it's printing
=== modes | arrayOfModes | modesValues ===
modes : System.Collections.Generic.List`1[System.Double]
arrayOfModes : System.Double[]
modesValues : System.Collections.Generic.List`1[System.Double]
Why aren't the count.Key double values added to the private List<double> modes; list?
Why do the prints return the types instead of the values? Thanks!1 PhotoLast edited by PaulMohn; 03-11-2022, 08:28 AM.
Comment
-
Chelsea I've found a way to to get the 3 most frequent modes with help from
How to get first object out from List<Object> using Linq
(needed to access and retrieve each list element value separately from their index, one by one)
with
PHP Code:namespace NinjaTrader.NinjaScript.Indicators { public class Testa : Indicator { private List<double> modes; private List<double> modesValues; protected override void OnStateChange() { if (State == State.SetDefaults) { } else if (State == State.Configure) { } else if (State == State.DataLoaded) { modes = new List<double>(); modesValues = new List<double>(); } protected override void OnBarUpdate() { if (CurrentBar < 1) return; Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index]; listOfSeriesValues.Add(Rng0Rng1[0]); if (CurrentBar <= BarsBack) return; listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(0, BarsBack); // Initialize the return value double mode = 1; double max = 1; if (listOfSeriesValuesBarsBack != null && listOfSeriesValuesBarsBack.Count() > 0) { // for (int element = 0; element < listOfSeriesValuesBarsBack.Count(); element++) // { // Print("CurrentBar : " + CurrentBar); //// Print("listOfSeriesValuesBarsBack[element] : " + listOfSeriesValuesBarsBack[element]); // Print("listOfSeriesValuesBarsBack.Count() : " + listOfSeriesValuesBarsBack.Count()); // } // Store the number of occurences for each element // Key = Double value from listOfSeriesValuesBarsBack // Value = The frequency of the Double value from listOfSeriesValuesBarsBack Dictionary<double, double> counts = new Dictionary<double, double>(); // Add one to the count for the occurence of a character foreach (double element in listOfSeriesValuesBarsBack) { if (counts.ContainsKey(element)) counts[element]++; else counts.Add(element, 1); } // Loop through the counts of each element and find the // element that occurred most often foreach (KeyValuePair<double, double> count in counts) { if (count.Value > max) { // Update the mode mode = count.Key; max = count.Value; Print("count.Key1 : " + count.Key); } else if (count.Value == max) { // Collect the doubles modes and the ints values modes.Add(count.Key); modesValues.Add(count.Value); Print("count.Key2 : " + count.Key); } } Print("\nmodes1 : " + modes[0] + " modesValues1 : " + modesValues[0]); Print("modes2 : " + modes[1] + " modesValues2 : " + modesValues[1]); Print("modes3 : " + modes[2] + " modesValues3 : " + modesValues[2]); Print("modes4 : " + modes[3] + " modesValues4 : " + modesValues[3]);
Output
Now all that remains is getting the .GetRange() with the latest listOfSeriesValuesBarsBack values. I'll test and be back asa. Thanks!Last edited by PaulMohn; 03-12-2022, 10:30 AM.
Comment
-
Chelsea I think I've found a way using the listOfSeriesValues.Count() instead of the CurrentBar as parameters for the .GetRange() method.
(from help from How can I find the last element in a List<>? )
I'd be interested in knowing how to achieve the same with CurrentBar if it is possible (I won't use CurrentBar for the current use but I'm curious to know if it's possible, I couldn't derive if it is from my testing (see demos below)).
After extensive prints testing, I came to the view CurrentBar is not always the latest/most recent bar on the chart. Rather it is dependent of the CurrentBar Check value.
As CurrentBar index 0 is set by the CurrentBar check value, or by BarsBack value.
Demo 1
https://drive.google.com/file/d/10T1...ew?usp=sharing
PHP Code:BarsAgo[0] to [4] vs CurrentBar[0] to [4] : 0. CurrentBar Start: If no CurrentBar Check Then CurrentBar stats at the leftmost bar on the chart -> CurrentBar : 0 = Leftmost Bar If CurrentBar Check is set to if (CurrentBar < 1) return; Then CurrentBar stats at Bar index 1 -> CurrentBar : 0 = Bar indexed 1 (the 2nd leftmost bar) If CurrentBar Check is set to if (CurrentBar < 2) return; Then CurrentBar stats at Bar index 2 -> CurrentBar : 0 = Bar indexed 2 (the 3nd leftmost bar) etc. 1. BarsAgo starts from the CurrentBar and goes Backward If no CurrentBar Check Then CurrentBar stats at the leftmost bar on the chart -> CurrentBar : 0 = Leftmost Bar ==> BarsAgo start = Leftmost Bar If CurrentBar Check is set to if (CurrentBar < 1) return; Then CurrentBar stats at Bar index 1 -> CurrentBar : 0 = Bar indexed 1 (the 2nd leftmost bar) ==> BarsAgo start = Bar indexed 1 (the 2nd leftmost bar) If CurrentBar Check is set to if (CurrentBar < 2) return; Then CurrentBar stats at Bar index 2 -> CurrentBar : 0 = Bar indexed 2 (the 3nd leftmost bar) ==> BarsAgo start = Bar indexed 2 (the 2nd leftmost bar) etc.
Demo 2
https://drive.google.com/file/d/10p1...ew?usp=sharing
Also the GetRange() index (GetRange(index, Barsback)) works for 0 and 1 but not for 2 and when with 0 and 1 the start bar is the same.
Demo 3
https://drive.google.com/file/d/1NqI...ew?usp=sharing
listOfSeriesValues.Count() instead of the CurrentBar
PHP Code:listOfSeriesValues.Add(RangeTopRangeBottom[0]); if (CurrentBar <= BarsBack) return; listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(((listOfSeriesValues.C ount() - 1) - BarsBack), (BarsBack - 1)); Print("\nTest : " + listOfSeriesValues.Count()); // THE TOTAL FORM THE LIST Print("\nTest2 : " + (listOfSeriesValues.Count()-BarsBack)); // THE TOTAL LIST - BARSBACK Print("\nTest3 : " + (listOfSeriesValues.Count()-1)); // THE LAST ELEMENT IN THE LIST Print("\nTime : " + Time[0]); Print("CurrentBar : " + CurrentBar); Print("BarsBack : " + BarsBack); Print("CurrentBar - BarsBack : " + (CurrentBar - (BarsBack))); Print("\nlistOfSeriesValues.Count() : " + listOfSeriesValues.Count()); // for (int element = 0; element < listOfSeriesValues.Count(); element++) // { // Print("\nlistOfSeriesValues[element] : " + listOfSeriesValues[element]); // } // Print("listOfSeriesValuesBarsBack.Count() : " + listOfSeriesValuesBarsBack.Count()); // for (int element = 0; element < listOfSeriesValuesBarsBack.Count(); element++) // { // Print("listOfSeriesValuesBarsBack[element] : " + listOfSeriesValuesBarsBack[element]); // } // Print("BarsBack + 1 : " + (BarsBack + 1)); // Print("CurrentBar - 1 : " + (CurrentBar - 1)); // Print("\nTime : " + Time[0]); // Print("CurrentBar : " + CurrentBar); // Print("BarsBack : " + BarsBack); // Print("CurrentBar - BarsBack : " + (CurrentBar - (BarsBack))); Print("\nlistOfSeriesValues[0] : " + listOfSeriesValues[0]); Print("listOfSeriesValues[1] : " + listOfSeriesValues[1]); Print("listOfSeriesValues[2] : " + listOfSeriesValues[2]); Print("listOfSeriesValues[3] : " + listOfSeriesValues[3]); Print("listOfSeriesValues[4] : " + listOfSeriesValues[4]); Print("listOfSeriesValues[5] : " + listOfSeriesValues[5]); Print("\n=== BarsAgo[0] to [5] vs CurrentBar[0] to [5] : ==="); Print("\nHigh[0]-Low[0] : " + (High[0]-Low[0])); Print("High[0] : " + High[0]); Print("Low[0] : " + Low[0]); Print("\nHigh[1]-Low[1] : " + (High[1]-Low[1])); Print("High[1] : " + High[1]); Print("Low[1] : " + Low[1]); Print("\nHigh[2]-Low[2] : " + (High[2]-Low[2])); Print("High[2] : " + High[2]); Print("Low[2] : " + Low[2]); Print("\nHigh[3]-Low[3] : " + (High[3]-Low[3])); Print("High[3] : " + High[3]); Print("Low[3] : " + Low[3]); Print("\nHigh[4]-Low[4] : " + (High[4]-Low[4])); Print("High[4] : " + High[4]); Print("Low[4] : " + Low[4]); Print("\nHigh[5]-Low[5] : " + (High[5]-Low[5])); Print("High[5] : " + High[5]); Print("Low[5] : " + Low[5]);
Thanks!Last edited by PaulMohn; 03-13-2022, 10:44 AM.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by tkaboris, Yesterday, 07:53 PM
|
2 responses
12 views
0 likes
|
Last Post
by tkaboris
Today, 08:27 AM
|
||
Started by smartromain, Today, 06:05 AM
|
1 response
11 views
0 likes
|
Last Post Today, 08:08 AM | ||
Started by wuannetraam, Yesterday, 07:43 AM
|
6 responses
38 views
0 likes
|
Last Post
by wuannetraam
Today, 08:02 AM
|
||
Started by pjsmith, 06-30-2021, 02:57 AM
|
2 responses
22 views
0 likes
|
Last Post
by balltrader
Today, 07:29 AM
|
||
Started by Richardmm, 06-05-2018, 08:29 AM
|
5 responses
627 views
0 likes
|
Last Post
by balltrader
Today, 07:27 AM
|
Comment