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 sjsj2732, Yesterday, 04:31 AM
          0 responses
          31 views
          0 likes
          Last Post sjsj2732  
          Started by NullPointStrategies, 03-13-2026, 05:17 AM
          0 responses
          286 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          283 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          133 views
          1 like
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          91 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Working...
          X