xmd79

Twin Range Filter (Long) + % Take Profit and Stop Loss by Nussara

Sep 26th, 2021
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1.  
  2. //@version=4
  3. //Thank you original script from colinmck "Twin Range Filter"
  4.  
  5. study(title="Twin Range Filter (Long) + % Take Profit and Stop Loss by Nussara", overlay=true)
  6. lineExpiryBars = 100000
  7.  
  8. takeprofit = input(title="Take Profit %", type=input.float, step=0.1, defval=3)/100
  9. stoploss = input(title="Stop Loss %", type=input.float, step=0.1, defval=2)/100
  10. source = input(defval=close, title="Source")
  11.  
  12. per1 = input(defval=27, minval=1, title="Fast period")
  13. mult1 = input(defval=1.6, minval=0.1, title="Fast range")
  14.  
  15. per2 = input(defval=55, minval=1, title="Slow period")
  16. mult2 = input(defval=2, minval=0.1, title="Slow range")
  17.  
  18. smoothrng(x, t, m) =>
  19. wper = t * 2 - 1
  20. avrng = ema(abs(x - x[1]), t)
  21. smoothrng = 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 :
  32. x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
  33. rngfilt
  34. filt = rngfilt(source, smrng)
  35.  
  36. upward = 0.0
  37. upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
  38. downward = 0.0
  39. downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
  40.  
  41. hband = filt + smrng
  42. lband = filt - smrng
  43.  
  44. longCond = bool(na)
  45. shortCond = bool(na)
  46. longCond := source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0
  47. shortCond := source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0
  48.  
  49. CondIni = 0
  50. CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
  51. buySignal = longCond and CondIni[1] == -1
  52. var savedLevel = float(na)
  53. var plotLine = false
  54. longStop = savedLevel * (1 - stoploss)
  55. longTake = savedLevel * (1 + takeprofit)
  56. startCondition = buySignal
  57. endConditionTP = high> longTake
  58. endConditionSL = close< longStop
  59. startEvent = not plotLine and startCondition
  60.  
  61. endEventTP = plotLine and (endConditionTP or barssince(startEvent) > lineExpiryBars)
  62. endEventSL = plotLine and (endConditionSL or barssince(startEvent) > lineExpiryBars)
  63. plotLine := startEvent or (plotLine and not endEventTP and not endEventSL)
  64.  
  65. if plotLine and not plotLine[1]
  66. savedLevel := close
  67. plotLine := startEvent or (plotLine and not endEventTP and not endEventSL)
  68. if plotLine and not plotLine[1]
  69. longStop := close
  70.  
  71. plot(plotLine ? longStop : na, color = color.red, style = plot.style_circles)
  72. plotLine := startEvent or (plotLine and not endEventTP and not endEventSL)
  73. if plotLine and not plotLine[1]
  74. longTake := close
  75.  
  76. plot(plotLine ? longTake : na, color = color.purple, style = plot.style_circles)
  77. plotshape(startEvent, title="Long", text="Long", style=shape.labelup, textcolor=color.black, size=size.tiny, location=location.belowbar, color=color.lime, transp=0)
  78. plotshape(endEventTP, style=shape.labeldown, location=location.abovebar, color=color.purple, size=size.tiny, title="TP", text="TP", textcolor=color.white)
  79. plotshape(endEventSL, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.tiny, title="SL", text="SL", textcolor=color.white)
  80.  
  81. alertcondition(condition=startEvent, title="Long", message='Long')
  82. alertcondition(condition=endEventTP, title="Take Profit", message='Take Profit')
  83. alertcondition(condition=endEventSL, title="Stop Loss", message='Stop Loss')
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment