Advertisement
andypartridge47

Support and Resistance (High Volume Boxes) AP V3

Dec 22nd, 2024
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.01 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.  
  12. //@version=6
  13. strategy("Support and Resistance (High Volume Boxes) [ChartPrime]", shorttitle = "SR Breaks and Retests [ChartPrime]", overlay=true, max_boxes_count = 50)
  14.  
  15.  
  16. // ---------------------------------------------------------------------------------------------------------------------}
  17. // π™π™Žπ™€π™ π™„π™‰π™‹π™π™π™Ž
  18. // ---------------------------------------------------------------------------------------------------------------------{
  19. int lookbackPeriod = input.int(20, "Lookback Period", minval=1, group = "Settings")
  20. int vol_len = input.int(2, "Delta Volume Filter Length", tooltip = "Higher input, will filter low volume boxes"
  21. , group = "Settings")
  22. float box_withd = input.float(1, "Adjust Box Width", maxval = 1000, minval = 0, step = 0.1)
  23. float riskReward = input.float(3, "Risk Reward")
  24.  
  25. // Add a dropdown for strategy selection
  26. string strategySelector = input.string(defval = "Strategy 1 - Resistance Holds: Short, Support Holds: Long", options = ["Strategy 1 - Resistance Holds: Short, Support Holds: Long", "Strategy 2 - Buy on new support level", "Strategy 3 - Sell on new support level", "Strategy 4 - Buy on breakout of Resistance. SL = Resistance", "Strategy 5 - Buy on Rapid false breakouts of support zones (Stop Hunting)"], title = "Select Strategy")
  27.  
  28. // ---------------------------------------------------------------------------------------------------------------------}
  29. // π™„π™‰π˜Ώπ™„π˜Ύπ˜Όπ™π™Šπ™ π˜Ύπ˜Όπ™‡π˜Ύπ™π™‡π˜Όπ™π™„π™Šπ™‰π™Ž
  30. // ---------------------------------------------------------------------------------------------------------------------{
  31. // Delta Volume Function
  32. upAndDownVolume() =>
  33. posVol = 0.0
  34. negVol = 0.0
  35.  
  36. var isBuyVolume = true
  37.  
  38. switch
  39. close > open => isBuyVolume := true
  40. close < open => isBuyVolume := false
  41.  
  42. if isBuyVolume
  43. posVol += volume
  44. else
  45. negVol -= volume
  46.  
  47. posVol + negVol
  48.  
  49.  
  50. // Function to identify support and resistance boxes
  51. calcSupportResistance(src, lookbackPeriod) =>
  52. // Volume
  53. Vol = upAndDownVolume()
  54. vol_hi = ta.highest(Vol/2.5, vol_len)
  55. vol_lo = ta.lowest(Vol/2.5, vol_len)
  56.  
  57. var float supportLevel = na
  58. var float supportLevel_1 = na
  59. var float resistanceLevel = na
  60. var float resistanceLevel_1 = na
  61. var box support = na
  62. var box resistance = na
  63. var color resistance_color = na
  64. var color support_color = na
  65. var float multi = na
  66.  
  67.  
  68. var bool breakout_resistance = false
  69. var bool breakout_support = false
  70. var bool resistance_holds = false
  71. var bool support_holds = false
  72.  
  73. // Find pivot points
  74. pivotHigh = ta.pivothigh(src, lookbackPeriod, lookbackPeriod)
  75. pivotLow = ta.pivotlow (src, lookbackPeriod, lookbackPeriod)
  76. // Box width
  77. atr = ta.atr(200)
  78. withd = atr * box_withd
  79.  
  80. // Find support levels with Positive Volume
  81. if (not na(pivotLow)) and Vol > vol_hi
  82.  
  83. supportLevel := pivotLow
  84. supportLevel_1 := supportLevel-withd
  85.  
  86. topLeft = chart.point.from_index(bar_index-lookbackPeriod, supportLevel)
  87. bottomRight = chart.point.from_index(bar_index, supportLevel_1)
  88.  
  89. support_color := color.from_gradient(Vol, 0, ta.highest(Vol, 25), color(na), color.new(color.green, 30))
  90.  
  91. support := box.new(
  92. top_left = topLeft,
  93. bottom_right = bottomRight,
  94. border_color = color.green,
  95. border_width = 1,
  96. bgcolor = support_color,
  97. text = "Vol: "+str.tostring(math.round(Vol,2)),
  98. text_color = chart.fg_color,
  99. text_size = size.small
  100. )
  101.  
  102.  
  103. // Find resistance levels with Negative Volume
  104. if (not na(pivotHigh)) and Vol < vol_lo
  105.  
  106. resistanceLevel := pivotHigh
  107. resistanceLevel_1 := resistanceLevel + withd
  108.  
  109. topLeft = chart.point.from_index(bar_index-lookbackPeriod, resistanceLevel)
  110. bottomRight = chart.point.from_index(bar_index, resistanceLevel_1)
  111.  
  112. resistance_color := color.from_gradient(Vol, ta.lowest(Vol, 25), 0, color.new(color.red, 30), color(na))
  113.  
  114. resistance := box.new(
  115. top_left = topLeft,
  116. bottom_right = bottomRight,
  117. border_color = color.red,
  118. border_width = 1,
  119. bgcolor = resistance_color,
  120. text = "Vol: "+str.tostring(math.round(Vol,2)),
  121. text_color = chart.fg_color,
  122. text_size = size.small
  123. )
  124.  
  125. // Adaptive Box Len
  126. support.set_right(bar_index+1)
  127. resistance.set_right(bar_index+1)
  128.  
  129. // Break of support or resistance conditions
  130. breakout_resistance := ta.crossover(low, resistanceLevel_1)
  131. resistance_holds := ta.crossunder(high, resistanceLevel)
  132.  
  133. support_holds := ta.crossover(low, supportLevel)
  134. breakout_support := ta.crossunder(high, supportLevel_1)
  135.  
  136. // Change Color of Support to red if it was break, change color of resistance to green if it was break
  137. if breakout_support
  138. support.set_bgcolor(color.new(color.red, 80))
  139. support.set_border_color(color.red)
  140. support.set_border_style(line.style_dashed)
  141.  
  142. if support_holds
  143. support.set_bgcolor(support_color)
  144. support.set_border_color(color.green)
  145. support.set_border_style(line.style_solid)
  146.  
  147. if breakout_resistance
  148. resistance.set_bgcolor(color.new(color.green, 80))
  149. resistance.set_border_color(color.new(color.green, 0))
  150. resistance.set_border_style(line.style_dashed)
  151.  
  152. if resistance_holds
  153. resistance.set_bgcolor(resistance_color)
  154. resistance.set_border_color(color.new(color.red, 0))
  155. resistance.set_border_style(line.style_solid)
  156.  
  157. [supportLevel, resistanceLevel, breakout_resistance, resistance_holds, support_holds, breakout_support]
  158.  
  159.  
  160. // Calculate support and resistance levels and their breakouts
  161. [supportLevel,
  162. resistanceLevel,
  163. breakout_resistance,
  164. resistance_holds,
  165. support_holds,
  166. breakout_support] = calcSupportResistance(close, lookbackPeriod)
  167.  
  168.  
  169. // Check if Resistance become Support or Support Become Resistance
  170. var bool resistance_is_support = false
  171. var bool support_is_resistance = false
  172.  
  173. switch
  174. breakout_resistance => resistance_is_support := true
  175. resistance_holds => resistance_is_support := false
  176.  
  177. switch
  178. breakout_support => support_is_resistance := true
  179. support_holds => support_is_resistance := false
  180.  
  181.  
  182. // ---------------------------------------------------------------------------------------------------------------------}
  183. // π™‘π™„π™Žπ™π˜Όπ™‡π™„π™•π˜Όπ™π™„π™Šπ™‰
  184. // ---------------------------------------------------------------------------------------------------------------------{
  185. // Plot resistance and support breakouts and holds
  186. plotchar(resistance_holds, "Resistance Holds", "β—†",
  187. color = #e92929, size = size.tiny, location = location.abovebar, offset = -1)
  188. plotchar(support_holds, "Support Holds", "β—†",
  189. color = #20ca26, size = size.tiny, location = location.belowbar, offset = -1)
  190.  
  191. plotchar(breakout_resistance and resistance_is_support[1], "Resistance as Support Holds", "β—†",
  192. color = #20ca26, size = size.tiny, location = location.belowbar, offset = -1)
  193. plotchar(breakout_support and support_is_resistance[1], "Support as Resistance Holds", "β—†",
  194. color = #e92929, size = size.tiny, location = location.abovebar, offset = -1)
  195.  
  196. // Break Out Labels
  197. if breakout_support and not support_is_resistance[1]
  198. label.new(
  199. bar_index[1], supportLevel[1],
  200. text = "Break Sup",
  201. style = label.style_label_down,
  202. color = #7e1e1e,
  203. textcolor = chart.fg_color,
  204. size = size.small
  205. )
  206.  
  207. if breakout_resistance and not resistance_is_support[1]
  208. label.new(
  209. bar_index[1], resistanceLevel[1],
  210. text = "Break Res",
  211. style = label.style_label_up,
  212. color = #2b6d2d,
  213. textcolor = chart.fg_color,
  214. size = size.small
  215. )
  216.  
  217. // Initialize longCondition and shortCondition
  218. var bool longCondition = false
  219. var bool shortCondition = false
  220.  
  221. // Set conditions based on selected strategy
  222. if strategySelector == "Strategy 1 - Resistance Holds: Short, Support Holds: Long"
  223. longCondition := support_holds
  224. shortCondition := resistance_holds
  225.  
  226. if strategySelector == "Strategy 2 - Buy on new support level"
  227. longCondition := (supportLevel[1] != supportLevel[0]) and (resistanceLevel > supportLevel)
  228. shortCondition := (resistanceLevel[1] != resistanceLevel[0]) and (supportLevel < resistanceLevel)
  229.  
  230. if strategySelector == "Strategy 3 - Sell on new support level"
  231. longCondition := (resistanceLevel[1] != resistanceLevel[0]) and (supportLevel < resistanceLevel)
  232. shortCondition := (supportLevel[1] != supportLevel[0]) and (resistanceLevel > supportLevel)
  233.  
  234. if strategySelector == "Strategy 4 - Buy on breakout of Resistance. SL = Resistance"
  235. longCondition := breakout_resistance
  236. shortCondition := breakout_support
  237.  
  238. // Define ATR period and threshold
  239. atrPeriod = 14 // ATR calculated over the last 14 periods
  240. atrValue = ta.atr(atrPeriod) // Calculate ATR
  241. atrThreshold = 1.5 * atrValue // ATR must be 1.5 times higher than its average value
  242.  
  243. // Strategy 5 - Buy on Rapid false breakouts of support zones (Stop Hunting)
  244. if strategySelector == "Strategy 5 - Buy on Rapid false breakouts of support zones (Stop Hunting)"
  245. longCondition := low[1] < supportLevel and close[1] > supportLevel and open[1] > supportLevel and close > open and atrValue > atrThreshold
  246. shortCondition := high[1] > resistanceLevel and close[1] < resistanceLevel and open[1] < resistanceLevel and close < open and atrValue > atrThreshold
  247.  
  248. // --- Display the selected strategy title in the top-right corner of the chart ---
  249. // Create a table to display text
  250. var table strategyTable = table.new(position.top_right, 1, 1)
  251.  
  252. // Update the table with the selected strategy text
  253. if bar_index == 0
  254. table.cell(table_id = strategyTable,column = 0,row = 0,text = strategySelector,text_color = color.white,bgcolor = color.new(color.black, 90))
  255.  
  256. if strategy.position_size == 0
  257.  
  258. if longCondition
  259. stop = supportLevel
  260. limit = close + (close - supportLevel) * riskReward
  261. strategy.entry("Long", strategy.long)
  262. strategy.exit("TP / SL", "Long", limit = limit, stop = stop)
  263.  
  264. if shortCondition
  265. stop = resistanceLevel
  266. limit = supportLevel
  267. strategy.entry("Short", strategy.short)
  268. strategy.exit("TP / SL", "Short", limit = limit, stop = stop)
  269.  
  270.  
  271. // plot(resistanceLevel, color=color.red)
  272. // plot(supportLevel, color=color.green)
  273. // β—†
  274. // ---------------------------------------------------------------------------------------------------------------------}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement