Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to address a series of bars ?

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

    How to address a series of bars ?

    Hello

    One of the conditions of my custom indicator is that it draws an arrow on the chart every time the high of a candle is lower than the high of at least one of the highs of the last ten candles. Is there a more efficient way to code this than to include every possible situation in the code?

    Currently it looks like this:

    if(High[0] < High[1] || High[0] < High[2] || High[0] < High[3] || etc until High[10]

    I'm looking for the correct version of something like:

    if(High[0] < High[1-10]

    Thanks

    #2
    Hello,

    This is one way you could do it.

    such as the following,

    for (int i = 0; i < 10; i++)
    {
    if (High[0] > High[i])
    {
    //Either store a counter variable and check to make sure this is equal to something to amke sure it did occur in the last 10 bars. This would iterate through all 10 previous values, or whatever you set as the condition for the for loop for how many bars back you want to go.
    BrettNinjaTrader Product Management

    Comment


      #3
      Hi Brett

      I just implemented what you suggested in my strategy, but it gets automatically disabled when I hit F5.

      Here's the code snippet:

      for (int i = 0; i < 30; i++)
      {
      if (High[0] < High[i])
      {
      SpaceToExit = true;
      }

      else
      {
      SpaceToExit = false;
      }
      }

      What did I miss?

      Thanks a lot.

      Comment


        #4
        Hello,

        PLease check your log tab in the control center to see the error.

        Most likely its the eror saying you dont have enough bars in the chart.

        Please see this tips page this will most likely do the trick. You need to make sure you have at least 30 bars before running this for loop.



        Let me know if I can be of further assistance.
        BrettNinjaTrader Product Management

        Comment


          #5
          Brett

          The log says "You are accessing an index with a value that is invalid since it's out of range, ie accessing a series with a value of 5 when there are only 4 bars on the chart".

          I don't understand this as there are dozens of bars on my chart.

          What am I missing?

          Thanks

          Comment


            #6
            Hello,

            This is the error I was talking about in my previous post.

            Please add the article information in that I linked to in my previous post to resolve this issue.

            OnBarUpdate() runs for every bar in the chart. Starting from Bar 1 to the last bar in the cahrt. What occurs is that when it first starts on Bar 1, its trying to grab a value from 30 bars ago. THis will not work as there is no bar that time. Therefor you get an error.
            BrettNinjaTrader Product Management

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            571 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            330 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            548 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            549 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X