indicator("OS-R", overlay=false) ////////////////////////////////////inputs//////////////////////////////////////////////// displayRSI = input.string(title="RSI Type", defval="RSI Divergence", options=["RSI Divergence","RSI Smoothed","RSI Momentum"],group="Main Settings") lenrsinordiv = input.int(title="RSI Normal Length", defval=14,minval=1, group= "RSI Normal") lenrsismodiv = input.int(title="RSI Smoothed Length", defval=40, minval=1,group = "RSI Smoothed" ) lenrsissmoma = input.int(title="RSI Smoothed MA", defval=40,minval=1, group = "RSI Smoothed" ) srcrsidiv = input(title="RSI Source", defval=close, group="Main Settings") min_length = input.int(3, minval = 1, title="Fast",group="Momentum") max_length = input.int(5, minval = 1, title="Slow",group="Momentum") ema_smoothing = input.bool(title="Ema Smoothing",defval=true,group="Momentum") ema_length = input.int(title="EMA Length",defval=3,group="Momentum") percent = input.int (title="Percent of change",defval=10, minval = 0, maxval = 100,step=5,group="Momentum") / 100.0 atr1 = input.int(title="atr1",defval=7,group="Momentum") atr2 = input.int(title="atr2",defval=21,group="Momentum") lbR = input.int(title="Pivot Lookback Right", defval=5,minval=1,group="Divergence Spotter") lbL = input.int(title="Pivot Lookback Left", defval=5,minval=1,group="Divergence Spotter") rangeUpper = input.int(title="Max of Lookback Range", defval=60,minval=1,group="Divergence Spotter") rangeLower = input.int(title="Min of Lookback Range", defval=5,minval=1,group="Divergence Spotter") plotBull = input.bool(title="Plot Bullish", defval=true,group="Divergence Spotter") plotHiddenBull = input.bool(title="Plot Hidden Bullish", defval=true,group="Divergence Spotter") plotBear = input.bool(title="Plot Bearish", defval=true,group="Divergence Spotter") plotHiddenBear = input.bool(title="Plot Hidden Bearish", defval=true,group="Divergence Spotter") bearColorrsidiv = color.red bullColorrsidiv = color.green hiddenBullColor = color.new(color.green, 80) hiddenBearColor = color.new(color.red, 80) textColor = color.white noneColor = color.new(color.white, 100) lenDisplay= displayRSI == "RSI Divergence" ? lenrsinordiv: displayRSI == "RSI Smoothed" ? lenrsismodiv: na rsiValue1 = ta.rsi(srcrsidiv, lenrsinordiv) // ### Smoothed MA averageSource = rsiValue1 typeofMA1 = "SMMA" length_ma1 = 50 f_smma(averageSource, averageLength) => smma = 0.0 smma := na(smma[1]) ? ta.sma(averageSource, averageLength) : (smma[1] * (averageLength - 1) + averageSource) / averageLength smma f_smwma(averageSource, averageLength) => smwma = 0.0 smwma := na(smwma[1]) ? ta.wma(averageSource, averageLength) : (smwma[1] * (averageLength - 1) + averageSource) / averageLength smwma f_tma(averageSource, averageLength) => ta.sma(ta.sma(averageSource, averageLength), averageLength) f_dema(averageSource, averageLength) => emaValue = ta.ema(averageSource, averageLength) 2 * emaValue - ta.ema(emaValue, averageLength) f_tema(averageSource, averageLength) => ema1 = ta.ema(averageSource, averageLength) ema2 = ta.ema(ema1, averageLength) ema3 = ta.ema(ema2, averageLength) (3 * ema1) - (3 * ema2) + ema3 f_ma(smoothing, averageSource, averageLength) => switch str.upper(smoothing) "SMA" => ta.sma(averageSource, averageLength) "EMA" => ta.ema(averageSource, averageLength) "WMA" => ta.wma(averageSource, averageLength) "HMA" => ta.hma(averageSource, averageLength) "RMA" => ta.rma(averageSource, averageLength) "SWMA" => ta.swma(averageSource) "ALMA" => ta.alma(averageSource, averageLength, 0.85, 6) "VWMA" => ta.vwma(averageSource, averageLength) "VWAP" => ta.vwap(averageSource) "SMMA" => f_smma(averageSource, averageLength) "SMWMA" => f_smwma(averageSource, averageLength) "DEMA" => f_dema(averageSource, averageLength) "TEMA"=> f_tema(averageSource, averageLength) => runtime.error("Moving average type '" + smoothing + "' not found!"), na MA1 = f_ma(typeofMA1, averageSource, length_ma1) showNormal = displayRSI=="RSI Divergence" showSmoothed = displayRSI=="RSI Smoothed" showMomentum = displayRSI =="RSI Momentum" showAll= displayRSI=="All Three" ///////OB/OS lines hline(showNormal or showSmoothed ? 80 :na, title="OverBought", linestyle=hline.style_dotted, linewidth=2) hline(showNormal or showSmoothed ? 20 :na, title="OverSold", linestyle=hline.style_dotted, linewidth=2) ////////////////show normal plot(showNormal? MA1 : na , linewidth=2, color=color.white) var int colortoken=1 color1= color.green color2 = color.yellow color3 = color.orange color4 = color.red if rsiValue1>rsiValue1[1] and colortoken!=1 colortoken:= colortoken[1] - 1 if rsiValue1 down and up > 0 ? up : 0) minusDM = na(down) ? na : (down > up and down > 0 ? down : 0) trur = ta.rma(ta.tr, len) plus = fixnan(100 * ta.rma(plusDM, len) / trur) minus = fixnan(100 * ta.rma(minusDM, len) / trur) sum = plus + minus adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig) rsisrc = ta.rsi(close,lenrsismodiv) adxthreshold=input.int(title="adx",defval=15) smoothColor= adx>adxthreshold and plus>minus? color.green:adx>adxthreshold and plus ta.atr(atr2) if high_Volatility dynamicLen := math.max(min_length, dynamicLen * (1 - percent)) else dynamicLen := math.min(max_length, dynamicLen * (1 + percent)) rsi1=ta.rsi(close,14) mom= ta.mom(src,int(dynamicLen)) value = ema_smoothing ? f_ma(typeofMA, mom, ema_length) : rsi1 //######################## Plotting ########################## WTF=value smmaLen = input.int(34, minval=1, title="SMMA Length", group = "RSI Momentum") smmaLen1= 2 smmaSrc = value WTFsmma = 0.0 WTFsmma := na(WTFsmma[1]) ? ta.sma(smmaSrc, smmaLen1) : (WTFsmma[1] * (smmaLen1 - 1) + smmaSrc) / smmaLen1 smma = 0.0 smma := na(smma[1]) ? ta.sma(smmaSrc, smmaLen) : (smma[1] * (smmaLen - 1) + smmaSrc) / smmaLen color1a= #0E3F01 color2a = #31FA2A color3a = #FA6B6B color4a = #971643 momentumColor= WTF>WTF[1] and WTF>smma ? color1a : WTFsmma ? color2a : WTF>WTF[1] and WTF bars = ta.barssince(cond == true) rangeLower <= bars and bars <= rangeUpper //------------------------------------------------------------------------------ // Regular Bullish // Osc: Higher Low oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1]) // Price: Lower Low priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1) bullCond = plotBull and priceLL and oscHL and plFound plot( displayRSI !="RSI Momentum"and plFound ? osc[lbR] : na, offset=-lbR, title="Regular Bullish", linewidth=2, color=(bullCond ? bullColorrsidiv : noneColor) ) plotshape( displayRSI !="RSI Momentum" and bullCond ? osc[lbR] : na, offset=-lbR, title="Regular Bullish Label", text=" Bull ", style=shape.labelup, location=location.absolute, color=bullColorrsidiv, textcolor=textColor ) //------------------------------------------------------------------------------ // Hidden Bullish // Osc: Lower Low oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1]) // Price: Higher Low priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1) hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound plot( displayRSI !="RSI Momentum" and plFound ? osc[lbR] : na, offset=-lbR, title="Hidden Bullish", linewidth=2, color=(hiddenBullCond ? hiddenBullColor : noneColor) ) plotshape( displayRSI !="RSI Momentum" and hiddenBullCond ? osc[lbR] : na, offset=-lbR, title="Hidden Bullish Label", text=" H Bull ", style=shape.labelup, location=location.absolute, color=bullColorrsidiv, textcolor=textColor ) //------------------------------------------------------------------------------ // Regular Bearish // Osc: Lower High oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1]) // Price: Higher High priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1) bearCond = plotBear and priceHH and oscLH and phFound plot( displayRSI !="RSI Momentum" and phFound ? osc[lbR] : na, offset=-lbR, title="Regular Bearish", linewidth=2, color=(bearCond ? bearColorrsidiv : noneColor) ) plotshape( displayRSI !="RSI Momentum" and bearCond ? osc[lbR] : na, offset=-lbR, title="Regular Bearish Label", text=" Bear ", style=shape.labeldown, location=location.absolute, color=bearColorrsidiv, textcolor=textColor ) //------------------------------------------------------------------------------ // Hidden Bearish // Osc: Higher High oscHH = osc[lbR] > ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1]) // Price: Lower High priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1) hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound plot( displayRSI !="RSI Momentum" and phFound ? osc[lbR] : na, offset=-lbR, title="Hidden Bearish", linewidth=2, color=(hiddenBearCond ? hiddenBearColor : noneColor) ) plotshape( displayRSI !="RSI Momentum" and hiddenBearCond ? osc[lbR] : na, offset=-lbR, title="Hidden Bearish Label", text=" H Bear ", style=shape.labeldown, location=location.absolute, color=bearColorrsidiv, textcolor=textColor ) plot_bull = rsiValue1>MA1 and rsisrc>rsiwsmma and WTF>smma plot_bear = rsiValue1