title: Trying to produce variable length horizontal lines based on variable numbers of candles in Pine
body: <p>After hours of working with arrays and loops hoping that would solve my problem I'm completely stumped on how to go about what I want with <code>pine</code> and yet what I want seems like it should be almost trivial so I'm hoping someone with pine experience can help.</p>
  <p>The code attached has two approaches, one with arrays and one without but neither could I figure out how to do what I want so they only get so far, so, if you think you are seeing duplicate functionality, you are, obviously that has to be cleaned up once I have a path forward.</p>
  <p>What I am trying to do is plot a line showing the highest point and lowest point of the candles for the length of candles where <code>isSideways</code> remains true and then not plot anything when <code>isSideways</code> is false. In other words just two straight horizontal lines in the shaded areas. Once that's figured out I'll want to draw lines a percentage of the way between those lines but that shouldn't be an issue once the first two lines are drawn.</p>
  <p>It is trivial to draw a path that follows the candles, also to draw a horizontal line however to draw a horizontal line segment that fits in a limited but variable length range that's based on the highest or lowest values of those variable number of candles, that's proving to be considerably more difficult than I thought would be the case.</p>
  <p>I'm using <code>barssince()</code> which gets me part way there but not all the way.</p>
  <p>Any help greatly appreciated.</p>
  <pre><code>//@version=4
  study(&quot;Sideways Channel Visual&quot;, max_bars_back=4999)
  
  sidewaysBbSource      = input(close, &quot;SMA Source&quot;              , type=input.source  ,                                        group=&quot;Sideways Detection&quot;)
  sidewaysBbLength      = input( 50  , &quot;SMA Length&quot;              , type=input.integer , minval=1      ,                        group=&quot;Sideways Detection&quot;)
  sidewaysBbrLen        = input( 21  , &quot;Std Dev Signal Smoothing&quot;, type=input.integer , minval=1      , maxval=50,             group=&quot;Sideways Detection&quot;)
  sidewaysBbMult        = input(  4.0, &quot;Std Dev&quot;                 , type=input.float   , minval=0.001  , maxval=50, step=0.001, group=&quot;Sideways Detection&quot;)
  sidewaysBbrStdThresh  = input( 0.08, &quot;Std Dev threshold&quot;       , type=input.float   ,                            step=0.01 , group=&quot;Sideways Detection&quot;)
  
  // Sideways indicator
  sidewaysBasis   = sma(sidewaysBbSource, sidewaysBbLength)
  sidewaysStdDev2 = sidewaysBbMult * stdev(sidewaysBbSource, sidewaysBbLength)
  sidewaysUpper   = sidewaysBasis + sidewaysStdDev2
  sidewaysLower   = sidewaysBasis - sidewaysStdDev2
  sidewaysBbr     = (sidewaysBbSource - sidewaysLower)/(sidewaysUpper - sidewaysLower)
  sidewaysStdDev  = stdev(sidewaysBbr, sidewaysBbrLen)
  isSideways      = (sidewaysBbr &gt; 0.0 and sidewaysBbr &lt; 1.0) and sidewaysStdDev &lt;= sidewaysBbrStdThresh
  
  barsSinceNotSideways = barssince(not isSideways)
  if barsSinceNotSideways == 0
      barsSinceNotSideways := 1 // highest/lowest will fail on 0.
  
  hLevel = array.new_float(1, highest(high, barsSinceNotSideways))
  lLevel = array.new_float(1, lowest (low , barsSinceNotSideways))
  
  hNextLevel() =&gt;
      _newLevel = isSideways ? array.get(hLevel, 0) : na
      array.set(hLevel, 0, _newLevel)
      _newLevel
  
  lNextLevel() =&gt;
      _newLevel = isSideways ? array.get(lLevel, 0) : na
      array.set(lLevel, 0, _newLevel)
      _newLevel
  
  L  = lowest(high, barsSinceNotSideways)
  H  = highest(low, barsSinceNotSideways)
  LB = lowestbars(high, barsSinceNotSideways)
  HB = highestbars(low, barsSinceNotSideways)
  
  if not isSideways
      L := na
      H := na
      LB := na
      HB := na
      L
      H
      LB
      HB
  
  bgcolor(title=&quot;Sidways&quot;, color=isSideways ? color.rgb(236, 64, 122, 90) : color.new(color.white, 100))
  
  plot(hNextLevel(), title=&quot;Level High&quot;, color=color.red               , style=plot.style_cross, display=display.none)
  plot(lNextLevel(), title=&quot;Level Low&quot; , color=color.blue              , style=plot.style_cross, display=display.none)
  plot(H           , &quot;highest&quot;         , color=color.new(color.red , 0), style=plot.style_cross, display=display.none)
  plot(L           , &quot;Lowest&quot;          , color=color.new(color.blue, 0), style=plot.style_cross, display=display.none)
  plot(HB          , &quot;highest Bars&quot;    , color=color.new(color.red , 0), style=plot.style_cross, display=display.none)
  plot(LB          , &quot;Lowest Bars&quot;     , color=color.new(color.blue, 0), style=plot.style_cross, display=display.none)
  
  </code></pre>