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/
- // © TeamTaurus
- //@version=4
- study(title="TTDivergence", format=format.price)
- OsC = input(title="Select OsC",defval="RSI",options=["RSI", "MACD", "Stochastic","Money Flow","Demand Index","Chaikin Money flow"])
- //Tradingview built-in divergence indicator script used to show Divergence
- //Thanks to Tradingview for providing this unique Built-in Divergence Indicator
- PlR = input(title="Pivot Lookback Right", defval=2)
- PlL = input(title="Pivot Lookback Left", defval=6)
- rUpper = input(title="Max of Lookback Range", defval=60)
- rLower = input(title="Min of Lookback Range", defval=5)
- PBull = input(title="Plot Bullish", defval=true)
- PHBull = input(title="Plot Hidden Bullish", defval=false)
- plotBear = input(title="Plot Bearish", defval=true)
- PHBear = input(title="Plot Hidden Bearish", defval=false)
- bearColor = color.red
- bullColor = color.green
- hiddenBullColor = color.new(color.green, 80)
- hiddenBearColor = color.new(color.red, 80)
- textColor = color.white
- noneColor = color.new(color.white, 100)
- [macdline,_,_]=macd(close,12,26,9)
- P = (close - open) / open
- DI = 0.0
- Spread = 0.0
- H = 0.0
- L = 0.0
- if (high > high[1])
- H := high
- else
- H := high[1]
- if (low < low[1])
- L := low
- else
- L := low[1]
- Spread := H - L
- Spread := avg(Spread, 10)
- if (Spread == 0)
- Spread := 1
- K = (3*close) / Spread
- P := P * K
- BP = 1.0
- SP = 1.0
- if (close > open)
- BP := volume
- SP := volume / P
- else
- BP := volume / P
- SP := volume
- if (abs(BP) > abs(SP))
- DI := 100*SP/BP
- else
- DI := 100*BP/SP
- DI := ema(DI, 10)
- ad = close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume
- cmf = (sum(ad, 20) / sum(volume, 20))*100
- osc = OsC=="RSI" ? rsi(close,14) : OsC=="MACD" ? macdline : OsC=="Stochastic" ?
- sma(stoch(close, high, low, 14), 3) : OsC=="Money Flow" ? mfi(hlc3,14) : OsC=="Demand Index" ? DI :
- OsC=="Chaikin Money flow" ? cmf : na
- plot(osc, title="RSI", linewidth=2, color=#8D1699)
- hline(50, title="Middle Line", linestyle=hline.style_dotted)
- obLevel = hline(60, title="Overbought", linestyle=hline.style_dotted)
- osLevel = hline(40, title="Oversold", linestyle=hline.style_dotted)
- fill(obLevel, osLevel, title="Background", color=#9915FF, transp=90)
- plFound = na(pivotlow(osc, PlL, PlR)) ? false : true
- phFound = na(pivothigh(osc, PlL, PlR)) ? false : true
- _inRange(cond) =>
- bars = barssince(cond == true)
- rLower <= bars and bars <= rUpper
- oscHL = osc[PlR] > valuewhen(plFound, osc[PlR], 1) and _inRange(plFound[1])
- // Price: Lower Low
- priceLL = low[PlR] < valuewhen(plFound, low[PlR], 1)
- bullCond = PBull and priceLL and oscHL and plFound
- plot(
- plFound ? osc[PlR] : na,
- offset=-PlR,
- title="Regular Bullish",
- linewidth=2,
- color=(bullCond ? bullColor : noneColor),
- transp=0
- )
- plotshape(
- bullCond ? osc[PlR] : na,
- offset=-PlR,
- title="Regular Bullish Label",
- text=" Bull ",
- style=shape.labelup,
- location=location.absolute,
- color=bullColor,
- textcolor=textColor,
- transp=0
- )
- oscLL = osc[PlR] < valuewhen(plFound, osc[PlR], 1) and _inRange(plFound[1])
- priceHL = low[PlR] > valuewhen(plFound, low[PlR], 1)
- hiddenBullCond = PHBull and priceHL and oscLL and plFound
- plot(
- plFound ? osc[PlR] : na,
- offset=-PlR,
- title="Hidden Bullish",
- linewidth=2,
- color=(hiddenBullCond ? hiddenBullColor : noneColor),
- transp=0
- )
- plotshape(
- hiddenBullCond ? osc[PlR] : na,
- offset=-PlR,
- title="Hidden Bullish Label",
- text=" H Bull ",
- style=shape.labelup,
- location=location.absolute,
- color=bullColor,
- textcolor=textColor,
- transp=0
- )
- oscLH = osc[PlR] < valuewhen(phFound, osc[PlR], 1) and _inRange(phFound[1])
- priceHH = high[PlR] > valuewhen(phFound, high[PlR], 1)
- bearCond = plotBear and priceHH and oscLH and phFound
- plot(
- phFound ? osc[PlR] : na,
- offset=-PlR,
- title="Regular Bearish",
- linewidth=2,
- color=(bearCond ? bearColor : noneColor),
- transp=0
- )
- plotshape(
- bearCond ? osc[PlR] : na,
- offset=-PlR,
- title="Regular Bearish Label",
- text=" Bear ",
- style=shape.labeldown,
- location=location.absolute,
- color=bearColor,
- textcolor=textColor,
- transp=0
- )
- oscHH = osc[PlR] > valuewhen(phFound, osc[PlR], 1) and _inRange(phFound[1])
- priceLH = high[PlR] < valuewhen(phFound, high[PlR], 1)
- hiddenBearCond = PHBear and priceLH and oscHH and phFound
- plot(
- phFound ? osc[PlR] : na,
- offset=-PlR,
- title="Hidden Bearish",
- linewidth=2,
- color=(hiddenBearCond ? hiddenBearColor : noneColor),
- transp=0
- )
- plotshape(
- hiddenBearCond ? osc[PlR] : na,
- offset=-PlR,
- title="Hidden Bearish Label",
- text=" H Bear ",
- style=shape.labeldown,
- location=location.absolute,
- color=bearColor,
- textcolor=textColor,
- transp=0
- )
- alertcondition(condition = (bullCond or hiddenBullCond), title = "Bullish Divergence", message = "Bullish Divergence")
- alertcondition(condition = (bearCond or hiddenBearCond), title = "Bearish Divergence", message = "Bearish Divergence")
Advertisement
Add Comment
Please, Sign In to add comment