Advertisement
xmd79

CryptoSignalScanner - RSI Overbought/Oversold + Divergence Indicator

Jan 18th, 2023
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.93 KB | None | 0 0
  1. // CryptoSignalScanner.com by SEOCO
  2. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  3. //@version=5
  4. indicator('CryptoSignalScanner - RSI Overbought/Oversold + Divergence Indicator', overlay=false, precision=2, shorttitle='RSI Overbought/Oversold+Divergence')
  5.  
  6. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7. //--- Show/Hide Input Settings ----------------------------------------------------------------------------------------------------------------------------------------
  8. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  9. rsiShowInput = input(true, title='Show RSI', group='Show/Hide Settings')
  10. maShowInput = input(false, title='Show MA', group='Show/Hide Settings')
  11. showRSIMAInput = input(true, title='Show RSIMA Cloud', group='Show/Hide Settings')
  12. rsiBandShowInput = input(true, title='Show Oversold/Overbought Lines', group='Show/Hide Settings')
  13. rsiBandExtShowInput = input(true, title='Show Oversold/Overbought Extended Lines', group='Show/Hide Settings')
  14. rsiHighlightShowInput = input(true, title='Show Oversold/Overbought Highlight Lines', group='Show/Hide Settings')
  15. DivergenceShowInput = input(true, title='Show RSI Divergence Labels', group='Show/Hide Settings')
  16.  
  17.  
  18. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  19. //--- RSI Input Settings ----------------------------------------------------------------------------------------------------------------------------------------------
  20. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  21. rsiSourceInput = input.source(close, 'Source', group='RSI Settings')
  22. rsiLengthInput = input.int(14, minval=1, title='RSI Length', group='RSI Settings', tooltip='Here we set the RSI lenght')
  23. rsiColorInput = input.color(#26a69a, title="RSI Color", group='RSI Settings')
  24. rsimaColorInput = input.color(#ef534f, title="RSIMA Color", group='RSI Settings')
  25. rsiBandColorInput = input.color(#787B86, title="RSI Band Color", group='RSI Settings')
  26. rsiUpperBandExtInput = input.int(title='RSI Overbought Extended Line', defval=80, minval=50, maxval=100, group='RSI Settings')
  27. rsiUpperBandInput = input.int(title='RSI Overbought Line', defval=70, minval=50, maxval=100, group='RSI Settings')
  28. rsiLowerBandInput = input.int(title='RSI Oversold Line', defval=30, minval=0, maxval=50, group='RSI Settings')
  29. rsiLowerBandExtInput = input.int(title='RSI Oversold Extended Line', defval=20, minval=0, maxval=50, group='RSI Settings')
  30.  
  31.  
  32. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  33. //--- MA Input Settings -----------------------------------------------------------------------------------------------------------------------------------------------
  34. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  35. maTypeInput = input.string("EMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
  36. maLengthInput = input.int(14, title="MA Length", group="MA Settings")
  37. maColorInput = input.color(color.yellow, title="MA Color", group='MA Settings')
  38.  
  39.  
  40. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  41. //--- Divergence Input Settings ---------------------------------------------------------------------------------------------------------------------------------------
  42. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  43. lbrInput = input(title="Pivot Lookback Right", defval=2, group='RSI Divergence Settings')
  44. lblInput = input(title="Pivot Lookback Left", defval=2, group='RSI Divergence Settings')
  45. lbRangeMaxInput = input(title="Max of Lookback Range", defval=10, group='RSI Divergence Settings')
  46. lbRangeMinInput = input(title="Min of Lookback Range", defval=2, group='RSI Divergence Settings')
  47.  
  48. plotBullInput = input(title="Plot Bullish", defval=true, group='RSI Divergence Settings')
  49. plotHiddenBullInput = input(title="Plot Hidden Bullish", defval=true, group='RSI Divergence Settings')
  50. plotBearInput = input(title="Plot Bearish", defval=true, group='RSI Divergence Settings')
  51. plotHiddenBearInput = input(title="Plot Hidden Bearish", defval=true, group='RSI Divergence Settings')
  52.  
  53. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  54. //--- RSI Calculation -------------------------------------------------------------------------------------------------------------------------------------------------
  55. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  56. rsi = ta.rsi(rsiSourceInput, rsiLengthInput)
  57. rsi15m = request.security(syminfo.tickerid, "15", rsi, lookahead = barmerge.lookahead_on)
  58. rsi1h = request.security(syminfo.tickerid, "60", rsi, lookahead = barmerge.lookahead_on)
  59. rsi4h = request.security(syminfo.tickerid, "240", rsi, lookahead = barmerge.lookahead_on)
  60. rsi1d = request.security(syminfo.tickerid, "1440", rsi, lookahead = barmerge.lookahead_on)
  61.  
  62. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  63. //--- MA Calculation -------------------------------------------------------------------------------------------------------------------------------------------------
  64. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  65. ma(source, length, type) =>
  66. switch type
  67. "SMA" => ta.sma(source, length)
  68. "Bollinger Bands" => ta.sma(source, length)
  69. "EMA" => ta.ema(source, length)
  70. "SMMA (RMA)" => ta.rma(source, length)
  71. "WMA" => ta.wma(source, length)
  72. "VWMA" => ta.vwma(source, length)
  73.  
  74. rsiMA = ma(rsi, maLengthInput, maTypeInput)
  75.  
  76.  
  77. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  78. //--- Stoch RSI Settings + Calculation --------------------------------------------------------------------------------------------------------------------------------
  79. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  80. showStochRSI = input(false, title="Show Stochastic RSI", group='Stochastic RSI Settings')
  81. smoothK = input.int(title="Stochastic K", defval=3, minval=1, maxval=10, group='Stochastic RSI Settings')
  82. smoothD = input.int(title="Stochastic D", defval=4, minval=1, maxval=10, group='Stochastic RSI Settings')
  83. lengthRSI = input.int(title="Stochastic RSI Lenght", defval=14, minval=1, group='Stochastic RSI Settings')
  84. lengthStoch = input.int(title="Stochastic Lenght", defval=14, minval=1, group='Stochastic RSI Settings')
  85.  
  86. rsi1 = request.security(syminfo.tickerid, "15", ta.rsi(rsiSourceInput, lengthRSI))
  87. k = request.security(syminfo.tickerid, "15", ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)) //Blue Line
  88. d = request.security(syminfo.tickerid, "15", ta.sma(k, smoothD)) //Red Line
  89.  
  90. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  91. //--- Divergence Settings ---------------------------------------------------------------------------------------------------------------------------------------------
  92. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  93. bearColor = color.red
  94. bullColor = color.green
  95. hiddenBullColor = color.new(color.green, 50)
  96. hiddenBearColor = color.new(color.red, 50)
  97. textColor = color.white
  98. noneColor = color.new(color.white, 100)
  99.  
  100. osc = rsi
  101. plFound = na(ta.pivotlow(osc, lblInput, lbrInput)) ? false : true
  102. phFound = na(ta.pivothigh(osc, lblInput, lbrInput)) ? false : true
  103.  
  104. _inRange(cond) =>
  105. bars = ta.barssince(cond == true)
  106. lbRangeMinInput <= bars and bars <= lbRangeMaxInput
  107.  
  108. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  109. //--- Define RSI Plot & Line Colors -----------------------------------------------------------------------------------------------------------------------------------
  110. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  111. rsiColor = rsi >= rsiMA ? rsiColorInput : rsimaColorInput
  112.  
  113. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  114. //--- Plot Lines ------------------------------------------------------------------------------------------------------------------------------------------------------
  115. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  116. hline(50, title='RSI Baseline', color=color.new(rsiBandColorInput, 50), linestyle=hline.style_dashed, editable=false)
  117. hline(rsiBandExtShowInput ? rsiUpperBandExtInput : na, title='RSI Upper Band', color=color.new(rsiBandColorInput, 10), linestyle=hline.style_dashed, editable=false)
  118. hline(rsiBandShowInput ? rsiUpperBandInput : na, title='RSI Upper Band', color=color.new(rsiBandColorInput, 10), linestyle=hline.style_dashed, editable=false)
  119. hline(rsiBandShowInput ? rsiLowerBandInput : na, title='RSI Upper Band', color=color.new(rsiBandColorInput, 10), linestyle=hline.style_dashed, editable=false)
  120. hline(rsiBandExtShowInput ? rsiLowerBandExtInput : na, title='RSI Upper Band', color=color.new(rsiBandColorInput, 10), linestyle=hline.style_dashed, editable=false)
  121.  
  122. bgcolor(rsiHighlightShowInput ? rsi >= rsiUpperBandExtInput ? color.new(rsiColorInput, 75) : na : na, title="Show Extended Oversold Highlight", editable=false)
  123. bgcolor(rsiHighlightShowInput ? rsi >= rsiUpperBandInput ? rsi < rsiUpperBandExtInput ? color.new(#64ffda, 90) : na : na: na, title="Show Overbought Highlight", editable=false)
  124. bgcolor(rsiHighlightShowInput ? rsi <= rsiLowerBandInput ? rsi > rsiLowerBandExtInput ? color.new(#F43E32, 90) : na : na : na, title="Show Extended Oversold Highlight", editable=false)
  125. bgcolor(rsiHighlightShowInput ? rsi <= rsiLowerBandInput ? color.new(rsimaColorInput, 75) : na : na, title="Show Oversold Highlight", editable=false)
  126.  
  127. maPlot = plot(maShowInput ? rsiMA : na, title='MA', color=color.new(maColorInput,0), linewidth=1)
  128. rsiMAPlot = plot(showRSIMAInput ? rsiMA : na, title="RSI EMA", color=color.new(rsimaColorInput,0), editable=false, display=display.none)
  129. rsiPlot = plot(rsiShowInput ? rsi : na, title='RSI', color=color.new(rsiColor,0), linewidth=1)
  130. fill(rsiPlot, rsiMAPlot, color=color.new(rsiColor, 70), title="RSIMA Cloud")
  131.  
  132. plot(showStochRSI ? k : na, title='Stochastic K', color=color.new(color.blue,50), linewidth=1)
  133. plot(showStochRSI ? d : na, title='Stochastic D', color=color.new(color.red,50), linewidth=1)
  134.  
  135.  
  136. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  137. //--- Plot Bullish Divergence - Higher Low / Lower Low ----------------------------------------------------------------------------------------------------------------
  138. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  139. oscHL = osc[lbrInput] > ta.valuewhen(plFound, osc[lbrInput], 1) and _inRange(plFound[1])
  140.  
  141. priceLL = low[lbrInput] < ta.valuewhen(plFound, low[lbrInput], 1)
  142. bullCond = plotBullInput and priceLL and oscHL and plFound
  143.  
  144. plot(
  145. plFound ? osc[lbrInput] : na,
  146. offset=-lbrInput,
  147. title="Regular Bullish",
  148. linewidth=2,
  149. color=(bullCond ? bullColor : noneColor)
  150. )
  151.  
  152. plotshape(
  153. DivergenceShowInput ? bullCond ? osc[lbrInput] : na : na,
  154. offset=-lbrInput,
  155. title="Regular Bullish Label",
  156. text=" Bull ",
  157. style=shape.labelup,
  158. location=location.absolute,
  159. color=bullColor,
  160. textcolor=textColor
  161. )
  162.  
  163. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  164. //--- Plot Hidden Bullish Divergence - Lower Low / Higher Low ---------------------------------------------------------------------------------------------------------
  165. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  166. oscLL = osc[lbrInput] < ta.valuewhen(plFound, osc[lbrInput], 1) and _inRange(plFound[1])
  167.  
  168. priceHL = low[lbrInput] > ta.valuewhen(plFound, low[lbrInput], 1)
  169. hiddenBullCond = plotHiddenBullInput and priceHL and oscLL and plFound
  170.  
  171. plot(
  172. plFound ? osc[lbrInput] : na,
  173. offset=-lbrInput,
  174. title="Hidden Bullish",
  175. linewidth=2,
  176. color=(hiddenBullCond ? hiddenBullColor : noneColor)
  177. )
  178.  
  179. plotshape(
  180. DivergenceShowInput ? hiddenBullCond ? osc[lbrInput] : na : na,
  181. offset=-lbrInput,
  182. title="Hidden Bullish Label",
  183. text=" H Bull ",
  184. style=shape.labelup,
  185. location=location.absolute,
  186. color=bullColor,
  187. textcolor=textColor
  188. )
  189.  
  190. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  191. //--- Plot Bearish Divergence - Lower High / Higher High --------------------------------------------------------------------------------------------------------------
  192. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  193. oscLH = osc[lbrInput] < ta.valuewhen(phFound, osc[lbrInput], 1) and _inRange(phFound[1])
  194.  
  195. // Price: Higher High
  196. priceHH = high[lbrInput] > ta.valuewhen(phFound, high[lbrInput], 1)
  197. bearCond = plotBearInput and priceHH and oscLH and phFound
  198.  
  199. plot(
  200. phFound ? osc[lbrInput] : na,
  201. offset=-lbrInput,
  202. title="Regular Bearish",
  203. linewidth=2,
  204. color=(bearCond ? bearColor : noneColor)
  205. )
  206.  
  207. plotshape(
  208. DivergenceShowInput ? bearCond ? osc[lbrInput] : na : na,
  209. offset=-lbrInput,
  210. title="Regular Bearish Label",
  211. text=" Bear ",
  212. style=shape.labeldown,
  213. location=location.absolute,
  214. color=bearColor,
  215. textcolor=textColor
  216. )
  217.  
  218. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  219. //--- Plot Hidden Bearish Divergence - Higher High / Lower High -------------------------------------------------------------------------------------------------------
  220. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  221. oscHH = osc[lbrInput] > ta.valuewhen(phFound, osc[lbrInput], 1) and _inRange(phFound[1])
  222.  
  223. // Price: Lower High
  224. priceLH = high[lbrInput] < ta.valuewhen(phFound, high[lbrInput], 1)
  225. hiddenBearCond = plotHiddenBearInput and priceLH and oscHH and phFound
  226.  
  227. plot(
  228. phFound ? osc[lbrInput] : na,
  229. offset=-lbrInput,
  230. title="Hidden Bearish",
  231. linewidth=2,
  232. color=(hiddenBearCond ? hiddenBearColor : noneColor)
  233. )
  234.  
  235. plotshape(
  236. DivergenceShowInput ? hiddenBearCond ? osc[lbrInput] : na : na,
  237. offset=-lbrInput,
  238. title="Hidden Bearish Label",
  239. text=" H Bear ",
  240. style=shape.labeldown,
  241. location=location.absolute,
  242. color=bearColor,
  243. textcolor=textColor
  244. )
  245.  
  246.  
  247. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  248. //--- RSI Info Table Setting ------------------------------------------------------------------------------------------------------------------------------------------
  249. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  250. width_c0 = 0
  251. width_c1 = 0
  252.  
  253. if rsiShowInput
  254. var rsi_Table = table.new(position=position.middle_right, columns=2, rows=5, bgcolor=color.rgb(54,58,69,50), frame_color=color.black, frame_width=2, border_width=0)
  255.  
  256. table.cell(table_id=rsi_Table, column=0, row=0, text=" RSI Current:", width=width_c0, text_color=color.white, text_halign='right')
  257. table.cell(table_id=rsi_Table, column=0, row=1, text=" RSI 15m:",width=width_c0, text_color=color.orange, text_halign='right')
  258. table.cell(table_id=rsi_Table, column=0, row=2, text=" RSI 1h:",width=width_c0, text_color=color.orange, text_halign='right')
  259. table.cell(table_id=rsi_Table, column=0, row=3, text=" RSI 4h:",width=width_c0, text_color=color.orange, text_halign='right')
  260. table.cell(table_id=rsi_Table, column=0, row=4, text=" RSI 1d:",width=width_c0, text_color=color.orange, text_halign='right')
  261.  
  262. table.cell(table_id=rsi_Table, column=1, row=0, text=str.format("{0,number,#.##} ", rsi), width=width_c1, text_color=(rsi < 50 ? color.red:color.green), text_halign='left')
  263. table.cell(table_id=rsi_Table, column=1, row=1, text=str.format("{0,number,#.##} ", rsi15m), width=width_c1, text_color=(rsi15m < 50 ? color.red:color.green), text_halign='left')
  264. table.cell(table_id=rsi_Table, column=1, row=2, text=str.format("{0,number,#.##} ", rsi1h), width=width_c1, text_color=(rsi1h < 50 ? color.red:color.green), text_halign='left')
  265. table.cell(table_id=rsi_Table, column=1, row=3, text=str.format("{0,number,#.##} ", rsi4h), width=width_c1, text_color=(rsi4h < 50 ? color.red:color.green), text_halign='left')
  266. table.cell(table_id=rsi_Table, column=1, row=4, text=str.format("{0,number,#.##} ", rsi1d), width=width_c1, text_color=(rsi1d < 50 ? color.red:color.green), text_halign='left')
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement