title: Pinescript/TradingView: Set max pyramiding (multiple stacked entries of the same direction) but greater than 1
body: <p>I'm writing a study (not a strategy) with alerts on the lower 5-min TF that allow for multiple entries (and a single close) of the same direction. Ideally, a max of 3 longs or 3 shorts before closing, or otherwise keep waiting for opposite entry or a close. (note I am not looking for code that limits to 1 entry/exit at a time). This is what I have thus far from what web research has suggested, but for some reason it doesn't seem to work (in my charts, plots appear very sparingly) -- I think I have pinpointed it to the security() function condition based on a higher TF data. When isolating the pyramiding by itself, it seems fine. When isolating the security function to check whether the data is being passed in, it plots fine. Please let me know if there is an alternative max-pyramiding logic or how to get it working with the higher TF data. Thanks in advance, any help appreciated.</p>
  
  <pre><code>// == ORDER CONDITIONS ==
  longCond = security(tickerid,'60',someHFCond1)
  shortCond = security(tickerid,'60',someHFCond2)
  
  // Count your long short conditions for more control with Pyramiding
  
  sectionLongs = 0
  sectionLongs := nz(sectionLongs[1])
  
  sectionShorts = 0
  sectionShorts := nz(sectionShorts[1])
  
  if longCond
      sectionLongs := sectionLongs + 1
      sectionShorts := 0
  
  if shortCond
      sectionLongs := 0
      sectionShorts := sectionShorts + 1
  
  // Pyramiding
  pyrl = input(3,title="Max Pyramiding (stackable entries, default=3)",minval=1)
  longCondition = longCond and sectionLongs &lt;= pyrl 
  shortCondition = shortCond and sectionShorts &lt;= pyrl 
  
  // Get the price of the last opened long or short
  last_open_longCondition = na
  last_open_shortCondition = na
  last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  
  // Check if your last position was a long or a short
  last_longCondition = na
  last_shortCondition = na
  last_longCondition := longCondition ? time : nz(last_longCondition[1])
  last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  
  in_longCondition = last_longCondition &gt; last_shortCondition
  in_shortCondition = last_shortCondition &gt; last_longCondition
  
  // Take profit
  Ltakeprofit = last_open_longCondition * (1+profittarget)
  Stakeprofit = last_open_shortCondition * (1-profittarget)
  long_tp = high[1] &gt; Ltakeprofit and longCondition == 0 and in_longCondition  == 1 and not longCondition[1]
  short_tp = low[1] &lt; Stakeprofit and shortCondition == 0 and in_shortCondition == 1 and not shortCondition[1]
  
  // Create a single close for all the different closing conditions.
  long_close = long_tp or long_sl ? 1 : 0
  short_close = short_tp or short_sl ? 1 : 0
  
  // Get the time of the last close
  last_long_close = na
  last_long_close := long_close ? time : nz(last_long_close[1])
  last_short_close = na
  last_short_close := short_close ? time : nz(last_short_close[1])
  
  // Alerts &amp; Signals
  bton(b) =&gt; b ? 1 : 0
  plotshape(longCondition , title="buy alert", color=green, textcolor=green, transp=0, 
            style=shape.triangleup, location=location.belowbar, size=size.small,text="LONG",offset=0)
  plotshape(shortCondition, title="sell alert", color=red, textcolor=red, transp=0, 
            style=shape.triangledown, location=location.abovebar, size=size.small,text="SHORT",offset=0)
  plotshape(long_tp and last_longCondition &gt; nz(last_long_close[1]), text ="Close", title="Take Profit Long", style=shape.triangledown, 
     location=location.abovebar, color = green, size=size.tiny, editable = false, transp = 0,offset=0) 
  plotshape(short_tp and last_shortCondition &gt; nz(last_short_close[1]) , text ="Cover", title="Take Profit Short", style=shape.triangleup, 
     location=location.belowbar, color = red, size=size.tiny, editable = false, transp = 0,offset=0)
  </code></pre>