Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need to run condition from X bars ago to current bar

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

    Need to run condition from X bars ago to current bar

    Hi, I need help in how to code the logic to loop thru and check a condition from 'X' bars ago (this can be variable) to the most recent bar. That is based on the OHLC value of an 'X' bar ago, I need to check the condition on every future bar until the last current bar. (as per image below). Any help would be appreciated. Thanks.

    Click image for larger version

Name:	image.png
Views:	196
Size:	16.2 KB
ID:	1244439
    Attached Files
    Last edited by Kuvala; 04-03-2023, 11:12 PM.

    #2
    Hello Kuvala,

    There is a NinjaScript method which can be used for checking a condition over a period of bars, that is called CountIF: https://ninjatrader.com/support/help...ghtsub=countif

    You can also use a C# for loop for that purpose.

    Comment


      #3
      Thank you. But how to identify the bar number from where I start checking my condition and I need to check the condition in each future bar until the current bar..need help in identifying the start bar number and current bar number so that I can write a loop logic. When I print current bar number at each candle I get incremental value from left most bar in chart..so not sure how to come up with the loop logic. Please refer to the diagram I attached of what I am looking for.

      Comment


        #4
        Hello Kuvala,

        If you want to use a loop I would suggest looking at external C# tutorials on how to use loops in C#. NinjaScript is C# language.

        To find the bar number in a loop would require that you subtract the iterator in the loop from the CurrentBar. That would give you a BarsAgo. For example if you started a loop with its iterator as CurrentBar and subtract 1 for each loop iteration you would get a number less than CurrentBar for each loop. When you subtract that number from the current bar it would give you a BarsAgo which you can use with any of the price or time series.


        .


        Comment


          #5
          Is there any sample code to identify BarAgo ?

          Comment


            #6
            Hello Kuvala,

            I am not aware of any samples using a for loop in NinjaScript. for loops are a general C# concept which you can learn about in external C# learning resources. I would suggest to search online for "C# for loop example" to get some samples of using loops.

            To get a BarsAgo from the CurrentBar you would use math and subtract the iterator for currentbar:

            Code:
            int barsAgo = CurrentBar - i;
            When working with loops there is a good chance you will hit an indexing error if the loop is not properly formed, I would also suggest using Prints with the loop to observe how your code is working as part of the debugging process. https://ninjatrader.com/support/help...lightsub=print

            Comment


              #7
              Bars.GetBar(DateTimetime)

              Comment


                #8
                Thanks for the suggestions.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                612 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                355 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                105 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                561 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                564 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X