• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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:
    https://ninjatrader.com/support/help...ghtsub=countif

    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 turbocing, Today, 03:21 AM
            0 responses
            2 views
            0 likes
            Last Post turbocing  
            Started by lakman184, Today, 03:04 AM
            0 responses
            2 views
            0 likes
            Last Post lakman184  
            Started by myrtienguyen, Today, 02:18 AM
            0 responses
            3 views
            0 likes
            Last Post myrtienguyen  
            Started by WHICKED, Yesterday, 09:03 PM
            1 response
            16 views
            0 likes
            Last Post Emma1
            by Emma1
             
            Started by oleotrim, Yesterday, 11:29 PM
            1 response
            11 views
            0 likes
            Last Post Emma1
            by Emma1
             
            Working...
            X