Advertisement
andypartridge47

Support and Resistance (High Volume Boxes) AP Edit V2

Dec 20th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. // This Pine Scriptβ„’ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // Β© ChartPrime
  3.  
  4. // 1. CHANGE VERSION TO V6
  5. // 2. INITIALIZE BOOLEANS AS FALSE IN PINESCRIPT V6 breakout_resistance, breakout_sup, resistance_holds, support_holds
  6. // 3. CHANGE brekout to breakout with ctrl + f
  7. // 4. CHANGE res to resistance and sup to support
  8. // 5. CHANGE library to strategy
  9. // 6. If support holds: Buy, If resistance holds: sell
  10.  
  11. //@version=6
  12. strategy("Support and Resistance (High Volume Boxes) [ChartPrime]", shorttitle = "SR Breaks and Retests [ChartPrime]", overlay=true, max_boxes_count = 50)
  13.  
  14.  
  15. // ---------------------------------------------------------------------------------------------------------------------}
  16. // π™π™Žπ™€π™ π™„π™‰π™‹π™π™π™Ž
  17. // ---------------------------------------------------------------------------------------------------------------------{
  18. int lookbackPeriod = input.int(20, "Lookback Period", minval=1, group = "Settings")
  19. int vol_len = input.int(2, "Delta Volume Filter Length", tooltip = "Higher input, will filter low volume boxes"
  20. , group = "Settings")
  21. float box_withd = input.float(1, "Adjust Box Width", maxval = 1000, minval = 0, step = 0.1)
  22.  
  23.  
  24. // ---------------------------------------------------------------------------------------------------------------------}
  25. // π™„π™‰π˜Ώπ™„π˜Ύπ˜Όπ™π™Šπ™ π˜Ύπ˜Όπ™‡π˜Ύπ™π™‡π˜Όπ™π™„π™Šπ™‰π™Ž
  26. // ---------------------------------------------------------------------------------------------------------------------{
  27. // Delta Volume Function
  28. upAndDownVolume() =>
  29. posVol = 0.0
  30. negVol = 0.0
  31.  
  32. var isBuyVolume = true
  33.  
  34. switch
  35. close > open => isBuyVolume := true
  36. close < open => isBuyVolume := false
  37.  
  38. if isBuyVolume
  39. posVol += volume
  40. else
  41. negVol -= volume
  42.  
  43. posVol + negVol
  44.  
  45.  
  46. // Function to identify support and resistance boxes
  47. calcSupportResistance(src, lookbackPeriod) =>
  48. // Volume
  49. Vol = upAndDownVolume()
  50. vol_hi = ta.highest(Vol/2.5, vol_len)
  51. vol_lo = ta.lowest(Vol/2.5, vol_len)
  52.  
  53. var float supportLevel = na
  54. var float supportLevel_1 = na
  55. var float resistanceLevel = na
  56. var float resistanceLevel_1 = na
  57. var box support = na
  58. var box resistance = na
  59. var color resistance_color = na
  60. var color support_color = na
  61. var float multi = na
  62.  
  63.  
  64. var bool breakout_resistance = false
  65. var bool breakout_support = false
  66. var bool resistance_holds = false
  67. var bool support_holds = false
  68.  
  69. // Find pivot points
  70. pivotHigh = ta.pivothigh(src, lookbackPeriod, lookbackPeriod)
  71. pivotLow = ta.pivotlow (src, lookbackPeriod, lookbackPeriod)
  72. // Box width
  73. atr = ta.atr(200)
  74. withd = atr * box_withd
  75.  
  76. // Find support levels with Positive Volume
  77. if (not na(pivotLow)) and Vol > vol_hi
  78.  
  79. supportLevel := pivotLow
  80. supportLevel_1 := supportLevel-withd
  81.  
  82. topLeft = chart.point.from_index(bar_index-lookbackPeriod, supportLevel)
  83. bottomRight = chart.point.from_index(bar_index, supportLevel_1)
  84.  
  85. support_color := color.from_gradient(Vol, 0, ta.highest(Vol, 25), color(na), color.new(color.green, 30))
  86.  
  87. support := box.new(
  88. top_left = topLeft,
  89. bottom_right = bottomRight,
  90. border_color = color.green,
  91. border_width = 1,
  92. bgcolor = support_color,
  93. text = "Vol: "+str.tostring(math.round(Vol,2)),
  94. text_color = chart.fg_color,
  95. text_size = size.small
  96. )
  97.  
  98.  
  99. // Find resistance levels with Negative Volume
  100. if (not na(pivotHigh)) and Vol < vol_lo
  101.  
  102. resistanceLevel := pivotHigh
  103. resistanceLevel_1 := resistanceLevel+withd
  104.  
  105. topLeft = chart.point.from_index(bar_index-lookbackPeriod, resistanceLevel)
  106. bottomRight = chart.point.from_index(bar_index, resistanceLevel_1)
  107.  
  108. resistance_color := color.from_gradient(Vol, ta.lowest(Vol, 25), 0, color.new(color.red, 30), color(na))
  109.  
  110. resistance := box.new(
  111. top_left = topLeft,
  112. bottom_right = bottomRight,
  113. border_color = color.red,
  114. border_width = 1,
  115. bgcolor = resistance_color,
  116. text = "Vol: "+str.tostring(math.round(Vol,2)),
  117. text_color = chart.fg_color,
  118. text_size = size.small
  119. )
  120.  
  121. // Adaptive Box Len
  122. support.set_right(bar_index+1)
  123. resistance.set_right(bar_index+1)
  124.  
  125. // Break of support or resistance conditions
  126. breakout_resistance := ta.crossover(low, resistanceLevel_1)
  127. resistance_holds := ta.crossunder(high, resistanceLevel)
  128.  
  129. support_holds := ta.crossover(low, supportLevel)
  130. breakout_support := ta.crossunder(high, supportLevel_1)
  131.  
  132. // Change Color of Support to red if it was break, change color of resistance to green if it was break
  133. if breakout_support
  134. support.set_bgcolor(color.new(color.red, 80))
  135. support.set_border_color(color.red)
  136. support.set_border_style(line.style_dashed)
  137.  
  138. if support_holds
  139. support.set_bgcolor(support_color)
  140. support.set_border_color(color.green)
  141. support.set_border_style(line.style_solid)
  142.  
  143. if breakout_resistance
  144. resistance.set_bgcolor(color.new(color.green, 80))
  145. resistance.set_border_color(color.new(color.green, 0))
  146. resistance.set_border_style(line.style_dashed)
  147.  
  148. if resistance_holds
  149. resistance.set_bgcolor(resistance_color)
  150. resistance.set_border_color(color.new(color.red, 0))
  151. resistance.set_border_style(line.style_solid)
  152.  
  153. [supportLevel, resistanceLevel, breakout_resistance, resistance_holds, support_holds, breakout_support]
  154.  
  155.  
  156. // Calculate support and resistance levels and their breakouts
  157. [supportLevel,
  158. resistanceLevel,
  159. breakout_resistance,
  160. resistance_holds,
  161. support_holds,
  162. breakout_support] = calcSupportResistance(close, lookbackPeriod)
  163.  
  164.  
  165. // Check if Resistance become Support or Support Become Resistance
  166. var bool resistance_is_support = false
  167. var bool support_is_resistance = false
  168.  
  169. switch
  170. breakout_resistance => resistance_is_support := true
  171. resistance_holds => resistance_is_support := false
  172.  
  173. switch
  174. breakout_support => support_is_resistance := true
  175. support_holds => support_is_resistance := false
  176.  
  177.  
  178. // ---------------------------------------------------------------------------------------------------------------------}
  179. // π™‘π™„π™Žπ™π˜Όπ™‡π™„π™•π˜Όπ™π™„π™Šπ™‰
  180. // ---------------------------------------------------------------------------------------------------------------------{
  181. // Plot resistance and support breakouts and holds
  182. plotchar(resistance_holds, "Resistance Holds", "β—†",
  183. color = #e92929, size = size.tiny, location = location.abovebar, offset = -1)
  184. plotchar(support_holds, "Support Holds", "β—†",
  185. color = #20ca26, size = size.tiny, location = location.belowbar, offset = -1)
  186.  
  187. plotchar(breakout_resistance and resistance_is_support[1], "Resistance as Support Holds", "β—†",
  188. color = #20ca26, size = size.tiny, location = location.belowbar, offset = -1)
  189. plotchar(breakout_support and support_is_resistance[1], "Support as Resistance Holds", "β—†",
  190. color = #e92929, size = size.tiny, location = location.abovebar, offset = -1)
  191.  
  192. // Break Out Labels
  193. if breakout_support and not support_is_resistance[1]
  194. label.new(
  195. bar_index[1], supportLevel[1],
  196. text = "Break Sup",
  197. style = label.style_label_down,
  198. color = #7e1e1e,
  199. textcolor = chart.fg_color,
  200. size = size.small
  201. )
  202.  
  203. if breakout_resistance and not resistance_is_support[1]
  204. label.new(
  205. bar_index[1], resistanceLevel[1],
  206. text = "Break Res",
  207. style = label.style_label_up,
  208. color = #2b6d2d,
  209. textcolor = chart.fg_color,
  210. size = size.small
  211. )
  212.  
  213. // Define risk-reward ratio as a user input
  214. float riskRewardRatio = input.float(2.0, "Risk Reward Ratio", minval = 0.1, step = 0.1, group = "Settings")
  215.  
  216. // If resistance Hold: short
  217. // If support Holds: Long
  218. longCondition = (supportLevel[3] != supportLevel[1]) and (resistanceLevel > supportLevel)
  219. shortCondition = false
  220. //// Uncomment for mean reverting strategy
  221. // longCondition = false
  222. // shortCondition = (supportLevel[1] != supportLevel[0]) and (resistanceLevel > supportLevel)
  223.  
  224. plot(supportLevel, color=color.green, linewidth=2)
  225. plot(resistanceLevel, color=color.red, linewidth=2)
  226.  
  227. if strategy.position_size == 0
  228. if longCondition
  229. strategy.entry("Long", strategy.long)
  230. stop = supportLevel
  231. limit = close + ((close - supportLevel) * riskRewardRatio)
  232. strategy.exit("TP / SL", "Long", limit = limit, stop = stop)
  233. if shortCondition
  234. strategy.entry("Short", strategy.short)
  235. stop = resistanceLevel
  236. limit = close - ((resistanceLevel - close) * riskRewardRatio)
  237. strategy.exit("TP / SL", "Short", limit = limit, stop = stop)
  238.  
  239. //Imput for RiskRewardRation
  240.  
  241.  
  242. // ---------------------------------------------------------------------------------------------------------------------}
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement