Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Lookback Period for Set of Conditions

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

    Lookback Period for Set of Conditions

    I'm trying to write code that executes when a set conditions are met in the present after a set of conditions were met in the past, i.e.: within a given look back period.

    For example

    if (X is rising and Y > 0 and etc...) within the last 100 bars
    && (X is falling and Z < 0 and etc...)
    then {draw vertical line}

    How do incorporate that "within the last 100 bars" ​statement into the code?

    In other words, when a set of conditions (ex: X is rising and Y > 0 and etc...) was met in the past (ex: 100 bars ago) and different set of conditions is met in the present (ex: X is falling and Z < 0 and etc...), and then the code executes (ex: draw vertical line), how can I code how long the present conditions 'look back to see' if the past conditions were met?


    Thank you, NJ Team and Community.

    #2
    Hello Tagliareni,

    You could use CountIf for that type of use case. That method returns the number of times a condition is true within a lookback period.

    Comment


      #3
      I'm still having trouble. Could you please give me an example using the following code snippets?

      if(
      ((AAA[0] > 0) && (IsRising(BBB) == true)) "within 100 bars"
      &&
      ((AAA[0] < 0) && (IsRising(CCC) == true)) "on current bar"
      )

      {
      Draw.VerticalLine(this, @"Down", 0, Brushes.Red, DashStyleHelper.Dot, 2);
      }


      Based on what you've said, I think it would look something like:

      if(
      CountIf(() => ((AAA[0] > 0) && (IsRising(BBB) == true)), 100
      ((AAA[0] < 0) && (IsRising(CCC) == true))
      )

      {
      Draw.VerticalLine(this, @"Down", 0, Brushes.Red, DashStyleHelper.Dot, 2);
      }

      ...but my computer hates this. Please advise.

      Thank you very much for your help.​

      Comment


        #4
        Hello Tagliareni,

        There is a sample on the page that I linked which shows how to use that method. It uses 10 bars as the lookback, you could change that to 100. The sample also checks if greater than 8 occurrences happened, if you need to know if only 1 happened you could use > 1. You would replace the Close[0] > Open[0] with your own conditions.

        Code:
        if(CountIf(() => Close[0] > Open[0], 100) > 1)

        Comment


          #5
          Thank you! That got it humming.

          The condition referencing the 8th bar was throwing me off, but it's intriguing and I'm looking into it.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          63 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          139 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X