Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © colinmck
- //@version=5
- strategy(title='Twin Range Filter', overlay=true)
- source = input(defval=close, title='Source')
- // Smooth Average Range
- per1 = input.int(defval=27, minval=1, title='Fast period')
- mult1 = input.float(defval=1.5, minval=0.1, title='Fast range')
- per2 = input.int(defval=55, minval=1, title='Slow period')
- mult2 = input.float(defval=2, minval=0.1, title='Slow range')
- smoothrng(x, t, m) =>
- wper = t * 2 - 1
- avrng = ta.ema(math.abs(x - x[1]), t)
- smoothrng = ta.ema(avrng, wper) * m
- smoothrng
- smrng1 = smoothrng(source, per1, mult1)
- smrng2 = smoothrng(source, per2, mult2)
- smrng = (smrng1 + smrng2) / 2
- // Range Filter
- rngfilt(x, r) =>
- rngfilt = x
- 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
- rngfilt
- filt = rngfilt(source, smrng)
- upward = 0.0
- upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
- downward = 0.0
- downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
- hband = filt + smrng
- lband = filt - smrng
- longCond = bool(na)
- shortCond = bool(na)
- longCond := source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0
- shortCond := source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0
- CondIni = 0
- CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
- long = longCond and CondIni[1] == -1
- short = shortCond and CondIni[1] == 1
- // Plotting
- 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))
- 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))
- // Alerts
- alertcondition(long, title='Long', message='Long')
- alertcondition(short, title='Short', message='Short')
- longstring = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
- shortstring = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
- ltos=input.text_area(title='Long Alert Message', defval='Long', confirm=false, group='Alert Messages', tooltip=longstring)
- stol=input.text_area(title='Short Alert Message', defval='Short', confirm=false, group='Alert Messages', tooltip=shortstring)
- if long
- alert(ltos)
- if short
- alert(stol)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement