Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Sanctom
- //@version=4
- study("RSI+ [RSX DIV CSRI]", shorttitle="RS+")
- length = input(title="Length", type=input.integer, defval=14)
- RSXsrc = input(title="Source", type=input.source, defval=hlc3)
- obLevel = input(title="OB Level", type=input.integer, defval=80)
- osLevel = input(title="OS Level", type=input.integer, defval=25)
- highlightBreakouts = input(title="Highlight Overbought/Oversold Breakouts ?", type=input.bool, defval=false)
- piv = input(false, "Hide pivots?")
- shrt = input(false, "Shorter labels?")
- xbars = input(28, "Div lookback period (bars)?", input.integer, minval=1)
- len = input(title="RSI Period", minval=1, defval=9)
- src = input(title="RSI Source", defval=close)
- lbR = input(title="Pivot Lookback Right", defval=5)
- lbL = input(title="Pivot Lookback Left", defval=4)
- rangeUpper = input(title="Max of Lookback Range", defval=60)
- rangeLower = input(title="Min of Lookback Range", defval=5)
- plotBull = input(title="Plot Bullish", defval=true)
- plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
- plotBear = input(title="Plot Bearish", defval=true)
- plotHiddenBear = input(title="Plot Hidden Bearish", defval=true)
- lenrsi = input(3, "RSI Length")
- lenupdown = input(2, "UpDown Length")
- lenroc = input(100, "ROC Length")
- updown(s) =>
- isEqual = s == s[1]
- isGrowing = s > s[1]
- ud = 0.0
- ud := isEqual ? 0 : isGrowing ? (nz(ud[1]) <= 0 ? 1 : nz(ud[1])+1) : (nz(ud[1]) >= 0 ? -1 : nz(ud[1])-1)
- ud
- rsi = rsi(src, lenrsi)
- updownrsi = rsi(updown(src), lenupdown)
- percentrank = percentrank(roc(src, 1), lenroc)
- crsi = avg(rsi, updownrsi, percentrank)
- bearColor = #CB0000
- bullColor = #008100
- hiddenBullColor = color.new(color.green, 80)
- hiddenBearColor = color.new(color.red, 80)
- textColor = color.white
- noneColor = color.new(color.white, 100)
- osc = rsi(src, len)
- plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
- phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
- _inRange(cond) =>
- bars = barssince(cond == true)
- rangeLower <= bars and bars <= rangeUpper
- //------------------------------------------------------------------------------
- // Regular Bullish
- // Osc: Higher Low
- oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
- // Price: Lower Low
- priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
- bullCond = plotBull and priceLL and oscHL and plFound
- //------------------------------------------------------------------------------
- // Hidden Bullish
- // Osc: Lower Low
- oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
- // Price: Higher Low
- priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
- hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
- //------------------------------------------------------------------------------
- // Regular Bearish
- // Osc: Lower High
- oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
- // Price: Higher High
- priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
- bearCond = plotBear and priceHH and oscLH and phFound
- //------------------------------------------------------------------------------
- // Hidden Bearish
- // Osc: Higher High
- oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
- // Price: Lower High
- priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
- hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
- f8 = 100 * RSXsrc
- f10 = nz(f8[1])
- v8 = f8 - f10
- f18 = 3 / (length + 2)
- f20 = 1 - f18
- f28 = 0.0
- f28 := f20 * nz(f28[1]) + f18 * v8
- f30 = 0.0
- f30 := f18 * f28 + f20 * nz(f30[1])
- vC = f28 * 1.5 - f30 * 0.5
- f38 = 0.0
- f38 := f20 * nz(f38[1]) + f18 * vC
- f40 = 0.0
- f40 := f18 * f38 + f20 * nz(f40[1])
- v10 = f38 * 1.5 - f40 * 0.5
- f48 = 0.0
- f48 := f20 * nz(f48[1]) + f18 * v10
- f50 = 0.0
- f50 := f18 * f48 + f20 * nz(f50[1])
- v14 = f48 * 1.5 - f50 * 0.5
- f58 = 0.0
- f58 := f20 * nz(f58[1]) + f18 * abs(v8)
- f60 = 0.0
- f60 := f18 * f58 + f20 * nz(f60[1])
- v18 = f58 * 1.5 - f60 * 0.5
- f68 = 0.0
- f68 := f20 * nz(f68[1]) + f18 * v18
- f70 = 0.0
- f70 := f18 * f68 + f20 * nz(f70[1])
- v1C = f68 * 1.5 - f70 * 0.5
- f78 = 0.0
- f78 := f20 * nz(f78[1]) + f18 * v1C
- f80 = 0.0
- f80 := f18 * f78 + f20 * nz(f80[1])
- v20 = f78 * 1.5 - f80 * 0.5
- f88_ = 0.0
- f90_ = 0.0
- f88 = 0.0
- f90_ := nz(f90_[1]) == 0 ? 1 :
- nz(f88[1]) <= nz(f90_[1]) ? nz(f88[1]) + 1 : nz(f90_[1]) + 1
- f88 := nz(f90_[1]) == 0 and length - 1 >= 5 ? length - 1 : 5
- f0 = f88 >= f90_ and f8 != f10 ? 1 : 0
- f90 = f88 == f90_ and f0 == 0 ? 0 : f90_
- v4_ = f88 < f90 and v20 > 0 ? (v14 / v20 + 1) * 50 : 50
- rsx = v4_ > 100 ? 100 : v4_ < 0 ? 0 : v4_
- rsxColor = rsx > obLevel ? #007300 : rsx < osLevel ? #CB0000 : #787b86
- transparent = color.new(color.white, 100)
- obFillColor = rsx > obLevel and highlightBreakouts ? color.green : transparent
- osFillColor = rsx < osLevel and highlightBreakouts ? color.red : transparent
- hb = abs(highestbars(rsx, xbars)) // Finds bar with highest value in last X bars
- lb = abs(lowestbars(rsx, xbars)) // Finds bar with lowest value in last X bars
- max = float(na)
- max_rsi = float(na)
- min = float(na)
- min_rsi = float(na)
- pivoth = bool(na)
- pivotl = bool(na)
- divbear = bool(na)
- divbull = bool(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 ? rsx : na(max_rsi[1]) ? rsx : max_rsi[1]
- min := lb == 0 ? close : na(min[1]) ? close : min[1]
- min_rsi := lb == 0 ? rsx : na(min_rsi[1]) ? rsx : 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
- max
- if rsx > max_rsi // we have a new high
- max_rsi := rsx // change variable "max_rsi" to use current bar's RSI value
- max_rsi
- if close < min // we have a new low
- min := close // change variable "min" to use current bar's low value
- min
- if rsx < min_rsi // we have a new low
- min_rsi := rsx // change variable "min_rsi" to use current bar's RSI value
- min_rsi
- // 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 rsx[1] < max_rsi and rsx <= rsx[1]
- divbear := true
- divbear
- if min[1] < min[2] and rsx[1] > min_rsi and rsx >= rsx[1]
- divbull := true
- divbull
- // Alerts
- alertcondition(divbear, title='Bear div', message='Bear div')
- alertcondition(divbull, title='Bull div', message='Bull div')
- // Plots
- plot(rsx, title="RSX", linewidth=2, color=rsxColor )
- plot(osc, title="RSI", linewidth=1, color=#000000)
- plot(crsi, "CRSI", linewidth=1, color=#b2b5be)
- maxLevelPlot = hline(100, title="Max Level", linestyle=hline.style_dotted, color=transparent)
- obLevelPlot = hline(obLevel, title="Overbought Level", linestyle=hline.style_dotted)
- hline(50, title="Middle Level", linestyle=hline.style_dotted)
- osLevelPlot = hline(osLevel, title="Oversold Level", linestyle=hline.style_dotted)
- minLevelPlot = hline(0, title="Min Level", linestyle=hline.style_dotted, color=transparent)
- fill(obLevelPlot, osLevelPlot, color=color.white, transp=100)
- fill(maxLevelPlot, obLevelPlot, color=obFillColor, transp=100)
- fill(minLevelPlot, osLevelPlot, color=osFillColor, transp=100)
- plot(
- plFound ? osc[lbR] : na,
- offset=-lbR,
- title="Regular Bullish",
- linewidth=2,
- color=(bullCond ? bullColor : noneColor)
- )
- plotshape(
- bullCond ? osc[lbR] : na,
- offset=-lbR,
- title="Regular Bullish Label",
- text=" Bull ",
- style=shape.labelup,
- location=location.absolute,
- color=bullColor,
- textcolor=textColor
- )
- plot(
- plFound ? osc[lbR] : na,
- offset=-lbR,
- title="Hidden Bullish",
- linewidth=2,
- color=(hiddenBullCond ? hiddenBullColor : noneColor)
- )
- plotshape(
- hiddenBullCond ? osc[lbR] : na,
- offset=-lbR,
- title="Hidden Bullish Label",
- text=" H Bull ",
- style=shape.labelup,
- location=location.absolute,
- color=#6f6f6f,
- textcolor=textColor
- )
- plot(
- phFound ? osc[lbR] : na,
- offset=-lbR,
- title="Regular Bearish",
- linewidth=2,
- color=(bearCond ? bearColor : noneColor)
- )
- plotshape(
- bearCond ? osc[lbR] : na,
- offset=-lbR,
- title="Regular Bearish Label",
- text=" Bear ",
- style=shape.labeldown,
- location=location.absolute,
- color=bearColor,
- textcolor=textColor
- )
- plot(
- phFound ? osc[lbR] : na,
- offset=-lbR,
- title="Hidden Bearish",
- linewidth=2,
- color=(hiddenBearCond ? hiddenBearColor : noneColor)
- )
- plotshape(
- hiddenBearCond ? osc[lbR] : na,
- offset=-lbR,
- title="Hidden Bearish Label",
- text=" H Bear ",
- style=shape.labeldown,
- location=location.absolute,
- color=#6f6f6f,
- textcolor=textColor
- )
- // Plots divergences and pivots with offest
- // Longer labels
- plotshape(shrt ? na : divbear ? rsx[1] + 1 : na, location=location.absolute, style=shape.labeldown, color=#BF1D00, size=size.tiny, text="Bear", textcolor=color.white, offset=-1)
- plotshape(shrt ? na : divbull ? rsx[1] - 1 : na, location=location.absolute, style=shape.labelup, color=#008702, size=size.tiny, text="Bull", textcolor=color.white, offset=-1)
- plotshape(piv ? na : shrt ? na : pivoth ? max_rsi + 1 : na, location=location.absolute, style=shape.labeldown, color=#000000, size=size.tiny, text="Pivot", textcolor=color.white, offset=-2)
- plotshape(piv ? na : shrt ? na : pivotl ? min_rsi - 1 : na, location=location.absolute, style=shape.labelup, color=#000000, size=size.tiny, text="Pivot", textcolor=color.white, offset=-2)
- // Shorter labels
- plotshape(shrt ? divbear ? rsx[1] + 3 : na : na, location=location.absolute, style=shape.triangledown, color=color.red, size=size.tiny, offset=-1)
- plotshape(shrt ? divbull ? rsx[1] - 3 : na : na, location=location.absolute, style=shape.triangleup, color=color.green, size=size.tiny, offset=-1)
- plotshape(piv ? na : shrt ? pivoth ? max_rsi + 3 : na : na, location=location.absolute, style=shape.triangledown, color=color.black, size=size.tiny, offset=-2)
- plotshape(piv ? na : shrt ? pivotl ? min_rsi - 3 : na : na, location=location.absolute, style=shape.triangleup, color=color.black, size=size.tiny, offset=-2)
Advertisement
Add Comment
Please, Sign In to add comment