Portland Oregon, United States
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731
|
When you see the dreaded Index Out of Range runtime error, it generally means that you are trying to access a value of an array that does not exist. Try{}Catch error handling will prevent these errors from crashing an indicator, but I now have zero tolerance for them. Here is how I deal with them.
One approach that often works is to make sure that the DataSeries or other type of array contains enough elements so that what you are trying to do can actually happen. For example, if you are using if(Risng(Values[0])) as a condition, don't do it unless Values[0].Count >=2, because if an array has less than 2 elements, Rising and Falling are meaningless.
Sometimes the errror occurs during startup when your array is empty, ie contains no elements and has a Count property of zero.. So for anything that accesses an array called MyArray, using if(MyArray.Count>3) as an enabling condition before trying to access a value of the array will take care of that condition.
Or before accessing the value for b bars back , check that MyArray.ContainsValue(b) is True.
If b is negative you can have problems. Unless you take special precautions and are more than b bars back from the CurrentBar, the value you are looking for will not exist. Avoid doing this.
NinjaTrader_RyanM thanked the original version of this post on the Ninjatrader forum, giving it an aura of respectability.
https://forum.ninjatrader.com/showthread.php?p=263028
|