Advertisement
Al0rse

Range filter

Dec 17th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. //@version=5
  2. indicator(title='Range Filter', overlay=true)
  3.  
  4. // Source
  5. src = input(defval=close, title='Source')
  6. per = input.int(defval=100, minval=1, title='Sampling Period')
  7. mult = input.float(defval=3.0, minval=0.1, title='Range Multiplier')
  8.  
  9. // Smooth Average Range
  10. smoothrng(x, t, m) =>
  11. wper = t * 2 - 1
  12. avrng = ta.ema(math.abs(x - x[1]), t)
  13. smoothrng = ta.ema(avrng, wper) * m
  14. smoothrng
  15. smrng = smoothrng(src, per, mult)
  16.  
  17. // Range Filter
  18. rngfilt(x, r) =>
  19. rngfilt = x
  20. rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
  21. rngfilt
  22. filt = rngfilt(src, smrng)
  23.  
  24. // Filter Direction
  25. upward = 0.0
  26. upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
  27. downward = 0.0
  28. downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
  29.  
  30. // Target Bands
  31. hband = filt + smrng
  32. lband = filt - smrng
  33.  
  34. // Colors
  35. filtcolor = upward > 0 ? color.lime : downward > 0 ? color.red : na
  36. filtplot = plot(filt, color=filtcolor, linewidth=3, title='Range Filter')
  37.  
  38. // Target
  39. hbandplot = plot(hband, color=color.new(color.aqua, 100), title='High Target')
  40. lbandplot = plot(lband, color=color.new(color.fuchsia, 100), title='Low Target')
  41.  
  42. // Fills
  43. fill(hbandplot, filtplot, color=color.new(color.fuchsia, 90), title='High Target Range')
  44. fill(lbandplot, filtplot, color=color.new(color.fuchsia, 90), title='Low Target Range')
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement