Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- // Copyright by Libertus - 2018
- // RSI Divergences v2.3
- // Free for private use
- study(title="Relative Strength Index - Divergences - Libertus", shorttitle="RSI Div - Lib")
- len = input(14, minval=1, title="RSI Length")
- ob = input(70, "Overbought", integer, minval=0, maxval=100)
- os = input(30, "Oversold", integer, minval=0, maxval=100)
- // RSI code
- rsi = rsi(close, len)
- band1 = hline(ob)
- band0 = hline(os)
- plot(rsi, color=(rsi > ob or rsi < os ? red : black), transp=0)
- fill(band1, band0, color=purple, transp=97)
- // DIVS code
- piv = input(false,"Hide pivots?")
- shrt = input(false,"Shorter labels?")
- xbars = input(90, "Div lookback period (bars)?", integer, minval=1)
- hb = abs(highestbars(rsi, xbars)) // Finds bar with highest value in last X bars
- lb = abs(lowestbars(rsi, xbars)) // Finds bar with lowest value in last X bars
- // Defining variable values, mandatory in Pine 3
- max = na
- max_rsi = na
- min = na
- min_rsi = na
- pivoth = na
- pivotl = na
- divbear = na
- divbull = na
- // If bar with lowest / highest is current bar, use it's value
- max := hb == 0 ? close : na(max[1]) ? close : max[1]
- max_rsi := hb == 0 ? rsi : na(max_rsi[1]) ? rsi : max_rsi[1]
- min := lb == 0 ? close : na(min[1]) ? close : min[1]
- min_rsi := lb == 0 ? rsi : na(min_rsi[1]) ? rsi : min_rsi[1]
- // Compare high of current bar being examined with previous bar's high
- // If curr bar high is higher than the max bar high in the lookback window range
- if close > max // we have a new high
- max := close // change variable "max" to use current bar's high value
- if rsi > max_rsi // we have a new high
- max_rsi := rsi // change variable "max_rsi" to use current bar's RSI value
- if close < min // we have a new low
- min := close // change variable "min" to use current bar's low value
- if rsi < min_rsi // we have a new low
- min_rsi := rsi // change variable "min_rsi" to use current bar's RSI value
- // Finds pivot point with at least 2 right candles with lower value
- pivoth := (max_rsi == max_rsi[2]) and (max_rsi[2] != max_rsi[3]) ? true : na
- pivotl := (min_rsi == min_rsi[2]) and (min_rsi[2] != min_rsi[3]) ? true : na
- // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
- if (max[1] > max[2]) and (rsi[1] < max_rsi) and (rsi <= rsi[1])
- divbear := true
- if (min[1] < min[2]) and (rsi[1] > min_rsi) and (rsi >= rsi[1])
- divbull := true
- // Alerts
- alertcondition(divbear, title='Bear div', message='Bear div')
- alertcondition(divbull, title='Bull div', message='Bull div')
- // Plots divergences and pivots with offest
- // Longer labels
- plotshape(shrt ? na : divbear ? rsi[1] + 1 : na, location=location.absolute, style=shape.labeldown, color=red, size=size.tiny, text="Bear", textcolor=white, transp=0, offset=-1)
- plotshape(shrt ? na : divbull ? rsi[1] - 1 : na, location=location.absolute, style=shape.labelup, color=green, size=size.tiny, text="Bull", textcolor=white, transp=0, offset=-1)
- plotshape(piv ? na : shrt ? na : pivoth ? max_rsi + 1 : na, location=location.absolute, style=shape.labeldown, color=blue, size=size.tiny, text="Pivot", textcolor=white, transp=0, offset=-2)
- plotshape(piv ? na : shrt ? na : pivotl ? min_rsi - 1 : na, location=location.absolute, style=shape.labelup, color=blue, size=size.tiny, text="Pivot", textcolor=white, transp=0, offset=-2)
- // Shorter labels
- plotshape(shrt ? (divbear ? rsi[1] + 3 : na) : na, location=location.absolute, style=shape.triangledown, color=red, size=size.tiny, transp=0, offset=-1)
- plotshape(shrt ? (divbull ? rsi[1] - 3 : na) : na, location=location.absolute, style=shape.triangleup, color=green, size=size.tiny, transp=0, offset=-1)
- plotshape(piv ? na : shrt ? (pivoth ? max_rsi + 3 : na) : na, location=location.absolute, style=shape.triangledown, color=blue, size=size.tiny, transp=0, offset=-2)
- plotshape(piv ? na : shrt ? (pivotl ? min_rsi - 3 : na) : na, location=location.absolute, style=shape.triangleup, color=blue, size=size.tiny, transp=0, offset=-2)
- // Debug tools
- // plot(max, color=blue, linewidth=2)
- // plot(max_rsi, color=red, linewidth=2)
- // plot(hb, color=orange, linewidth=2)
- // plot(lb, color=purple, linewidth=1)
- // plot(min_rsi, color=lime, linewidth=1)
- // plot(min, color=black, linewidth=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement