Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- //Thank you original script from colinmck "Twin Range Filter"
- study(title="Twin Range Filter (Long) + % Take Profit and Stop Loss by Nussara", overlay=true)
- lineExpiryBars = 100000
- takeprofit = input(title="Take Profit %", type=input.float, step=0.1, defval=3)/100
- stoploss = input(title="Stop Loss %", type=input.float, step=0.1, defval=2)/100
- source = input(defval=close, title="Source")
- per1 = input(defval=27, minval=1, title="Fast period")
- mult1 = input(defval=1.6, minval=0.1, title="Fast range")
- per2 = input(defval=55, minval=1, title="Slow period")
- mult2 = input(defval=2, minval=0.1, title="Slow range")
- smoothrng(x, t, m) =>
- wper = t * 2 - 1
- avrng = ema(abs(x - x[1]), t)
- smoothrng = 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]
- buySignal = longCond and CondIni[1] == -1
- var savedLevel = float(na)
- var plotLine = false
- longStop = savedLevel * (1 - stoploss)
- longTake = savedLevel * (1 + takeprofit)
- startCondition = buySignal
- endConditionTP = high> longTake
- endConditionSL = close< longStop
- startEvent = not plotLine and startCondition
- endEventTP = plotLine and (endConditionTP or barssince(startEvent) > lineExpiryBars)
- endEventSL = plotLine and (endConditionSL or barssince(startEvent) > lineExpiryBars)
- plotLine := startEvent or (plotLine and not endEventTP and not endEventSL)
- if plotLine and not plotLine[1]
- savedLevel := close
- plotLine := startEvent or (plotLine and not endEventTP and not endEventSL)
- if plotLine and not plotLine[1]
- longStop := close
- plot(plotLine ? longStop : na, color = color.red, style = plot.style_circles)
- plotLine := startEvent or (plotLine and not endEventTP and not endEventSL)
- if plotLine and not plotLine[1]
- longTake := close
- plot(plotLine ? longTake : na, color = color.purple, style = plot.style_circles)
- plotshape(startEvent, title="Long", text="Long", style=shape.labelup, textcolor=color.black, size=size.tiny, location=location.belowbar, color=color.lime, transp=0)
- plotshape(endEventTP, style=shape.labeldown, location=location.abovebar, color=color.purple, size=size.tiny, title="TP", text="TP", textcolor=color.white)
- plotshape(endEventSL, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.tiny, title="SL", text="SL", textcolor=color.white)
- alertcondition(condition=startEvent, title="Long", message='Long')
- alertcondition(condition=endEventTP, title="Take Profit", message='Take Profit')
- alertcondition(condition=endEventSL, title="Stop Loss", message='Stop Loss')
Advertisement
Add Comment
Please, Sign In to add comment