21/03/2022 19:09:49 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 301: 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.
Time Category Message
21/03/2022 16:16:29 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 602: 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.
Time Category Message
21/03/2022 15:50:26 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 201: 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.
Time Category Message
21/03/2022 15:43:11 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 201: Index was outside the bounds of the array.
The offending lines were
double var4_0 = modes[0];
double var4_1 = modes[1];
double var4_2 = modes[2];
double var4_3 = modes[3];
double var4_01 = Instrument.MasterInstrument.RoundToTickSize(modes[0] / TickSize);
double var4_02 = Instrument.MasterInstrument.RoundToTickSize(modes[1] / TickSize);
double var4_03 = Instrument.MasterInstrument.RoundToTickSize(modes[2] / TickSize);
double var4_04 = Instrument.MasterInstrument.RoundToTickSize(modes[3] / TickSize);
But those lines were necessary so after more trial I finally found
I could solve the errors by commenting the unnecessary part of the foreach loop (I found out I didn't needed it while I thought I did need it prior to finding out I didn't) as
// if (count.Value > max)
// {
// // Update the mode
// mode = count.Key;
// max = count.Value;
// // Print("count.Key1 : " + count.Key);
// }else
Now it works for all BarsBack value with the complete snippet as
// Mode Code Starts Here
// 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 occurrences 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);
}
}
}
double var4_0 = modes[0];
double var4_1 = modes[1];
double var4_2 = modes[2];
double var4_3 = modes[3];
double var4_01 = Instrument.MasterInstrument.RoundToTickSize(modes[0] / TickSize);
double var4_02 = Instrument.MasterInstrument.RoundToTickSize(modes[1] / TickSize);
double var4_03 = Instrument.MasterInstrument.RoundToTickSize(modes[2] / TickSize);
double var4_04 = Instrument.MasterInstrument.RoundToTickSize(modes[3] / TickSize);
Thanks again!
Comment