Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculating the number of bars between Bar[0] backwards to a given starting point.

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

    Calculating the number of bars between Bar[0] backwards to a given starting point.

    Hello !
    Here is the problem.

    I want to have the number of bars between a fixed starting point to the current bar, the Bar[0]. For the starting point, I have chosen the moment when the faster moving average crosses above the slower one. At this moment, I save the index of the bar in int startP. On the output window, I have 3758, 3759…. Minutes later, couple of bars are printed on the chart from the starting point stardP. I want to know the number of bars between these two points (Bar[0] and startP). I make the following calculation. int barsDiff = CurrentBar - startP. On the output window, I have the value 0. It can not be zero, because I can count manually the bars and I have at least 10 bars.


    Here is the piece of my code:
    HTML Code:
      if(myFasterMA CrossesAbove mySlowerMA)
      {
         if(!flagUp)
         {
            int startP    = CurrentBar;
            falgUp = true;
         }
    
         int barsDiff = CurrentBar-startP;
      }
    Could someone please points out wher I am mistaking?


    Thanks in advance!

    #2
    Hello Stanfillirenfro,

    It appears you are only barsDiff to the CurrentBar minus startP when the CrossAbove occurs, so that would be 0 bars ago.

    Perhaps you are wanting to calculate this on every bar, not just in the logic block when the CrossAbove occurs?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Many thanks NinjaTrader_ChelseaB for your reply.

      Here is the idea.

      1) I want the CrossAbove to occur.
      2) After the CossAbove is in place, I want to count each subsequent bar formed.

      Is the calculation at the wrong place inn the code?

      Thanks in advance!

      Comment


        #4
        Hello Stanfillirenfro,

        " After the CossAbove is in place, I want to count each subsequent bar formed."

        Your code to calculate the number of bars is within the scope of CrossAbove condition so it will only evaluate when there is a CrossAbove.

        Calculate outside of the scope of the logic block so this evaluates on every bar.

        See the section on Scope in the Basic Syntax support article linked below.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Many thanks NinjaTrader_ChelseaB for your help.

          Problem solved.

          Thanks again!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          566 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
          547 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          548 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X