xmd79

[HC] Tripple RSI & Divergence

Dec 4th, 2022
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. //@version=4
  2. //Script snipets & inspiration from Chris Moody, Libertus and Sander
  3. //Triple RSI Indicator with Divergence
  4. //Edited by Henri Cormier
  5. study(title="[HC] Tripple RSI & Divergence", shorttitle="[HC] 3 RSI & DIV", overlay=false)
  6. src = input(close)
  7. len2 = input(2, minval=1, title="RSI Length 2")
  8. len5 = input(7, minval=1, title="RSI Length 7")
  9. len14 = input(14, minval=1, title="RSI Length 14")
  10. rsi2 = rsi(src, len2)
  11. rsi5 = rsi(src, len5)
  12. rsi14 = rsi(src, len14)
  13.  
  14. //xbars = input(14, "Div lookback period (bars)?", integer, minval=1)
  15. //hb = abs(highestbars(rsi14, xbars)) // Finds bar with highest value in last X bars
  16. //lb = abs(lowestbars(rsi14, xbars)) // Finds bar with lowest value in last X bars
  17.  
  18. cciP = input(20, title="CCI Period Length")
  19. rsiP = input(20, title="RSI Period Length")
  20. cciThreshold = input(0, title="CCI threshold for color coding")
  21.  
  22. change_1 = change(src)
  23. obv = cum(change(src) > 0 ? volume : change_1 < 0 ? -volume : 0 * volume)
  24. c = cci(src, cciP)
  25. rsiobv = float(na)
  26. rsiobv := rsi(obv, rsiP)
  27. plot(rsiobv, color=c >= cciThreshold ? color.lime : color.red, title="RSIa", transp=1, linewidth=1)
  28. alertcondition(rsiobv > 70 or rsiobv < 30, title="RSIOBV L/S Trigger", message="RSIOBV_LS")
  29.  
  30. // Plots
  31. //plot(rsi2, title="RSI2", style=line, linewidth=1, color=red, transp=0)
  32. //plot(rsi5, title="RSI5", style=line, linewidth=1, color=lime, transp=0)
  33. plot(rsi14, title="RSI14", style=plot.style_line, linewidth=1, color=color.yellow, transp=0)
  34. plot(sma(rsi14, 9), title="RSI SMA", style=plot.style_line, linewidth=1, color=#8146ff, transp=0) //Purple
  35. plot(ema(rsi14, 45), title="RSI SMA", style=plot.style_line, linewidth=1, color=color.aqua, transp=0) //Aqua
  36.  
  37. emaCrossUp = false
  38. emaCrossUp := crossover(sma(rsi14, 9), ema(rsi14, 45))
  39. emaCrossDown = false
  40. emaCrossDown := crossunder(sma(rsi14, 9), ema(rsi14, 45))
  41. plotshape(emaCrossUp ? emaCrossUp : na, title="ema cross up", color=color.lime, style=shape.diamond, location=location.top)
  42. plotshape(emaCrossDown ? emaCrossDown : na, title="ema cross down", color=color.red, style=shape.diamond, location=location.top)
  43. plotshape(c >= cciThreshold and c[1] < cciThreshold[1] ? true : na, title="RSI OVB Cross", style=shape.arrowup, location=location.top, color=color.lime)
  44. plotshape(c < cciThreshold and c[1] >= cciThreshold[1] ? true : na, title="RSI OVB Cross", style=shape.arrowdown, location=location.top, color=color.red)
  45.  
  46. band80 = hline(80, title="BU80", linestyle=hline.style_dotted, linewidth=1, color=color.green)
  47. band70 = hline(70, title="BU70", linestyle=hline.style_dotted, linewidth=1, color=color.gray)
  48. band40 = hline(40, title="BU40", linestyle=hline.style_dotted, linewidth=1, color=color.green)
  49. band50 = hline(50, title="BU50", linestyle=hline.style_dotted, linewidth=1, color=color.gray)
  50. band60 = hline(60, title="BE60", linestyle=hline.style_dotted, linewidth=1, color=color.red)
  51. band30 = hline(30, title="BU30", linestyle=hline.style_dotted, linewidth=1, color=color.gray)
  52. band20 = hline(20, title="BE20", linestyle=hline.style_dotted, linewidth=1, color=color.red)
  53. bgcolor(rsi2 <= 10 ? color.green : rsi2 >= 90 ? color.red : color.black, transp=85)
  54. bgcolor(rsi5 <= 20 ? color.green : rsi5 >= 80 ? color.red : color.black, transp=75)
  55. bgcolor(rsi14 <= 30 ? color.green : rsi14 >= 70 ? color.red : color.black, transp=65)
  56. rsiLong = rsi2 <= 10 and rsi5 <= 20 and rsi14 <= 30
  57. rsiShort = rsi2 >= 90 and rsi5 >= 80 and rsi14 >= 70
  58.  
  59. // *** RSI Divergence START ***
  60. length = input(45)
  61.  
  62. f_top_fractal(_src) =>
  63. _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and
  64. _src[2] > _src[0]
  65. f_bot_fractal(_src) =>
  66. _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and
  67. _src[2] < _src[0]
  68. f_fractalize(_src) =>
  69. f_bot_fractal__1 = f_bot_fractal(_src)
  70. f_top_fractal(_src) ? 1 : f_bot_fractal__1 ? -1 : 0
  71.  
  72. rsi_high = rsi(high, length)
  73. rsi_low = rsi(low, length)
  74. fractal_top_rsi = f_fractalize(rsi_high) > 0 ? rsi_high[2] : na
  75. fractal_bot_rsi = f_fractalize(rsi_low) < 0 ? rsi_low[2] : na
  76.  
  77. rsi_high_prev = valuewhen(fractal_top_rsi, rsi_high[2], 1)
  78. rsi_high_price = valuewhen(fractal_top_rsi, high[2], 1)
  79. rsi_low_prev = valuewhen(fractal_bot_rsi, rsi_low[2], 1)
  80. rsi_low_price = valuewhen(fractal_bot_rsi, low[2], 1)
  81.  
  82. regular_bearish_div = fractal_top_rsi and high[2] > rsi_high_price and rsi_high[2] < rsi_high_prev
  83. hidden_bearish_div = fractal_top_rsi and high[2] < rsi_high_price and rsi_high[2] > rsi_high_prev
  84. regular_bullish_div = fractal_bot_rsi and low[2] < rsi_low_price and rsi_low[2] > rsi_low_prev
  85. hidden_bullish_div = fractal_bot_rsi and low[2] > rsi_low_price and rsi_low[2] < rsi_low_prev
  86.  
  87. plotshape(title='+RBD', series=regular_bearish_div ? rsi_high[2] : na, text='R\n▼', style=shape.labeldown, location=location.absolute, color=color.maroon, textcolor=color.white, offset=-2)
  88. plotshape(title='+HBD', series=hidden_bearish_div ? rsi_high[2] : na, text='H\n▼', style=shape.labeldown, location=location.absolute, color=color.maroon, textcolor=color.white, offset=-2)
  89. plotshape(title='-RBD', series=regular_bullish_div ? rsi_low[2] : na, text='▲\nR', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, offset=-2)
  90. plotshape(title='-HBD', series=hidden_bullish_div ? rsi_low[2] : na, text='▲\nH', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, offset=-2)
  91. // *** RSI Divergence END ***
  92.  
  93. //***** ALERTS START *****//
  94. alertcondition(regular_bullish_div or hidden_bullish_div, title="RSI Bull Div", message="RSI Bull Div")
  95. alertcondition(regular_bearish_div or hidden_bearish_div, title="RSI Bear Div", message="RSI Bear Div")
  96. alertcondition(rsiLong, title="RSI Long", message="RSI Long")
  97. alertcondition(rsiShort, title="RSI Short", message="RSI Short")
  98. //***** ALERTS END *****//
  99.  
Add Comment
Please, Sign In to add comment