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

# of bars up/dn in lookback -- please help

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

    # of bars up/dn in lookback -- please help

    Hello NT community,

    I am looking to create a list of the last x bars and count how many of the bars within the lookback period are up bars and how many are down bars. Could someone please kindly point me towards a method or logic that would help me achieve this? Do I need to create a loop of some kind that counts up the index from the oldest relevant bar? If so, what is the logic for that? Etc...

    Thanks, community. Any logic samples or links to relevant NinjaScript reference pages would be much appreciated.

    #2
    Hello lunardiplomacy,

    You could likely use the CountIf function for this or a loop depending on how complex you need the conditions to be. If you just need a count the CountIf is likely the easiest way:


    You supply a condition and a period and it returns a count of true occurasnces which you can use in a condition like shown in the help guide.

    The alternative would be to use a loop much in the same way, you would loop over a period of bars and check the values of X BarsAgo in your conditions. If the CountIf does not work for your use let me know and we could go further into loops. this is not generally a NinjaScript topic and you can find C# examples however we could explore that further if needed.


    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello, Jesse, thanks for getting back to me!

      It sounds from the description it would be sufficient for my purposes logically, however it says here that this method will not work for "multi-series" strategies or indicators. My strategy uses a secondary tick series, that means I would need to use a loop instead?

      Comment


        #4
        for (int i = 0; Close[i] > Open[i]; i++)
        {
        Count? += 1
        }

        would it be something like that? I have seen loops before and I understand the concept (I think) but haven't implemented them before myself, nor do I really know how. I am reading from C# reference directly, but don't quite get how to piece this together with price data within my strategy in ninjascript.

        Comment


          #5
          Hello lunardiplomacy,

          Right, if you have a multi series you would need a loop, you may also need to specify the series you want to use depending on how you formed your logic. I will provide an example below assuming you are working in a specific BarsInProgress condition like: if(BarsInProgress == 0)

          A for loop needs a starting point and ending point along with an increment. You have the starting point and increment but not the end point. Here is a external guide that contains some general C# for loop samples which can help explain the syntax better: https://www.w3schools.com/cs/cs_for_loop.asp

          Your condition will need to go within the loop, we also need to account for using BarsAgo when using a loop by doing a CurrentBar check.

          Code:
          if(CurrentBar < Period) return;
          for (int i = 0; i <= Period; i++)
          {
             if(Close[i] > Open[i])
             {
          
             }
          }

          If you are not using a BarsInProgress condition you would likely need to use Closes[1][i] in your loop instead where you specify the series you wanted data for.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            This is excellent, Jesse. Thank you. This will not only work for what I originally asked about, but I can already think of ways to build upon it. Cheers.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Kaledus, Today, 01:29 PM
            0 responses
            3 views
            0 likes
            Last Post Kaledus
            by Kaledus
             
            Started by PaulMohn, Today, 12:36 PM
            1 response
            16 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by yertle, Yesterday, 08:38 AM
            8 responses
            36 views
            0 likes
            Last Post ryjoga
            by ryjoga
             
            Started by rdtdale, Today, 01:02 PM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by alifarahani, Today, 09:40 AM
            3 responses
            18 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X