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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            43 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            20 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            30 views
            1 like
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            48 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            38 views
            0 likes
            Last Post CarlTrading  
            Working...
            X