NexusFi: Find Your Edge


Home Menu

 





Index Out of Range Exceptions


Discussion in NinjaTrader

Updated
    1. trending_up 2,212 views
    2. thumb_up 3 thanks given
    3. group 2 followers
    1. forum 2 posts
    2. attach_file 0 attachments




 
Search this Thread

Index Out of Range Exceptions

  #1 (permalink)
 
Zondor's Avatar
 Zondor 
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

Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Deepmoney LLM
Elite Quantitative GenAI/LLM
Are there any eval firms that allow you to sink to your …
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
 
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102



Zondor View Post
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.

The Index Out of Range Runtime Error occurs when the code is flawed, meaning that the code is trying to access a value that does not exist.

Try{}Catch is certainly not the right response. Try{}Catch is quite similar to the ostrich sticking its head into the sand. You know that there is a problem, but you do not want to see it. The result is bad code, as the coder admits that he did not thoroughly understand his own code. I agree that zero tolerance is the best way to deal with this problem. You need to find out why the Index Out of Range runtime error occured, and not just hide it.

I mostly use Try{}Catch now, when an indicator or strategy accesses an other indicator or file, and I cannot be sure that this file exists on the PC of the user, but certainly not to avoid Index Out of Range runtime errors.

Reply With Quote




Last Updated on November 17, 2011


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts