0% found this document useful (0 votes)
194 views

Double RSI

The document defines an indicator for calculating and plotting the Relative Strength Index (RSI) along with moving averages. It allows customizing RSI and moving average parameters. The indicator also identifies and labels bullish and bearish divergence patterns by comparing RSI values with price action over a configurable lookback period. Alerts are triggered when divergences are detected.

Uploaded by

Rohit Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
194 views

Double RSI

The document defines an indicator for calculating and plotting the Relative Strength Index (RSI) along with moving averages. It allows customizing RSI and moving average parameters. The indicator also identifies and labels bullish and bearish divergence patterns by comparing RSI values with price action over a configurable lookback period. Alerts are triggered when divergences are detected.

Uploaded by

Rohit Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

//@version=5

indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2,


timeframe="", timeframe_gaps=true)

ma(source, length, type) =>

switch type

"SMA" => ta.sma(source, length)

"Bollinger Bands" => ta.sma(source, length)

"EMA" => ta.ema(source, length)

"SMMA (RMA)" => ta.rma(source, length)

"WMA" => ta.wma(source, length)

"VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")

rsiSourceInput = input.source(close, "Source", group="RSI Settings")

maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA",


"SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")

maLengthInput = input.int(14, title="MA Length", group="MA Settings")

bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

showDivergence = input.bool(false, title="Show Divergence", group="RSI Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)

down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)

rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

rsiMA = ma(rsi, maLengthInput, maTypeInput)

isBB = maTypeInput == "Bollinger Bands"

rsiPlot = plot(rsi, "RSI", color=#7E57C2)

plot(rsiMA, "RSI-based MA", color=color.yellow)

rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)

midline = hline(50, "RSI Middle Band", color=color.new(#787B86, 50))

rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)


fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")

bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper


Bollinger Band", color=color.green)

bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower


Bollinger Band", color=color.green)

fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands


Background Fill")

midLinePlot = plot(50, color = na, editable = false, display = display.none)

fill(rsiPlot, midLinePlot, 100, 70, top_color = color.new(color.green, 0), bottom_color =


color.new(color.green, 100), title = "Overbought Gradient Fill")

fill(rsiPlot, midLinePlot, 30, 0, top_color = color.new(color.red, 100), bottom_color =


color.new(color.red, 0), title = "Oversold Gradient Fill")

// Divergence

lookbackRight = 5

lookbackLeft = 5

rangeUpper = 60

rangeLower = 5

bearColor = color.red

bullColor = color.green

textColor = color.white

noneColor = color.new(color.white, 100)

plFound = na(ta.pivotlow(rsi, lookbackLeft, lookbackRight)) ? false : true

phFound = na(ta.pivothigh(rsi, lookbackLeft, lookbackRight)) ? false : true

_inRange(cond) =>

bars = ta.barssince(cond == true)

rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------

// Regular Bullish

// rsi: Higher Low


rsiHL = rsi[lookbackRight] > ta.valuewhen(plFound, rsi[lookbackRight], 1) and _inRange(plFound[1])

// Price: Lower Low

priceLL = low[lookbackRight] < ta.valuewhen(plFound, low[lookbackRight], 1)

bullCondAlert = priceLL and rsiHL and plFound

bullCond = showDivergence and bullCondAlert

plot(

plFound ? rsi[lookbackRight] : na,

offset=-lookbackRight,

title="Regular Bullish",

linewidth=2,

color=(bullCond ? bullColor : noneColor)

plotshape(

bullCond ? rsi[lookbackRight] : na,

offset=-lookbackRight,

title="Regular Bullish Label",

text=" Bull ",

style=shape.labelup,

location=location.absolute,

color=bullColor,

textcolor=textColor

//------------------------------------------------------------------------------

// Regular Bearish

// rsi: Lower High


rsiLH = rsi[lookbackRight] < ta.valuewhen(phFound, rsi[lookbackRight], 1) and _inRange(phFound[1])

// Price: Higher High

priceHH = high[lookbackRight] > ta.valuewhen(phFound, high[lookbackRight], 1)

bearCondAlert = priceHH and rsiLH and phFound

bearCond = showDivergence and bearCondAlert

plot(

phFound ? rsi[lookbackRight] : na,

offset=-lookbackRight,

title="Regular Bearish",

linewidth=2,

color=(bearCond ? bearColor : noneColor)

plotshape(

bearCond ? rsi[lookbackRight] : na,

offset=-lookbackRight,

title="Regular Bearish Label",

text=" Bear ",

style=shape.labeldown,

location=location.absolute,

color=bearColor,

textcolor=textColor

alertcondition(bullCondAlert, title='Regular Bullish Divergence', message="Found a new Regular


Bullish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.")
alertcondition(bearCondAlert, title='Regular Bearish Divergence', message='Found a new Regular
Bearish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.')

//@version=5

//indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2,


timeframe="", timeframe_gaps=true)

maa(source, length, type) =>

switch type

"SMA" => ta.sma(source, length)

"Bollinger Bands" => ta.sma(source, length)

"EMA" => ta.ema(source, length)

"SMMA (RMA)" => ta.rma(source, length)

"WMA" => ta.wma(source, length)

"VWMA" => ta.vwma(source, length)

rsiLengthInputt = input.int(14, minval=1, title="RSI Length", group="RSI Settings")

rsiSourceInputt = input.source(close, "Source", group="RSI Settings")

maTypeInputt = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA",


"SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")

maLengthInputt = input.int(14, title="MA Length", group="MA Settings")

bbMultInputt = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

showDivergencee = input.bool(false, title="Show Divergence", group="RSI Settings")

upp = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)

downn = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)

rsii = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

rsiMAA = maTypeInput == "Bollinger Bands"

rsiPlott = plot(rsi, "RSI", color=#7E57C2)

plot(rsiMA, "RSI-based MA", color=color.yellow)


rsiUpperBandd = hline(70, "RSI Upper Band", color=#787B86)

midlinee = hline(50, "RSI Middle Band", color=color.new(#787B86, 50))

rsiLowerBandd = hline(30, "RSI Lower Band", color=#787B86)

fill(rsiUpperBandd, rsiLowerBandd, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")

bbUpperBandd = plot(isBB ? rsiMAA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper


Bollinger Band", color=color.green)

bbLowerBandd = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower


Bollinger Band", color=color.green)

fill(bbUpperBandd, bbLowerBandd, color= isBB ? color.new(color.green, 90) : na, title="Bollinger


Bands Background Fill")

midLinePlott = plot(50, color = na, editable = false, display = display.none)

fill(rsiPlott, midLinePlott, 100, 70, top_color = color.new(color.green, 0), bottom_color =


color.new(color.green, 100), title = "Overbought Gradient Fill")

fill(rsiPlott, midLinePlott, 30, 0, top_color = color.new(color.red, 100), bottom_color =


color.new(color.red, 0), title = "Oversold Gradient Fill")

// Divergence

lookbackRightt = 5

lookbackLeftt = 5

rangeUpperr = 60

rangeLowerr = 5

bearColorr = color.red

bullColorr = color.green

textColorr = color.white

noneColorr = color.new(color.white, 100)

plFoundd = na(ta.pivotlow(rsii, lookbackLeftt, lookbackRightt)) ? false : true

phFoundd = na(ta.pivothigh(rsii, lookbackLeftt, lookbackRightt)) ? false : true

_iinRange(cond) =>

bars = ta.barssince(cond == true)

rangeLower <= bars and bars <= rangeUpper


//------------------------------------------------------------------------------

// Regular Bullish

// rsi: Higher Low

rsiHLL = rsi[lookbackRight] > ta.valuewhen(plFound, rsi[lookbackRight], 1) and _inRange(plFound[1])

// Price: Lower Low

priceLL = low[lookbackRight] < ta.valuewhen(plFound, low[lookbackRight], 1)

bullCondAlert = priceLL and rsiHL and plFound

bullCond = showDivergence and bullCondAlert

plot(

plFound ? rsi[lookbackRight] : na,

offset=-lookbackRight,

title="Regular Bullish",

linewidth=2,

color=(bullCond ? bullColor : noneColor)

plotshape(

bullCond ? rsi[lookbackRight] : na,

offset=-lookbackRight,

title="Regular Bullish Label",

text=" Bull ",

style=shape.labelup,

location=location.absolute,

color=bullColor,

textcolor=textColor

)
//------------------------------------------------------------------------------

// Regular Bearish

// rsi: Lower High

rsiLH = rsi[lookbackRight] < ta.valuewhen(phFound, rsi[lookbackRight], 1) and _inRange(phFound[1])

// Price: Higher High

priceHH = high[lookbackRight] > ta.valuewhen(phFound, high[lookbackRight], 1)

bearCondAlert = priceHH and rsiLH and phFound

bearCond = showDivergence and bearCondAlert

plot(

phFound ? rsi[lookbackRight] : na,

offset=-lookbackRight,

title="Regular Bearish",

linewidth=2,

color=(bearCond ? bearColor : noneColor)

plotshape(

bearCond ? rsi[lookbackRight] : na,

offset=-lookbackRight,

title="Regular Bearish Label",

text=" Bear ",

style=shape.labeldown,

location=location.absolute,

color=bearColor,

textcolor=textColor

)
alertcondition(bullCondAlert, title='Regular Bullish Divergence', message="Found a new Regular
Bullish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.")

alertcondition(bearCondAlert, title='Regular Bearish Divergence', message='Found a new Regular


Bearish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.')

You might also like