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

Count how many times a condition was true in a row

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

    Count how many times a condition was true in a row

    hi, trying to create a strategy and i need to figure out how many times a indicator was a certain value in a row.

    ex1:
    indicator value =10
    indicator value =9
    indicator value =8
    indicator value =10
    indicator value =10
    indicator value =10

    output for the above should be 3

    ex2:
    indicator value =1
    indicator value =2
    indicator value =10
    indicator value =10
    indicator value =10
    indicator value =10

    output for the above should be 4

    ex1:
    indicator value =10
    indicator value =10
    indicator value =10
    indicator value =10
    indicator value =10
    indicator value =10

    output for the above should be 6

    i tried this but it is not working:

    if (indicatorvalue<>10, 1))
    { // Reset counter }
    else { indicatorvalue +1 }

    #2
    Hello calhawk01,

    Thanks for your note.

    I have made a small example of how to find the longest streak in the SMA(19) of the last 10 bars.
    Code:
    int lookBackPeriod = 10;
    int longestStreak = 0;
    double longestStreakValue = 0;
    
    Print("");
    
    for (int i=0; i<lookBackPeriod; i++)
    {
    int j = i+1;
    //Print(Math.Round(SMA(19)[i], 1) +" - "+ Math.Round(SMA(19)[j], 1));
    while (Math.Round(SMA(19)[i], 1) == Math.Round(SMA(19)[j], 1) && j<lookBackPeriod)
    {
    /*Print(i+" | "+j+" | "+Math.Round(SMA(19)[i], 2) + " - " + Math.Round(SMA(19)[j], 2)
    + " / " + SMA(19)[i]+" - "+SMA(19)[j]);*/
    if ((j-i) > longestStreak)
    {
    longestStreak = j-i;
    longestStreakValue = SMA(19)[i];
    }
    j++;
    }
    }
    Print(longestStreak + " - " + longestStreakValue);
    This will look for the longest streak and return how long it was as well as the price that held the streak.

    This currently rounds the price of the SMA(19) to the nearest tenth decimal. This is so I could have data that is only sometimes the same and not always different. (1636.37769 will become 1636.4)

    This is just an example so you will want to edit this to reflect the proper indicator as well as the proper amount of decimals you are comparing.

    Math.Round(SMA(19)[0], 2) would round to two decimals.


    Please let me know if you have questions about this.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      thank you for taking the time Chelsea.

      however, having a X lookback period will not work for what i'm trying to do.

      on close[0] if the last 5 bars in a row = X, on close[0] I want an output of 5.
      on any close[0] if the last 10 bars in a row = X, on close[0] I want an output of 10.
      on any close[0] if the last 8 bars in a row = X, on close[0] I want an output of 8

      ie:

      count if an indicator value =10 in a row
      ex1)
      close[0]=10
      close[-1]=9
      close[-2]=9
      close[-3]=9
      close[-4]=9

      output here would be 0

      ex2)
      count if an indicator value =10 in a row

      close[0]=10
      close[-1]=10
      close[-2]=10
      close[-3]=9
      close[-4]=9

      output here would be 3, since at close[0] the last three bars (including close[0]) were also 10


      the problem with defining a lookback period is that, if we set a lookback period of 10, it will not give me the correct answer, even if the 11th and 12th bars were also = 10. what i'm trying to identify is ''strength'' of my indicator, how strong is it on close[0]
      Last edited by staycool3_a; 06-06-2013, 03:11 PM.

      Comment


        #4
        Hi calhawk01,

        So to clarify, you like to compare the value of the current close to the previous 5 closes. If there consecutive closes with the same value, print the number of consecutive values. Is this correct?

        If so, please try the following:
        Code:
        int lookBackPeriod = 5;
        int tempStreak = 0;
        
        Print("");
        int i = 1;
        
        while (Close[0] == Close[i] && i<=lookBackPeriod)
        {
        tempStreak++;
        i++;
        }
        
        Print(tempStreak);

        Please let me know if this is still not quite what you are looking for.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by calhawk01 View Post
          if (indicatorvalue<>10, 1))
          { // Reset counter }
          else { indicatorvalue +1 }
          What exactly is the line that I have marked in red, supposed to mean? It does not look like any C# syntax that I have ever seen. What kind of error message does it return?

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by rhyminkevin, Today, 04:58 PM
          3 responses
          47 views
          0 likes
          Last Post Anfedport  
          Started by iceman2018, Today, 05:07 PM
          0 responses
          5 views
          0 likes
          Last Post iceman2018  
          Started by lightsun47, Today, 03:51 PM
          0 responses
          7 views
          0 likes
          Last Post lightsun47  
          Started by 00nevest, Today, 02:27 PM
          1 response
          14 views
          0 likes
          Last Post 00nevest  
          Started by futtrader, 04-21-2024, 01:50 AM
          4 responses
          50 views
          0 likes
          Last Post futtrader  
          Working...
          X