I'm using the simulated data feed.
I created a custom indicator, which I translated from a working TOS script. It compiles cleanly, but gives no output on the chart.
I opened an output window, and see the following error:
Error on calling 'OnBarUpdate' method for indicator 'SecondDer' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
I put in some print statements to see where exactly the error was occurring. I deliberately put one before accessing the SMA function, then after, as I'm using an SMA with 10 and one with 11 periods.
These work fine, as the print statements before and after show up on the output window.
After that, I print the value of the counter I'm using in a while statement where I'm accessing bars via the Close[subscript] array, and it is set to the expected value of 10.
The above error pops up after the print of the counter, so obviously the while loop is causing this.
Output window output now looks like this:
Before SMA
After SMA
10
Error on calling 'OnBarUpdate' method for indicator 'SecondDer': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
Below is the while loop. If someone can tell me what I'm doing wrong, I'd be much obliged:
Scratch = AverageDiv - 1; (this is set to 11 at the top of the program; this statement sets this to 10, which is printed in the output window output above after the "After SMA" output.)
Print(Scratch);
while (Scratch >= 0)
{
ROCNow = (Close[Scratch] - Close[Scratch + 1]) / Close[Scratch + 1];
ROCPrev = (Close[Scratch + 1] - Close[Scratch + 2]) / Close[Scratch + 2];
if (Math.Abs(Close[Scratch + 1] - Close[Scratch + 2]) > 0)
{
SecondDerValue[Scratch] = ((ROCNow - ROCPrev) / Math.Abs(ROCPrev));
}
{
SecondDerValue[Scratch] = ROCNow;
}
SumSecondDer = SumSecondDer + SecondDerValue[Scratch];
Scratch = Scratch - 1;
}
SecondDerAverage = SumSecondDer / AverageDiv;
Print("Average");
Print(SecondDerAverage);
Comment