xmd79

Support Resistance Interactive

Nov 30th, 2021
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © LonesomeTheBlue
  3.  
  4. //@version=5
  5. indicator("Support Resistance Interactive", overlay=true)
  6. srnum = input.int(defval = 5, title = "Number of Support/Resistance", minval = 1, maxval = 5, confirm=true, group = "Setup", tooltip = "you can set the number of S/R levels to be shown")
  7. threshold = input.float(defval = 1., title = "% Threshold", minval = 0, group = "Setup", tooltip = "it's calculated using % of the distance between highest/lowest in last 300 bars")
  8.  
  9. float thold = (ta.highest(300) - ta.lowest(300)) * math.max(threshold, 0.1) / 100.
  10. // get SR levels interactively and assign it to the array
  11. var float [] srarray = array.from(
  12. srnum > 0 ? input.price(0.0, title="S/R level 1", confirm=true, group = "Setup") : na,
  13. srnum > 1 ? input.price(0.0, title="S/R level 2", confirm=true, group = "Setup") : na,
  14. srnum > 2 ? input.price(0.0, title="S/R level 3", confirm=true, group = "Setup") : na,
  15. srnum > 3 ? input.price(0.0, title="S/R level 4", confirm=true, group = "Setup") : na,
  16. srnum > 4 ? input.price(0.0, title="S/R level 5", confirm=true, group = "Setup") : na)
  17.  
  18. // colors and labels
  19. supportcol = input.color(defval = color.rgb(0, 255, 0, 50), title = "Support/Resistance", inline ="srcol", group = "Colors")
  20. resistancecol = input.color(defval = color.rgb(255, 0, 0, 50), title = "", inline ="srcol", group = "Colors")
  21. notrcol = input.color(defval = color.rgb(100, 100, 100, 50), title = "", inline ="srcol", group = "Colors")
  22. showlabel = input.bool(defval = true, title = "Label", inline = "labs", group = "Colors")
  23. labelloc = input.int(defval = 40, title = "Location +-", inline = "labs", group = "Colors")
  24. supporttcol = input.color(defval = color.white, title = "Text Color", inline ="srtcol", group = "Colors")
  25. resistancetcol = input.color(defval = color.white, title = "", inline ="srtcol", group = "Colors")
  26. notrtcol = input.color(defval = color.white, title = "", inline ="srtcol", group = "Colors")
  27.  
  28. // variables for the alert
  29. supportBroken = -1
  30. resistanceBroken = -1
  31. priceisSRzone = -1
  32.  
  33. //run on the last bar
  34. if barstate.islast
  35. var box [] srzones = array.new_box(5, na)
  36. var label [] plabels = array.new_label(5, na)
  37. for x = 0 to 4
  38. // no more SR levels?
  39. if na(array.get(srarray, x))
  40. break
  41. // draw SR zones
  42. box.delete(array.get(srzones, x))
  43. label.delete(array.get(plabels, x))
  44. col = close > array.get(srarray, x) + thold ? supportcol : close < array.get(srarray, x) - thold ? resistancecol : notrcol
  45. tcol = close > array.get(srarray, x) + thold ? supporttcol : close < array.get(srarray, x) - thold ? resistancetcol : notrtcol
  46. array.set(srzones, x, box.new( bar_index - 280, array.get(srarray, x) + thold, bar_index, array.get(srarray, x) - thold,
  47. bgcolor = col,
  48. border_width = 0,
  49. extend = extend.right))
  50.  
  51. //show labels
  52. if showlabel
  53. array.set(plabels, x, label.new(bar_index + labelloc, array.get(srarray, x), text = "SR: " + str.tostring(x+1) +
  54. " Upper: " + str.tostring(math.round_to_mintick(array.get(srarray, x) + thold)) +
  55. " Lower: " + str.tostring(math.round_to_mintick(array.get(srarray, x) - thold)),
  56. color = col,
  57. textcolor = tcol))
  58. // check for the alerts
  59. if close[1] <= array.get(srarray, x) + thold and close > array.get(srarray, x) + thold
  60. resistanceBroken := x
  61. if close[1] >= array.get(srarray, x) - thold and close < array.get(srarray, x) - thold
  62. supportBroken := x
  63. if close >= array.get(srarray, x) - thold and close <= array.get(srarray, x) + thold
  64. priceisSRzone := x
  65.  
  66. // alerts
  67. enablesrbroken = input.bool(defval = true, title = "SR Broken Alert", inline = "srb", group = "Alerts")
  68. supporsrbrknfreq = input.string(defval = alert.freq_once_per_bar, title = "", options = [alert.freq_once_per_bar, alert.freq_once_per_bar_close], inline = "srb", group = "Alerts")
  69. enablepiz = input.bool(defval = true, title = "Price in SR Zone Alert", inline = "piz", group = "Alerts")
  70. pizfreq = input.string(defval = alert.freq_once_per_bar_close, title = "", options = [alert.freq_once_per_bar, alert.freq_once_per_bar_close], inline = "piz", group = "Alerts")
  71.  
  72. if enablesrbroken
  73. if supportBroken >= 0
  74. alert("Support Broken, Close :" + str.tostring(close) +
  75. " Support :" + str.tostring(math.round_to_mintick(array.get(srarray, supportBroken) - thold)),
  76. supporsrbrknfreq)
  77. if resistanceBroken >= 0
  78. alert("Resistance Broken, Close :" + str.tostring(close) +
  79. " Resistance :" + str.tostring(math.round_to_mintick(array.get(srarray, resistanceBroken) + thold)),
  80. supporsrbrknfreq)
  81. if enablepiz and priceisSRzone >= 0
  82. alert("Price in SR Zone, Close :" + str.tostring(close) +
  83. " Upper Band :" + str.tostring(math.round_to_mintick(array.get(srarray, priceisSRzone)) + thold) +
  84. " Lower Band :" + str.tostring(math.round_to_mintick(array.get(srarray, priceisSRzone)) - thold),
  85. pizfreq)
  86.  
Advertisement
Add Comment
Please, Sign In to add comment