Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Number of Last consecutive Bars

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

    Number of Last consecutive Bars

    Hi,
    I am trying to get an indicator which shows the number of last consecutive up or down bars.
    I was not able to find any indicators that gives me this information. nbarsup and nbarsdown provides a 1 or 0 if the if the last n periods were in a trend.

    What I need is a numerical count for how many bars/ periods the candles have been the same color.
    If the candle 4 periods ago was green and the 3 more recent candles were all red, indicator should be -3.
    Then if the last six candles were all green, indicator equal 6

    Is this difficult to build?
    Can you please help?


    #2
    Hello horacioofman,

    I'm not certain there is an existing method that has that specific functionality and you will likely need to do this in a for loop.

    Starting at 0 bars ago (the current bar) to CurrentBar (the number of barsAgo to the first bar), compare if the index bar meets the criteria, if it does increment an integer as a counter, if it does not then break the loop.

    Code:
    int consecutiveBarsFound = 0;
    
    for (int index = 0; index < CurrentBar; index++)
    {
    if (Close[index] < Open[index]) // your condition here
    {
    consecutiveBarsFound++;
    }
    else 
    {
    break;
    }
    }
    
    Print(consecutiveBarsFound);
    Below is a link to a support article on looping.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      HI Chelsea,

      I managed to put together an indicator, but I am having a different issue now. I will open a different post for specifc problemshooting.

      Thanks for your help
      Attached Files
      Last edited by horacioofman; 11-03-2024, 01:20 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      547 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      323 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      99 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      543 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      545 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X