xmd79

++ ATR SR and EMA cycles

Nov 20th, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.42 KB | None | 0 0
  1. //@version=5
  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. indicator(title='+ ATR Support and Resistance', shorttitle='+ ATR S/R', overlay=true)
  36.  
  37. ////ATR CANDLES
  38. period = input.int(title='ATR Period', defval=14, minval=1, group='ATR Inputs')
  39. mult = input.float(title='ATR Multiple', defval=1.75, step=0.05, group='ATR Inputs')
  40.  
  41. atr = ta.sma(ta.tr(true), period)
  42. atr_mult = atr * mult
  43.  
  44. imp = ta.tr >= atr_mult
  45. imp_up = ta.tr >= atr_mult and close > open
  46. imp_down = ta.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.new(color.blue, 0))
  51. plotshape(imp_down, title='Impulse Down', style=shape.circle, location=location.abovebar, color=color.new(color.red, 0))
  52.  
  53.  
  54. ////////////////////////////////////////////////////
  55.  
  56.  
  57. ////ATR HORIZONTAL LINES
  58. line_term = input.int(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&D ZONES
  65.  
  66. ////IMPULSE DOWN
  67. i_imp_down = input.bool(defval=true, title='Bearish Impulse?', group='Support & Resistance Zones')
  68. d_style = input.string(defval='Dotted', options=['Dotted', 'Dashed', 'Solid'], title='Line Style', group='Support & Resistance Zones')
  69. d_width = input.int(defval=1, maxval=4, title='Line Width', group='Support & Resistance Zones')
  70. d_color = input.color(color.new(color.red, 25), title='Border Color', group='Support & Resistance Zones')
  71. i_d_bgcolor = input.color(color.new(color.red, 90), title='Background Color', group='Support & Resistance Zones')
  72.  
  73. d_left = imp_down ? time : na
  74. d_top = imp_down ? high : na
  75. d_right = imp_down ? time + line_term * bar_index_duration : na
  76. d_bottom = imp_down ? open : na
  77. d_box_xloc = xloc.bar_time
  78. d_border_color = d_color
  79. d_border_width = d_width
  80. d_border_style = d_style
  81. d_bgcolor = i_d_bgcolor
  82.  
  83. d_box_style = d_style == 'Dotted' ? line.style_dotted : d_style == 'Dashed' ? line.style_dashed : d_style == 'Solid' ? line.style_solid : na
  84.  
  85. var box box_down = na
  86.  
  87. if i_imp_down
  88. 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)
  89. box_down
  90.  
  91.  
  92. ////IMPULSE UP
  93. i_imp_up = input.bool(defval=true, title='Bullish Impulse?', group='Support & Resistance Zones')
  94. u_style = input.string(defval='Dotted', options=['Dotted', 'Dashed', 'Solid'], title='Line Style', group='Support & Resistance Zones')
  95. u_width = input.int(defval=1, maxval=4, title='Line Width', group='Support & Resistance Zones')
  96. u_color = input.color(color.new(color.blue, 25), title='Border Color', group='Support & Resistance Zones')
  97. i_u_bgcolor = input.color(color.new(color.blue, 90), title='Background Color', group='Support & Resistance Zones')
  98.  
  99. u_left = imp_up ? time : na
  100. u_top = imp_up ? low : na
  101. u_right = imp_up ? time + line_term * bar_index_duration : na
  102. u_bottom = imp_up ? open : na
  103. u_box_xloc = xloc.bar_time
  104. u_border_color = u_color
  105. u_border_width = u_width
  106. u_border_style = u_style
  107. u_bgcolor = i_u_bgcolor
  108.  
  109. u_box_style = u_style == 'Dotted' ? line.style_dotted : u_style == 'Dashed' ? line.style_dashed : u_style == 'Solid' ? line.style_solid : na
  110.  
  111. var box box_up = na
  112.  
  113. if i_imp_up
  114. 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)
  115. box_up
  116.  
  117.  
  118. ////CLOSE LINES
  119. i_closes = input.bool(defval=false, title='Closes?', group='Close Lines')
  120. line_style_c = input.string(defval='Dotted', options=['Dotted', 'Dashed', 'Solid'], title='Line Style', group='Close Lines')
  121. line_width_c = input.int(defval=1, maxval=4, title='Line Width', group='Close Lines')
  122. line_color_c = input.color(color.silver, title='Line Color | Closes', group='Close Lines')
  123.  
  124. x1_c = imp ? time : na
  125. y1_c = imp ? close : na
  126. x2_c = imp ? time + line_term * bar_index_duration : na
  127. y2_c = imp ? y1_c : na
  128. xloc_c = xloc.bar_time
  129. color_c = line_color_c
  130. style_c = line_style_c == 'Dotted' ? line.style_dotted : line_style_c == 'Dashed' ? line.style_dashed : line_style_c == 'Solid' ? line.style_solid : na
  131.  
  132. 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
  133.  
  134.  
  135. ////////////////////////////////////////////////////
  136.  
  137.  
  138. ////RSI VALUES
  139. rsi_len = input.int(defval=13, title='Lookback', group='RSI')
  140.  
  141. up = ta.rma(math.max(ta.change(close), 0), rsi_len)
  142. down = ta.rma(-math.min(ta.change(close), 0), rsi_len)
  143. rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
  144.  
  145. rsi_sr = rsi >= 49 and rsi <= 51
  146.  
  147. barcolor(rsi <= 51 and rsi >= 49 ? color.aqua : na, title='RSI Candles')
  148.  
  149. plotshape(rsi >= 70, title='RSI Overbought', style=shape.triangledown, location=location.top, color=color.new(color.yellow, 0))
  150. plotshape(rsi <= 30, title='RSI Oversold', style=shape.triangleup, location=location.top, color=color.new(color.purple, 0))
  151. plotshape(rsi_sr and close < open, title='RSI Centerline Support', style=shape.square, location=location.belowbar, color=color.new(color.aqua, 0))
  152. plotshape(rsi_sr and close > open, title='RSI Centerline Resistance', style=shape.square, location=location.abovebar, color=color.new(color.aqua, 0))
  153.  
  154.  
  155. ////RSI SUPPORT & RESISTANCE ZONES
  156. rsi_support = close < open and rsi_sr
  157. rsi_resistance = close > open and rsi_sr
  158.  
  159.  
  160. ////SUPPORT ZONES
  161. i_rsi_s = input.bool(defval=true, title='RSI Support Zones?', group='RSI Zones')
  162. rsi_s_style = input.string(defval='Dotted', options=['Dotted', 'Dashed', 'Solid'], title='Line Style', group='RSI Zones')
  163. rsi_s_width = input.int(defval=1, maxval=4, title='Line Width', group='RSI Zones')
  164. rsi_s_color = input.color(color.new(color.aqua, 25), title='Border Color', group='RSI Zones')
  165. i_rsi_s_bgcolor = input.color(color.new(color.aqua, 90), title='Background Color', group='RSI Zones')
  166.  
  167. rsi_s_left = rsi_support ? time : na
  168. rsi_s_top = rsi_support ? close : na
  169. rsi_s_right = rsi_support ? time + line_term * bar_index_duration : na
  170. rsi_s_bottom = rsi_support ? low : na
  171. rsi_s_box_xloc = xloc.bar_time
  172. rsi_s_border_color = rsi_s_color
  173. rsi_s_border_width = rsi_s_width
  174. rsi_s_border_style = rsi_s_style
  175. rsi_s_bgcolor = i_rsi_s_bgcolor
  176.  
  177. rsi_s_box_style = rsi_s_style == 'Dotted' ? line.style_dotted : d_style == 'Dashed' ? line.style_dashed : d_style == 'Solid' ? line.style_solid : na
  178.  
  179. var box rsi_s_box = na
  180.  
  181. if i_rsi_s and not rsi_sr[1]
  182. 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)
  183. rsi_s_box
  184.  
  185.  
  186. ////RESISTANCE ZONES
  187. i_rsi_r = input.bool(defval=true, title='RSI Resistance Zones?', group='RSI Zones')
  188. rsi_r_style = input.string(defval='Dotted', options=['Dotted', 'Dashed', 'Solid'], title='Line Style', group='RSI Zones')
  189. rsi_r_width = input.int(defval=1, maxval=4, title='Line Width', group='RSI Zones')
  190. rsi_r_color = input.color(color.new(color.aqua, 25), title='Border Color', group='RSI Zones')
  191. i_rsi_r_bgcolor = input.color(color.new(color.aqua, 90), title='Background Color', group='RSI Zones')
  192.  
  193. rsi_r_left = rsi_resistance ? time : na
  194. rsi_r_top = rsi_resistance ? high : na
  195. rsi_r_right = rsi_resistance ? time + line_term * bar_index_duration : na
  196. rsi_r_bottom = rsi_resistance ? close : na
  197. rsi_r_box_xloc = xloc.bar_time
  198. rsi_r_border_color = rsi_s_color
  199. rsi_r_border_width = rsi_s_width
  200. rsi_r_border_style = rsi_s_style
  201. rsi_r_bgcolor = i_rsi_r_bgcolor
  202.  
  203. rsi_r_box_style = rsi_s_style == 'Dotted' ? line.style_dotted : d_style == 'Dashed' ? line.style_dashed : d_style == 'Solid' ? line.style_solid : na
  204.  
  205. var box rsi_r_box = na
  206.  
  207. if i_rsi_r and not rsi_sr[1]
  208. 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)
  209. rsi_r_box
  210.  
  211.  
  212. ////////////////////////////////////////////////////
  213.  
  214.  
  215. ////HIGH VOLUME CANDLES & STOP RUNNING CANDLES
  216. i_volume_ma = input.int(title='EMA Period', defval=21, group='Volume')
  217. volume_ma = ta.ema(volume, i_volume_ma)
  218.  
  219. high_volume = volume > volume_ma
  220.  
  221. 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')
  222.  
  223. plotshape(high_volume and close < close[1] and high > high[1], title='Ran the Highs?', style=shape.circle, location=location.abovebar, color=color.new(#ef9a9a, 0))
  224. plotshape(high_volume and close > close[1] and low < low[1], title='Ran the Lows?', style=shape.circle, location=location.belowbar, color=color.new(#90bff9, 0))
  225.  
  226.  
  227. ////////////////////////////////////////////////////
  228.  
  229.  
  230. ////OBV CANDLES
  231. obv_prd = input.int(title='EMA Period', defval=21, minval=2, group='OBV')
  232. obv_ema = ta.ema(ta.obv, obv_prd)
  233.  
  234. barcolor(ta.obv > obv_ema ? color.yellow : ta.obv < obv_ema ? color.purple : na, title='OBV Candles')
  235.  
  236.  
  237. ////////////////////////////////////////////////////
  238.  
  239.  
  240. ////200 PERIOD EMA/SMA/KIJUN AVERAGE
  241. show_ma = input.bool(title='Yes or No?', defval=true, group='200 MA AVG')
  242. ma_lw = input.int(title='Linewidth', defval=2, minval=1, group='200 MA AVG')
  243. ma_pos = input.color(color.blue, title='Up', group='200 MA AVG')
  244. ma_neg = input.color(color.red, title='Down', group='200 MA AVG')
  245.  
  246. kijun_200 = math.avg(ta.lowest(200), ta.highest(200))
  247.  
  248. ma_avgs = (ta.ema(close, 200) + ta.sma(close, 200) + kijun_200) / 3
  249.  
  250. ma_color = close > ma_avgs ? ma_pos : ma_neg
  251.  
  252. plot(show_ma ? ma_avgs : na, color=ma_color, linewidth=ma_lw, title='200 AVG')
  253.  
  254.  
  255. //end of this part
  256.  
  257. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  258. // © adjmali7912
  259.  
  260. //@version=5
  261. //indicator(title="Above50",shorttitle="Above 50",overlay=true)
  262. //Creating input controlls
  263. len1=input.int(defval=50,title='len1',minval=50,maxval=1000,step=1 )
  264. len2=input.int(defval=100,title='len1',minval=50,maxval=1000,step=1 )
  265. len3=input.int(defval=200,title='len1',minval=50,maxval=1000,step=1 )
  266. len4=input.int(defval=1000,title='len1',minval=50,maxval=1000,step=1 )
  267. //Indicator
  268. ema1=ta.ema(close,len1)
  269. ema2=ta.ema(close,len2)
  270. ema3=ta.ema(close,len3)
  271. ema4=ta.ema(close,len4)
  272. //Plot the indicator
  273. plot(ema1,color=color.red,linewidth=2)
  274. plot(ema2,color=color.green,linewidth=2)
  275. plot(ema3,color=color.yellow,linewidth=2)
  276. plot(ema4,color=color.red,linewidth=2)
  277.  
Advertisement
Add Comment
Please, Sign In to add comment