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 cmoran13, 04-16-2026, 01:02 PM
          0 responses
          51 views
          0 likes
          Last Post cmoran13  
          Started by PaulMohn, 04-10-2026, 11:11 AM
          0 responses
          31 views
          0 likes
          Last Post PaulMohn  
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          165 views
          1 like
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          100 views
          1 like
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          160 views
          2 likes
          Last Post CaptainJack  
          Working...
          X