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 NullPointStrategies, Today, 05:17 AM
            0 responses
            23 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            120 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            63 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            41 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            45 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X