xmd79

Multi-Timeframe Support & Resistance

Sep 13th, 2022
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.40 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. // © jordanfray
  3.  
  4. //@version=5
  5. indicator(title="Multi-Timeframe Support & Resistance", overlay=true, max_bars_back=5000)
  6. import jordanfray/threengine_global_automation_library/74 as bot
  7.  
  8. // Colors - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9. green = color.new(#2DBD85,0)
  10. lightGreen = color.new(#2DBD85,80)
  11. red = color.new(#E02A4A,0)
  12. lightRed = color.new(#E02A4A,80)
  13. white = color.new(#ffffff,0)
  14. transparent = color.new(#ffffff,100)
  15.  
  16.  
  17. // Strategy Settings - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  18. supportLookBack = input.int(defval=22, title = "Support Look Back", group="Support & Resistance Settings")
  19. supportLookAhead = input.int(defval=26, title = "Support Look Ahead", group="Support & Resistance Settings")
  20. resistanceLookBack = input.int(defval=30, title = "Reistance Look Back", group="Support & Resistance Settings")
  21. resistanceLookAhead = input.int(defval=27, title = "Reistance Look Ahead", group="Support & Resistance Settings")
  22.  
  23. timeframe1 = input.timeframe(defval="60", title="Timeframe 1", group="Support & Resistance 1")
  24. enableSrRanges1 = input.bool(defval=true, title="Enable Support & Resistance Ranges", group="Support & Resistance 1")
  25. supportRange1 = input.float(defval=.25, title="     Support Range (%)       ", inline="support1", group="Support & Resistance 1")
  26. supportRange1Color = input.color(defval=green, title="", inline="support1", group="Support & Resistance 1")
  27. resistanceRange1 = input.float(defval=.25, title="     Resistance Range (%)    ", inline="resistance1", group="Support & Resistance 1")
  28. resistanceRange1Color = input.color(defval=red, title="", inline="resistance1", group="Support & Resistance 1")
  29.  
  30. timeframe2 = input.timeframe(defval="120", title="Timeframe 2", group="Support & Resistance 2")
  31. enableSrRanges2 = input.bool(defval=true, title="Enable Support & Resistance Ranges", group="Support & Resistance 2")
  32. supportRange2 = input.float(defval=.25, title="     Support Range (%)       ", inline="support2", group="Support & Resistance 2")
  33. supportRange2Color = input.color(defval=green, title="", inline="support2", group="Support & Resistance 2")
  34. resistanceRange2 = input.float(defval=.25, title="     Resistance Range (%)    ", inline="resistance2", group="Support & Resistance 2")
  35. resistanceRange2Color = input.color(defval=red, title="", inline="resistance2", group="Support & Resistance 2")
  36.  
  37. // Support and Resistance Functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  38. getSupport(lookback, lookahead) =>
  39. support = fixnan(ta.pivotlow(lookback, lookahead)[1])
  40.  
  41. support
  42.  
  43. getResistance(lookback, lookahead) =>
  44. resistance = fixnan(ta.pivothigh(lookback, lookahead)[1])
  45.  
  46. resistance
  47.  
  48.  
  49. // Support and Resistance 1
  50. support1 = request.security(syminfo.ticker, timeframe1, getSupport(supportLookBack, supportLookAhead))
  51. resistance1 = request.security(syminfo.ticker, timeframe1, getResistance(resistanceLookBack, resistanceLookAhead))
  52. var float mostRecentResistance1 = na
  53. var float mostRecentSupport1 = na
  54. mostRecentResistance1 := ta.change(resistance1) ? resistance1 : mostRecentResistance1
  55. mostRecentSupport1 := ta.change(support1) ? support1 : mostRecentSupport1
  56. var line resistance1Line = na
  57. var line support1Line = na
  58. line.delete(resistance1Line)
  59. line.delete(support1Line)
  60. resistance1Line := line.new(x1=bar_index[10], y1=mostRecentResistance1, x2=bar_index, y2=mostRecentResistance1, color=resistanceRange1Color, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  61. support1Line := line.new(x1=bar_index[10], y1=mostRecentSupport1, x2=bar_index, y2=mostRecentSupport1, color=supportRange1Color, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  62. resistance1Price = plot(series=mostRecentResistance1, title="Resistance Price", color=red, editable=false, display=display.price_scale)
  63. support1Price = plot(series=mostRecentSupport1, title="Support Price", color=green, editable=false, display=display.price_scale)
  64.  
  65. var label support1label = na
  66. var label resistance1label = na
  67. label.delete(support1label)
  68. label.delete(resistance1label)
  69. support1label := label.new(x=bar_index+30, y=mostRecentSupport1, text=bot.convertTimeframeToString(timeframe1), color=supportRange1Color, textcolor=white, style=label.style_label_down)
  70. resistance1label := label.new(x=bar_index+30, y=mostRecentResistance1, text=bot.convertTimeframeToString(timeframe1), color=resistanceRange1Color, textcolor=white, style=label.style_label_down)
  71.  
  72.  
  73. // Support and Resistance 2
  74. support2 = request.security(syminfo.ticker, timeframe2, getSupport(supportLookBack, supportLookAhead))
  75. resistance2 = request.security(syminfo.ticker, timeframe2, getResistance(resistanceLookBack, resistanceLookAhead))
  76. var float mostRecentResistance2 = na
  77. var float mostRecentSupport2 = na
  78. mostRecentResistance2 := ta.change(resistance2) ? resistance2 : mostRecentResistance2
  79. mostRecentSupport2 := ta.change(support2) ? support2 : mostRecentSupport2
  80. var line resistance2Line = na
  81. var line support2Line = na
  82. line.delete(resistance2Line)
  83. line.delete(support2Line)
  84. resistance2Line := line.new(x1=bar_index[10], y1=mostRecentResistance2, x2=bar_index, y2=mostRecentResistance2, color=resistanceRange2Color, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  85. support2Line := line.new(x1=bar_index[10], y1=mostRecentSupport2, x2=bar_index, y2=mostRecentSupport2, color=supportRange2Color, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  86. resistance2Price = plot(series=mostRecentResistance2, title="Resistance Price", color=red, editable=false, display=display.price_scale)
  87. support2Price = plot(series=mostRecentSupport2, title="Support Price", color=green, editable=false, display=display.price_scale)
  88.  
  89. var label support2label = na
  90. var label resistance2label = na
  91. label.delete(support2label)
  92. label.delete(resistance2label)
  93. support2label := label.new(x=bar_index+30, y=mostRecentSupport2, text=bot.convertTimeframeToString(timeframe2), color=supportRange2Color, textcolor=white, style=label.style_label_down)
  94. resistance2label := label.new(x=bar_index+30, y=mostRecentResistance2, text=bot.convertTimeframeToString(timeframe2), color=resistanceRange2Color, textcolor=white, style=label.style_label_down)
  95.  
  96.  
  97. if enableSrRanges1
  98. support1RangeTop = support1 + (support1 * (supportRange1/100))
  99. support1RangeBottom = support1 - (support1 * (supportRange1/100))
  100. resistance1RangeTop = resistance1 + (resistance1 * (resistanceRange1/100))
  101. resistance1RangeBottom = resistance1 - (resistance1 * (resistanceRange1/100))
  102.  
  103. var line support1RangeTopLine = na
  104. var line support1RangeBottomLine = na
  105. var line resistance1RangeTopLine = na
  106. var line resistance1RangeBottomLine = na
  107.  
  108. line.delete(support1RangeTopLine)
  109. line.delete(support1RangeBottomLine)
  110. line.delete(resistance1RangeTopLine)
  111. line.delete(resistance1RangeBottomLine)
  112.  
  113. support1RangeTopLine := line.new(x1=bar_index[10], y1=support1RangeTop, x2=bar_index, y2=support1RangeTop, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  114. support1RangeBottomLine := line.new(x1=bar_index[10], y1=support1RangeBottom, x2=bar_index, y2=support1RangeBottom, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  115. linefill.new(line1=support1RangeTopLine, line2=support1RangeBottomLine, color=lightGreen)
  116.  
  117. resistance1RangeTopLine := line.new(x1=bar_index[10], y1=resistance1RangeTop, x2=bar_index, y2=resistance1RangeTop, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  118. resistance1RangeBottomLine := line.new(x1=bar_index[10], y1=resistance1RangeBottom, x2=bar_index, y2=resistance1RangeBottom, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  119. linefill.new(line1=resistance1RangeTopLine, line2=resistance1RangeBottomLine, color=lightRed)
  120.  
  121.  
  122. if enableSrRanges2
  123. support2RangeTop = support2 + (support2 * (supportRange2/100))
  124. support2RangeBottom = support2 - (support2 * (supportRange2/100))
  125. resistance2RangeTop = resistance2 + (resistance2 * (resistanceRange2/100))
  126. resistance2RangeBottom = resistance2 - (resistance2 * (resistanceRange2/100))
  127.  
  128. var line support2RangeTopLine = na
  129. var line support2RangeBottomLine = na
  130. var line resistance2RangeTopLine = na
  131. var line resistance2RangeBottomLine = na
  132.  
  133. line.delete(support2RangeTopLine)
  134. line.delete(support2RangeBottomLine)
  135. line.delete(resistance2RangeTopLine)
  136. line.delete(resistance2RangeBottomLine)
  137.  
  138. support2RangeTopLine := line.new(x1=bar_index[10], y1=support2RangeTop, x2=bar_index, y2=support2RangeTop, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  139. support2RangeBottomLine := line.new(x1=bar_index[10], y1=support2RangeBottom, x2=bar_index, y2=support2RangeBottom, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  140. linefill.new(line1=support2RangeTopLine, line2=support2RangeBottomLine, color=lightGreen)
  141.  
  142. resistance2RangeTopLine := line.new(x1=bar_index[10], y1=resistance2RangeTop, x2=bar_index, y2=resistance2RangeTop, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  143. resistance2RangeBottomLine := line.new(x1=bar_index[10], y1=resistance2RangeBottom, x2=bar_index, y2=resistance2RangeBottom, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
  144. linefill.new(line1=resistance2RangeTopLine, line2=resistance2RangeBottomLine, color=lightRed)
  145.  
  146.  
Advertisement
Add Comment
Please, Sign In to add comment