xmd79

+ ATR Support and Resistance

Oct 2nd, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.15 KB | None | 0 0
  1. //@version=4
  2.  
  3.  
  4. //Arranged by @ClassicScott
  5.  
  6. //Essentially, my line of thinking is that on impulsive candles down, the origin of the impulse is more important than the close (not always of course, as there
  7. //are no unbreakable rules in what markets can do), and with impulsive candles up, the same theory applies.
  8. //So, for upward impulsive candles I've marked out the zone from the open to the low as a support (until broken, in which case it may become resistance).
  9. //For downward impulsive candles the zone encompasses the open to the high.
  10. //I've given the option to plot a line from the close for all of these. It's turned off by default as it's just less stuff on the chart, but you may like it.
  11.  
  12. //The line length is customizable in a menu. It does funny things on low timeframes on forex and stock charts (long lines that result in chart compression),
  13. //but, for some reason, very rarely on crypto charts. If someone who is smart (not me) and has much experience with pinescript could perhaps help me out with a fix
  14. //for this, that would be great. I suspect it has something to do with my "bar_index_duration" that I defined using the time function, but I'm not sure how or why.
  15.  
  16. //Line length on time frames of one hour and up it is typically fine.
  17.  
  18. //Use the ATR multiple to change the sensitivity of the indicator. This is basically the determination of when a candle is beyond the ATR.
  19. //A multiple of two is two times the ATR. With lower volatile pairs you can maybe make this lower. On lower time frames or with more volatile pairs (illiquid alts
  20. //in particular) a higher multiple might serve better. I find the defaul 1.75 is good balance.
  21.  
  22. //As I started this I also thought adding some sort of volume information to the candles might be useful as well, so I added a simple candle coloring feature
  23. //referencing the OBV and a 21 period EMA. Candles are colored based on the OBV's relation to its moving average.
  24.  
  25. //I added some plot shapes and candle coloring utilizing the RSI as well. Options to turn on or off shapes plotted for overbought and oversold across the top of
  26. //the chart. The most interesting feature that I implemented here is a support/resistance zone around the centerline of the RSI. If the RSI is between 49 and 51
  27. //then you can have optional candle coloring, shapes plotted above the candles, and s/r zones drawn on the chart. In trending markets the centerline of the RSI will
  28. //frequetly act as support or resistance, so by being alerted of this condition on the chart you can use that with actual levels marked off in order to help make a
  29. //judgement on a trade. I think it's a nice addition, and an oft overlooked aspect of the usefulness of the RSI.
  30.  
  31. //I've also included a calculation, with candle coloring and/or plot shapes, for something like a stop run on high volume (high volume being the measure of the
  32. //volume in a candle versus a 21 period EMA of volume). The calculation for that is in its section below, and should be pretty self explanatory.
  33.  
  34.  
  35. study(title="+ ATR Support and Resistance", shorttitle="+ ATR S/R", overlay=true)
  36.  
  37. ////ATR CANDLES
  38. period = input(title="ATR Period", defval=14, minval=1, group="ATR Inputs")
  39. mult = input(type=input.float, title="ATR Multiple", defval=1.75, step=0.05, group="ATR Inputs")
  40.  
  41. atr = sma(tr(true), period)
  42. atr_mult = atr * mult
  43.  
  44. imp = tr >= atr_mult
  45. imp_up = tr >= atr_mult and close > open
  46. imp_down = tr >= atr_mult and close < open
  47.  
  48. barcolor(imp_up ? color.blue : imp_down ? color.red : na, title="ATR Candles")
  49.  
  50. plotshape(imp_up, title="Impulse Up", style=shape.circle, location=location.belowbar, color=color.blue)
  51. plotshape(imp_down, title="Impulse Down", style=shape.circle, location=location.abovebar, color=color.red)
  52.  
  53.  
  54. ////////////////////////////////////////////////////
  55.  
  56.  
  57. ////ATR HORIZONTAL LINES
  58. line_term = input(defval=20, title="Horizontal Line Length", group="ATR Inputs")
  59. bar_index_duration = time - time[1]
  60.  
  61.  
  62. ////////////////////////////////////////////////////
  63.  
  64. ////CALCULATIONS AND INPUTS FOR S&R ZONES
  65. //IMPULSE DOWN
  66. i_imp_down = input(defval=true, title="Bearish Impulse?", group="Support & Resistance Zones")
  67. d_style = input(type=input.string, defval="Dotted", options=["Dotted", "Dashed", "Solid"], title="Line Style", group="Support & Resistance Zones")
  68. d_width = input(defval=1, maxval=4, title="Line Width", group="Support & Resistance Zones")
  69. d_color = input(color.new(color.red, 25), title="Border Color", group="Support & Resistance Zones")
  70. i_d_bgcolor = input(color.new(color.red, 90), title="Background Color", group="Support & Resistance Zones")
  71.  
  72. d_left = imp_down ? time : na
  73. d_top = imp_down ? high : na
  74. d_right = imp_down ? time + (line_term * bar_index_duration) : na
  75. d_bottom = imp_down ? open : na
  76. d_box_xloc = xloc.bar_time
  77. d_border_color = d_color
  78. d_border_width = d_width
  79. d_border_style = d_style
  80. d_bgcolor = i_d_bgcolor
  81.  
  82. d_box_style = d_style == "Dotted" ? line.style_dotted : d_style == "Dashed" ? line.style_dashed : d_style == "Solid" ? line.style_solid : na
  83.  
  84. var box box_down = na
  85.  
  86. if i_imp_down
  87. box_down := box.new(d_left, d_top, d_right, d_bottom, xloc=d_box_xloc, border_color=d_border_color, border_style=d_box_style, border_width=d_border_width, bgcolor=d_bgcolor)
  88.  
  89.  
  90. //IMPULSE UP
  91. i_imp_up = input(defval=true, title="Bullish Impulse?", group="Support & Resistance Zones")
  92. u_style = input(type=input.string, defval="Dotted", options=["Dotted", "Dashed", "Solid"], title="Line Style", group="Support & Resistance Zones")
  93. u_width = input(defval=1, maxval=4, title="Line Width", group="Support & Resistance Zones")
  94. u_color = input(color.new(color.blue, 25), title="Border Color", group="Support & Resistance Zones")
  95. i_u_bgcolor = input(color.new(color.blue, 90), title="Background Color", group="Support & Resistance Zones")
  96.  
  97. u_left = imp_up ? time : na
  98. u_top = imp_up ? low : na
  99. u_right = imp_up ? time + (line_term * bar_index_duration) : na
  100. u_bottom = imp_up ? open : na
  101. u_box_xloc = xloc.bar_time
  102. u_border_color = u_color
  103. u_border_width = u_width
  104. u_border_style = u_style
  105. u_bgcolor = i_u_bgcolor
  106.  
  107. u_box_style = u_style == "Dotted" ? line.style_dotted : u_style == "Dashed" ? line.style_dashed : u_style == "Solid" ? line.style_solid : na
  108.  
  109. var box box_up = na
  110.  
  111. if i_imp_up
  112. box_up := box.new(u_left, u_top, u_right, u_bottom, xloc=u_box_xloc, border_color=u_border_color, border_style=u_box_style, border_width=u_border_width, bgcolor=u_bgcolor)
  113.  
  114.  
  115. //CLOSE LINES
  116. i_closes = input(defval=false, title="Closes?", group="Close Lines")
  117. line_style_c = input(type=input.string, defval="Dotted", options=["Dotted", "Dashed", "Solid"], title="Line Style", group="Close Lines")
  118. line_width_c = input(defval=1, maxval=4, title="Line Width", group="Close Lines")
  119. line_color_c = input(color.silver, title="Line Color | Closes", group="Close Lines")
  120.  
  121. x1_c = imp ? time : na
  122. y1_c = imp ? close : na
  123. x2_c = imp ? time + (line_term * bar_index_duration) : na
  124. y2_c = imp ? y1_c : na
  125. xloc_c = xloc.bar_time
  126. color_c = line_color_c
  127. style_c = line_style_c == "Dotted" ? line.style_dotted : line_style_c == "Dashed" ? line.style_dashed : line_style_c == "Solid" ? line.style_solid : na
  128.  
  129. closes = i_closes ? line.new(x1_c, y1_c, x2_c, y2_c, xloc=xloc_c, color=color_c, style=style_c, width=line_width_c) : na
  130.  
  131.  
  132. ////////////////////////////////////////////////////
  133.  
  134.  
  135. ////RSI VALUES
  136. rsi_len = input(defval=13, title="Lookback", group="RSI")
  137.  
  138. up = rma(max(change(close), 0), rsi_len)
  139. down = rma(-min(change(close), 0), rsi_len)
  140. rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
  141.  
  142. rsi_sr = rsi >= 49 and rsi <= 51
  143.  
  144. barcolor(rsi <= 51 and rsi >=49 ? color.aqua : na, title="RSI Candles")
  145.  
  146. plotshape(rsi >= 70, title="RSI Oversold", style=shape.triangledown, location=location.top, color=color.yellow)
  147. plotshape(rsi <= 30, title="RSI Overbought", style=shape.triangleup, location=location.top, color=color.purple)
  148. plotshape(rsi_sr, title="RSI Mid Line", style=shape.square, location=location.abovebar, color=color.aqua)
  149.  
  150.  
  151. ////RSI SUPPORT & RESISTANCE
  152. rsi_support = open > close and rsi_sr
  153. rsi_resistance = open < close and rsi_sr
  154.  
  155. //RSI SUPPORT ZONES
  156. i_rsi_s = input(defval=true, title="RSI Support Zones?", group="RSI Zones")
  157. rsi_s_style = input(type=input.string, defval="Dotted", options=["Dotted", "Dashed", "Solid"], title="Line Style", group="RSI Zones")
  158. rsi_s_width = input(defval=1, maxval=4, title="Line Width", group="RSI Zones")
  159. rsi_s_color = input(color.new(color.aqua, 25), title="Border Color", group="RSI Zones")
  160. i_rsi_s_bgcolor = input(color.new(color.aqua, 90), title="Background Color", group="RSI Zones")
  161.  
  162. rsi_s_left = rsi_support ? time : na
  163. rsi_s_top = rsi_support ? close : na
  164. rsi_s_right = rsi_support ? time + (line_term * bar_index_duration) : na
  165. rsi_s_bottom = rsi_support ? low : na
  166. rsi_s_box_xloc = xloc.bar_time
  167. rsi_s_border_color = rsi_s_color
  168. rsi_s_border_width = rsi_s_width
  169. rsi_s_border_style = rsi_s_style
  170. rsi_s_bgcolor = i_rsi_s_bgcolor
  171.  
  172. rsi_s_box_style = rsi_s_style == "Dotted" ? line.style_dotted : d_style == "Dashed" ? line.style_dashed : d_style == "Solid" ? line.style_solid : na
  173.  
  174. var box rsi_s_box = na
  175.  
  176. if i_rsi_s and not rsi_sr[1]
  177. rsi_s_box := box.new(rsi_s_left, rsi_s_top, rsi_s_right, rsi_s_bottom, xloc=rsi_s_box_xloc, border_color=rsi_s_border_color, border_style=rsi_s_box_style, border_width=rsi_s_border_width, bgcolor=rsi_s_bgcolor)
  178.  
  179. //RSI RESISTANCE ZONES
  180. i_rsi_r = input(defval=true, title="RSI Resistance Zones?", group="RSI Zones")
  181. rsi_r_style = input(type=input.string, defval="Dotted", options=["Dotted", "Dashed", "Solid"], title="Line Style", group="RSI Zones")
  182. rsi_r_width = input(defval=1, maxval=4, title="Line Width", group="RSI Zones")
  183. rsi_r_color = input(color.new(color.aqua, 25), title="Border Color", group="RSI Zones")
  184. i_rsi_r_bgcolor = input(color.new(color.aqua, 90), title="Background Color", group="RSI Zones")
  185.  
  186. rsi_r_left = rsi_resistance ? time : na
  187. rsi_r_top = rsi_resistance ? high : na
  188. rsi_r_right = rsi_resistance ? time + (line_term * bar_index_duration) : na
  189. rsi_r_bottom = rsi_resistance ? close : na
  190. rsi_r_box_xloc = xloc.bar_time
  191. rsi_r_border_color = rsi_s_color
  192. rsi_r_border_width = rsi_s_width
  193. rsi_r_border_style = rsi_s_style
  194. rsi_r_bgcolor = i_rsi_s_bgcolor
  195.  
  196. rsi_r_box_style = rsi_s_style == "Dotted" ? line.style_dotted : d_style == "Dashed" ? line.style_dashed : d_style == "Solid" ? line.style_solid : na
  197.  
  198. var box rsi_r_box = na
  199.  
  200. if i_rsi_r and not rsi_sr[1]
  201. rsi_r_box := box.new(rsi_r_left, rsi_r_top, rsi_r_right, rsi_r_bottom, xloc=rsi_r_box_xloc, border_color=rsi_r_border_color, border_style=rsi_r_box_style, border_width=rsi_r_border_width, bgcolor=rsi_r_bgcolor)
  202.  
  203.  
  204.  
  205. ////////////////////////////////////////////////////
  206.  
  207.  
  208. ////HIGH VOLUME CANDLES & STOP RUNNING CANDLES
  209. i_volume_ma = input(title="EMA Period", defval=21, group="Volume")
  210. volume_ma = ema(volume, i_volume_ma)
  211.  
  212. high_volume = volume > volume_ma
  213.  
  214. barcolor(high_volume and close < close[1] and high > high[1] ? #ef9a9a : high_volume and close > close[1] and low < low[1] ? #90bff9 : na, title="High Volume Candles")
  215.  
  216. plotshape(high_volume and close < close[1] and high > high[1], title="Ran the Highs?", style=shape.circle, location=location.abovebar, color=#ef9a9a)
  217. plotshape(high_volume and close > close[1] and low < low[1], title="Ran the Lows?", style=shape.circle, location=location.belowbar, color=#90bff9)
  218.  
  219.  
  220. ////////////////////////////////////////////////////
  221.  
  222.  
  223. ////OBV CANDLES
  224. obv_prd = input(title="EMA Period", defval=21, minval=2, group="OBV")
  225. obv_ema = ema(obv, obv_prd)
  226.  
  227. barcolor(obv > obv_ema ? color.yellow : obv < obv_ema ? color.purple : na, title="OBV Candles")
  228.  
  229.  
  230. ////////////////////////////////////////////////////
  231.  
  232.  
  233. ////200 PERIOD EMA/SMA/KIJUN AVERAGE
  234. show_ma = input(type=input.bool, title="Yes or No?", defval=true, group="200 MA AVG")
  235. ma_lw = input(title="Linewidth", defval=2, minval=1, group="200 MA AVG")
  236. ma_pos = input(color.blue, title="Up", group="200 MA AVG")
  237. ma_neg = input(color.red, title="Down", group="200 MA AVG")
  238.  
  239. kijun_200 = avg(lowest(200), highest(200))
  240.  
  241. ma_avgs = (ema(close, 200) + sma(close, 200) + kijun_200) / 3
  242.  
  243. ma_color = close > ma_avgs ? ma_pos : ma_neg
  244.  
  245. plot(show_ma ? ma_avgs : na, color=ma_color, linewidth=ma_lw, title="200 AVG")
  246.  
  247.  
Advertisement
Add Comment
Please, Sign In to add comment