Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

looping an indicator over several bars

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

    looping an indicator over several bars

    Hi, trying to determine a rising condition over multiple bars. Rather than compare each set of bars manually I thought I would try a loop. But the loop does not seem to be working. So I imagine I am doing something wrong. The loop initiates with 0 comparing LinRegSlope1[0] > LinRegSlope1[1] then the next should increment [1] > [2] etcetera... The count never prints so it is not ever satisfying the condition. I hope someone can shed some light on this.

    private int loopBack =10;
    if (BarsInProgress > 20)
    {
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    for(int barsMin = 0, barsMax = lookBack; barsMin < barsMax; barsMin ++)
    {
    if (LinRegSlope1[barsMin] > LinRegSlope1[barsMin + 1])
    Count = Count +1;
    Print("Count: " + Count);
    }
    }
    }
    if ( Count > 3) LinRegSlopeRising = true; else LinRegSlopeRising = false;

    #2
    Hello set2win,

    Thanks for your post.

    I suspect you want BarsInProgress to actually be CurrentBar. BarsInProgress would identify which data series is calling the OnBarUpdate() and I doubt (and hope) you don't have 20 data series.

    The for loop has some syntax issues for(int barsMin = 0, barsMax = lookBack; barsMin < barsMax; barsMin ++) you are seperating the first parameter from the second with a comma instead of a semicolon. You would not assign a value in there like barsMax = lookBack, you could assign that ahead of the for loop or not use barMax at all. Perhaps something like this: for(int barsMin = 0; barsMin < LookBack; barsMin++)

    Where you have Count = Count+1; while that would work you can also write it this way: Count++;

    Comment


      #3
      I think I figured out something that seems to be working:
      I did eliminate the barsinprogress and just added if (CurrentBars[0] < 10) return;
      I did see the separation in barsMin and ++.
      So with some print statements I have checked some values and it now is performing I think as it should.
      And the backtesting seems to be doing what I hoped which is to check for so many bars back that the indicator is increasing. I can experiment with the lookback and the barCount to see what is best performance. The barsMax is to only check the last so many bars.

      barCount = 4;
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      for(int barsMin = 0; barsMin < lookBack; barsMin++)
      {
      if (LinRegSlope1[barsMin] > LinRegSlope1[barsMin + 1])
      {
      Count = Count +1;
      if ( Count > barCount) LinRegSlopeRising = true; else LinRegSlopeRising = false;
      }
      }
      Count = 0; /// resets the count
      }
      Last edited by set2win; 04-15-2021, 01:12 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      135 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      119 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      69 views
      0 likes
      Last Post PaulMohn  
      Working...
      X