Advertisement
sygma1982

Twin Range Filter

May 17th, 2023
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © colinmck
  3.  
  4. //@version=5
  5.  
  6. strategy(title='Twin Range Filter', overlay=true)
  7.  
  8. source = input(defval=close, title='Source')
  9.  
  10. // Smooth Average Range
  11.  
  12. per1 = input.int(defval=27, minval=1, title='Fast period')
  13. mult1 = input.float(defval=1.5, minval=0.1, title='Fast range')
  14.  
  15. per2 = input.int(defval=55, minval=1, title='Slow period')
  16. mult2 = input.float(defval=2, minval=0.1, title='Slow range')
  17.  
  18. smoothrng(x, t, m) =>
  19.     wper = t * 2 - 1
  20.     avrng = ta.ema(math.abs(x - x[1]), t)
  21.     smoothrng = ta.ema(avrng, wper) * m
  22.     smoothrng
  23. smrng1 = smoothrng(source, per1, mult1)
  24. smrng2 = smoothrng(source, per2, mult2)
  25. smrng = (smrng1 + smrng2) / 2
  26.  
  27. // Range Filter
  28.  
  29. rngfilt(x, r) =>
  30.     rngfilt = x
  31.     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
  32.     rngfilt
  33. filt = rngfilt(source, smrng)
  34.  
  35. upward = 0.0
  36. upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
  37. downward = 0.0
  38. downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
  39.  
  40. hband = filt + smrng
  41. lband = filt - smrng
  42.  
  43. longCond = bool(na)
  44. shortCond = bool(na)
  45. longCond := source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0
  46. shortCond := source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0
  47.  
  48. CondIni = 0
  49. CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
  50.  
  51. long = longCond and CondIni[1] == -1
  52. short = shortCond and CondIni[1] == 1
  53.  
  54. // Plotting
  55.  
  56. plotshape(long, title='Long', text='Long', style=shape.labelup, textcolor=color.new(color.black, 0), size=size.tiny, location=location.belowbar, color=color.new(color.lime, 0))
  57. plotshape(short, title='Short', text='Short', style=shape.labeldown, textcolor=color.new(color.white, 0), size=size.tiny, location=location.abovebar, color=color.new(color.red, 0))
  58.  
  59. // Alerts
  60.  
  61. alertcondition(long, title='Long', message='Long')
  62. alertcondition(short, title='Short', message='Short')
  63.  
  64. longstring = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
  65. shortstring = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
  66.  
  67. ltos=input.text_area(title='Long Alert Message', defval='Long', confirm=false, group='Alert Messages', tooltip=longstring)
  68. stol=input.text_area(title='Short Alert Message', defval='Short', confirm=false, group='Alert Messages', tooltip=shortstring)
  69.  
  70. if long
  71.     alert(ltos)
  72. if short
  73.     alert(stol)
  74.  
  75.  
Tags: Range Filter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement