Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Out of range error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Out of range error

    I have two if statements, one detects a cross above a level of an indicator the other detects a cross below a level of the indicator. There is nothing fancy about the code just two simple if statements. The first "IF" functions fine, however when I add in the second "IF" statement now I get an "Out of range error"

    The first IF STATEMENT :
    if (Default[0] > 70 && Default[1] <= 70)
    {
    startBarsAgo = CurrentBar;
    Draw.VerticalLine(this, @"xAbove70" + Bars.Count.ToString(),
    0, Brushes.HotPink, DashStyleHelper.Dot, 2);
    Print(string.Format("Start bar : {0}", startBarsAgo.ToString()));
    }

    The second IF STATEMENT :
    if (Default[0] < 70 && Default[1] >= 70)
    {
    endBarsAgo = CurrentBar;
    Draw.VerticalLine(this, @"xBelow70" + Bars.Count.ToString(),
    0, Brushes.Cyan, DashStyleHelper.Dash, 2);
    Print(string.Format("End bar : {0}", endBarsAgo.ToString()));
    }

    #2
    So in trouble shooting I tried putting a try/catch block around the second "IF" statement and it works, however I could still use some insight into what is going on, what I'm missing. Also, no exception is being thrown and output to the console.

    Comment


      #3
      My guess is you're accessing Default value 1 bar ago at CurrentBar = 0. It works in the first if statement because Default[0] > 70 is false so it doesn't check Default[1] <= 70 because it's the way && operator works.

      So place this at start of your code:
      if(CurrentBar <1) return;

      Comment


        #4
        Thanks Leeroy, that worked out.

        Comment


          #5
          Hello Itachi,

          Below is a link to a forum post on indexing errors.
          Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:


          Leeroy_Jenkins, thank you for providing this guidance.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CaptainJack, 05-29-2026, 05:09 AM
          0 responses
          237 views
          0 likes
          Last Post CaptainJack  
          Started by CaptainJack, 05-29-2026, 12:02 AM
          0 responses
          152 views
          0 likes
          Last Post CaptainJack  
          Started by charlesugo_1, 05-26-2026, 05:03 PM
          0 responses
          163 views
          1 like
          Last Post charlesugo_1  
          Started by DannyP96, 05-18-2026, 02:38 PM
          1 response
          246 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 05-11-2026, 05:56 AM
          0 responses
          199 views
          0 likes
          Last Post CarlTrading  
          Working...
          X