Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Brandon H.NinjaTrader Customer Service

    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.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by pibrew, Today, 06:37 AM
        0 responses
        0 views
        0 likes
        Last Post pibrew
        by pibrew
         
        Started by rbeckmann05, Yesterday, 06:48 PM
        1 response
        12 views
        0 likes
        Last Post bltdavid  
        Started by llanqui, Today, 03:53 AM
        0 responses
        6 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        10 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        15 views
        0 likes
        Last Post AaronKoRn  
        Working...
        X