Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Count Number of Bars

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

    Count Number of Bars

    Hi,

    I would like to count the number of bars since Trend[1] != Trend[2]. So essentially it's at the point where the trend direction changes. Is below how I would be able to do this or am I completely off?

    int TrendCounter = 0;

    OnBarUpdate()

    if (Trend[1] != Trend[2])
    {
    TrendCounter = 0;
    }
    else
    TrendCounter += _________;
    }

    #2
    Hello AgriTrdr,

    Yes, this would be the general idea.

    Below is a link to a forum post that discusses incrementing an integer as a counter.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      So in the attached post, is:

      Counter++;

      adding the bars or what is it adding?

      Comment


        #4
        Hello AgriTrdr,

        Using '++' means to increment by one. This means add one to the existing value.

        Below is a link to the Microsoft documentation on arithmetic operators.
        Learn about C# operators that perform multiplication, division, remainder, addition, and subtraction operations with numeric types.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you! That worked.

          Is there a way to take the current TrendCounter value and compare it to the previous TrendCounter value of the prior before resets to 0?

          Comment


            #6
            Hello AgriTrdr,

            You could assign it to another variable.

            private double myVariable;

            myVariable = TrendCounter[0];


            }
            else​ {

            TrendCounter += _________;

            Print("myVariable: " + myVariable + " | TrendCounter: " + TrendCounter);

            }
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            87 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            132 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            68 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by cmoran13, 04-16-2026, 01:02 PM
            0 responses
            118 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            67 views
            0 likes
            Last Post PaulMohn  
            Working...
            X