title: Tradingview Occasional False Alarms
body: <p>There is an indicator that I have shared the codes below. The alarms I set are working incorrectly. It can send fake signals without showing &quot;buy&quot; or &quot;sell&quot; on the screen. How can we make this work only after typing buy and sell on the screen and the candle is closed?
  I want the buy alarm to work only when buy appears on the screen, and the sell alarm when sell appears. I don't want broken alarms generated in between.</p>
  <pre><code>
  
  //Original Script &gt; @DonovanWall
  
  // Actual Version &gt; @guikroth
  
  
  //////////////////////////////////////////////////////////////////////////
  // Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
  //////////////////////////////////////////////////////////////////////////
  
  
  study(title=&quot;Range Filter  5min&quot;, overlay=true)
  
  // Source
  
  src = input(defval=close, title=&quot;Source&quot;)
  
  // Sampling Period
  // Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
  
  per = input(defval=100, minval=1, title=&quot;Sampling Period&quot;)
  
  // Range Multiplier
  
  mult = input(defval=3.0, minval=0.1, title=&quot;Range Multiplier&quot;)
  
  // Smooth Average Range
  
  smoothrng(x, t, m) =&gt;
      wper = t * 2 - 1
      avrng = ema(abs(x - x[1]), t)
      smoothrng = ema(avrng, wper) * m
      smoothrng
  smrng = smoothrng(src, per, mult)
  
  // Range Filter
  
  rngfilt(x, r) =&gt;
      rngfilt = x
      rngfilt := x &gt; nz(rngfilt[1]) ? x - r &lt; nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : 
         x + r &gt; nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
      rngfilt
  filt = rngfilt(src, smrng)
  
  // Filter Direction
  
  upward = 0.0
  upward := filt &gt; filt[1] ? nz(upward[1]) + 1 : filt &lt; filt[1] ? 0 : nz(upward[1])
  downward = 0.0
  downward := filt &lt; filt[1] ? nz(downward[1]) + 1 : filt &gt; filt[1] ? 0 : nz(downward[1])
  
  // Target Bands
  
  hband = filt + smrng
  lband = filt - smrng
  
  // Colors
  
  filtcolor = upward &gt; 0 ? color.lime : downward &gt; 0 ? color.red : color.orange
  barcolor = src &gt; filt and src &gt; src[1] and upward &gt; 0 ? color.lime : 
     src &gt; filt and src &lt; src[1] and upward &gt; 0 ? color.green : 
     src &lt; filt and src &lt; src[1] and downward &gt; 0 ? color.red : 
     src &lt; filt and src &gt; src[1] and downward &gt; 0 ? color.maroon : color.orange
  
  filtplot = plot(filt, color=filtcolor, linewidth=3, title=&quot;Range Filter&quot;)
  
  // Target
  
  hbandplot = plot(hband, color=color.aqua, transp=100, title=&quot;High Target&quot;)
  lbandplot = plot(lband, color=color.fuchsia, transp=100, title=&quot;Low Target&quot;)
  
  // Fills
  
  fill(hbandplot, filtplot, color=color.aqua, title=&quot;High Target Range&quot;)
  fill(lbandplot, filtplot, color=color.fuchsia, title=&quot;Low Target Range&quot;)
  
  // Bar Color
  
  barcolor(barcolor)
  
  // Break Outs
  
  longCond = close and bool(na)
  shortCond = close and bool(na)
  longCond := src &gt; filt and src &gt; src[1] and upward &gt; 0 or 
     src &gt; filt and src &lt; src[1] and upward &gt; 0
  shortCond := src &lt; filt and src &lt; src[1] and downward &gt; 0 or 
     src &lt; filt and src &gt; src[1] and downward &gt; 0
  
  CondIni = 0
  CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
  longCondition = longCond and CondIni[1] == -1
  shortCondition = shortCond and CondIni[1] == 1
  
  //Alerts
  
  plotshape(longCondition, title=&quot;Buy Signal&quot;, text=&quot;BUY&quot;, textcolor=color.white, style=shape.labelup, size=size.normal, location=location.belowbar, color=color.green, transp=0)
  plotshape(shortCondition, title=&quot;Sell Signal&quot;, text=&quot;SELL&quot;, textcolor=color.white, style=shape.labeldown, size=size.normal, location=location.abovebar, color=color.red, transp=0)
  
  alertcondition(longCondition, title=&quot;Buy Alert&quot;, message=&quot;BUY&quot;)
  alertcondition(shortCondition, title=&quot;Sell Alert&quot;, message=&quot;SELL&quot;)
  
  //For use like Strategy, 
  
  //1. Change the word  &quot;study&quot; for &quot;strategy&quot; at the top
  //2.  Remove the &quot;//&quot; below
  
  //strategy.entry( id = &quot;Long&quot;, long = true, when = longCondition )
  //strategy.close( id = &quot;Long&quot;, when = shortCondition )```
  </code></pre>