Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Condition met in N consecutive bars

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

    Condition met in N consecutive bars

    Hello,
    I am not very experienced in ninjascript programming.
    I am trying to create a condition that is met on N consecutive bars.
    For example, Open is lower than close for the last N consecutive bars.
    How can I do that?
    Thanks in advance.

    Pedro BM

    #2
    Hello PedroBM,

    Thanks for your post.

    You could consider creating conditions in your script that compare the Open price to the Close price and pass in the barsAgo value for the bar you want to compare the prices for.

    For example, to do something after the Open price is greater than the Close for the last 3 bars, the condition would look something like this. Note that to compare the current Open to the current Close, you would pass in a barsAgo value of 0. To reference the previous bar you would pass in a barsAgo value of 1. To reference 2 bars back from the current bar, you would pass in a barsAgo value of 2, and so on.

    Code:
    if (Open[0] > Close[0] && Open[1] > Close[1] && Open[2] > Close[2])
      //do something
    See the help guide documentation below for more information.
    Close: https://ninjatrader.com/support/help.../nt8/close.htm
    Open: https://ninjatrader.com/support/helpGuides/nt8/open.htm

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hello Brandon,

      Thank you for your answer.

      In the case that I want to meet a condition N times during the last M bars.
      How should I do this?

      Regards

      Pedro

      Comment


        #4
        Hello PedroBM,

        Thanks for your note.

        You could consider creating two counters in your script.

        The first counter would be used to count from 1 to M number of bars. Then, you could have a second counter that increments each time the condition becomes true.

        Depending on your use case you might also consider using MRO() which returns the number of bars ago that the test condition evaluated to true within the specified lookback period expressed in bars.

        See this help guide for more information: https://ninjatrader.com/support/help...urence_mro.htm

        Please note that in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        666 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        376 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        110 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        580 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X