Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- //Script snipets & inspiration from Chris Moody, Libertus and Sander
- //Triple RSI Indicator with Divergence
- //Edited by Henri Cormier
- study(title="[HC] Tripple RSI & Divergence", shorttitle="[HC] 3 RSI & DIV", overlay=false)
- src = input(close)
- len2 = input(2, minval=1, title="RSI Length 2")
- len5 = input(7, minval=1, title="RSI Length 7")
- len14 = input(14, minval=1, title="RSI Length 14")
- rsi2 = rsi(src, len2)
- rsi5 = rsi(src, len5)
- rsi14 = rsi(src, len14)
- //xbars = input(14, "Div lookback period (bars)?", integer, minval=1)
- //hb = abs(highestbars(rsi14, xbars)) // Finds bar with highest value in last X bars
- //lb = abs(lowestbars(rsi14, xbars)) // Finds bar with lowest value in last X bars
- cciP = input(20, title="CCI Period Length")
- rsiP = input(20, title="RSI Period Length")
- cciThreshold = input(0, title="CCI threshold for color coding")
- change_1 = change(src)
- obv = cum(change(src) > 0 ? volume : change_1 < 0 ? -volume : 0 * volume)
- c = cci(src, cciP)
- rsiobv = float(na)
- rsiobv := rsi(obv, rsiP)
- plot(rsiobv, color=c >= cciThreshold ? color.lime : color.red, title="RSIa", transp=1, linewidth=1)
- alertcondition(rsiobv > 70 or rsiobv < 30, title="RSIOBV L/S Trigger", message="RSIOBV_LS")
- // Plots
- //plot(rsi2, title="RSI2", style=line, linewidth=1, color=red, transp=0)
- //plot(rsi5, title="RSI5", style=line, linewidth=1, color=lime, transp=0)
- plot(rsi14, title="RSI14", style=plot.style_line, linewidth=1, color=color.yellow, transp=0)
- plot(sma(rsi14, 9), title="RSI SMA", style=plot.style_line, linewidth=1, color=#8146ff, transp=0) //Purple
- plot(ema(rsi14, 45), title="RSI SMA", style=plot.style_line, linewidth=1, color=color.aqua, transp=0) //Aqua
- emaCrossUp = false
- emaCrossUp := crossover(sma(rsi14, 9), ema(rsi14, 45))
- emaCrossDown = false
- emaCrossDown := crossunder(sma(rsi14, 9), ema(rsi14, 45))
- plotshape(emaCrossUp ? emaCrossUp : na, title="ema cross up", color=color.lime, style=shape.diamond, location=location.top)
- plotshape(emaCrossDown ? emaCrossDown : na, title="ema cross down", color=color.red, style=shape.diamond, location=location.top)
- plotshape(c >= cciThreshold and c[1] < cciThreshold[1] ? true : na, title="RSI OVB Cross", style=shape.arrowup, location=location.top, color=color.lime)
- plotshape(c < cciThreshold and c[1] >= cciThreshold[1] ? true : na, title="RSI OVB Cross", style=shape.arrowdown, location=location.top, color=color.red)
- band80 = hline(80, title="BU80", linestyle=hline.style_dotted, linewidth=1, color=color.green)
- band70 = hline(70, title="BU70", linestyle=hline.style_dotted, linewidth=1, color=color.gray)
- band40 = hline(40, title="BU40", linestyle=hline.style_dotted, linewidth=1, color=color.green)
- band50 = hline(50, title="BU50", linestyle=hline.style_dotted, linewidth=1, color=color.gray)
- band60 = hline(60, title="BE60", linestyle=hline.style_dotted, linewidth=1, color=color.red)
- band30 = hline(30, title="BU30", linestyle=hline.style_dotted, linewidth=1, color=color.gray)
- band20 = hline(20, title="BE20", linestyle=hline.style_dotted, linewidth=1, color=color.red)
- bgcolor(rsi2 <= 10 ? color.green : rsi2 >= 90 ? color.red : color.black, transp=85)
- bgcolor(rsi5 <= 20 ? color.green : rsi5 >= 80 ? color.red : color.black, transp=75)
- bgcolor(rsi14 <= 30 ? color.green : rsi14 >= 70 ? color.red : color.black, transp=65)
- rsiLong = rsi2 <= 10 and rsi5 <= 20 and rsi14 <= 30
- rsiShort = rsi2 >= 90 and rsi5 >= 80 and rsi14 >= 70
- // *** RSI Divergence START ***
- length = input(45)
- f_top_fractal(_src) =>
- _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and
- _src[2] > _src[0]
- f_bot_fractal(_src) =>
- _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and
- _src[2] < _src[0]
- f_fractalize(_src) =>
- f_bot_fractal__1 = f_bot_fractal(_src)
- f_top_fractal(_src) ? 1 : f_bot_fractal__1 ? -1 : 0
- rsi_high = rsi(high, length)
- rsi_low = rsi(low, length)
- fractal_top_rsi = f_fractalize(rsi_high) > 0 ? rsi_high[2] : na
- fractal_bot_rsi = f_fractalize(rsi_low) < 0 ? rsi_low[2] : na
- rsi_high_prev = valuewhen(fractal_top_rsi, rsi_high[2], 1)
- rsi_high_price = valuewhen(fractal_top_rsi, high[2], 1)
- rsi_low_prev = valuewhen(fractal_bot_rsi, rsi_low[2], 1)
- rsi_low_price = valuewhen(fractal_bot_rsi, low[2], 1)
- regular_bearish_div = fractal_top_rsi and high[2] > rsi_high_price and rsi_high[2] < rsi_high_prev
- hidden_bearish_div = fractal_top_rsi and high[2] < rsi_high_price and rsi_high[2] > rsi_high_prev
- regular_bullish_div = fractal_bot_rsi and low[2] < rsi_low_price and rsi_low[2] > rsi_low_prev
- hidden_bullish_div = fractal_bot_rsi and low[2] > rsi_low_price and rsi_low[2] < rsi_low_prev
- 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)
- 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)
- 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)
- 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)
- // *** RSI Divergence END ***
- //***** ALERTS START *****//
- alertcondition(regular_bullish_div or hidden_bullish_div, title="RSI Bull Div", message="RSI Bull Div")
- alertcondition(regular_bearish_div or hidden_bearish_div, title="RSI Bear Div", message="RSI Bear Div")
- alertcondition(rsiLong, title="RSI Long", message="RSI Long")
- alertcondition(rsiShort, title="RSI Short", message="RSI Short")
- //***** ALERTS END *****//
Add Comment
Please, Sign In to add comment