VolatilityDivergenceRedGreen by STTA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Trading-Software-Consulting

// Name: VolaDivergenceRedGreen by STTA
// - Undelying and implied volatiliy normally show negative correlated behavior (price rises, vola falls and vice versa)
// - This study shows symbols in on candles in chart where Undelying and corresponding vola index show same bahvior for 1,2 or 3 consecutive bars. (price rises and vola rises and vice versa)
// - This situation is called Vola Divergence. Red when prices and vola fall; green when price and vola rise
// - This information can be used to detect end of up / Down swings.
// - User can configure if rising or falling or both price movements shall be displayed.
// - This study can be used with root symbols, which provide corresponding volatility indices.
// - in all other symbols, no symbols are displayed.
// 
// Inputs
// - underlying displayed in chart
// 
// Settings/Parameter
// - each Divergence can be switched off/on separately
// - each displayed symbol can be configured
// 
// Outputs
// - RedDiv1: first bar with rising price and rising volatility index
// - GreenDiv1: first bar with falling price and falling volatility index
// - RedDiv2: second bar in a row with rising price and rising volatility index
// - GreenDiv2: second bar with falling price and falling volatility index
// - RedDiv3: third bar in a row with rising price and rising volatility index
// - GreenDiv3: third bar in a row with falling price and falling volatility index


//@version=5
indicator(title="VolaDivergenceRedGreen by STTA", shorttitle="VDRG", precision=0, overlay=true)

// load symbol
cSymbol = close

// load Vola Index
iVolaIndex = if syminfo.root == "SPX" or syminfo.root == "SPY" or syminfo.root == "ES" or syminfo.root == "MES"
    iVolaIndex = "VIX"
else if syminfo.root == "NDQ" or syminfo.root == "QQQ"  or syminfo.root == "NQ"
    iVolaIndex = "VXN"
else if syminfo.root == "DJI" or syminfo.root == "DJIA"  or syminfo.root == "DIA" or syminfo.root == "US30"
    iVolaIndex = "VXD"
else if syminfo.root == "RUT" or syminfo.root == "RTY"  or syminfo.root == "IWM" or syminfo.root == "US2000"
    iVolaIndex = "RVX"
else if syminfo.root == "CL" or syminfo.root == "OIL"
    iVolaIndex = "OVX"
else if syminfo.ticker == "XLE"
    iVolaIndex = "VXXLE"
else if syminfo.root == "GC" or syminfo.root == "GOLD" or syminfo.root == "XAU"
    iVolaIndex = "GVZ"
else if syminfo.root == "SI" or syminfo.root == "SILVER" or syminfo.root == "XAG" or syminfo.root == "SLV"
    iVolaIndex = "VXSLV"
else if syminfo.root == "EUR"
    iVolaIndex = "EVZ"
else if syminfo.root == "HSI"
    iVolaIndex = "VHSI"
else if syminfo.ticker == "FXI"
    iVolaIndex = "VXFXI"
else if syminfo.ticker== "EWZ"
    iVolaIndex = "VXEWZ"
else if syminfo.ticker == "AMZN"
    iVolaIndex = "VXAZN"
else if syminfo.ticker == "AAPL"
    iVolaIndex = "VXAPL"
else if syminfo.ticker == "GS"
    iVolaIndex = "VXGS"
else if syminfo.root == "GOOG"
    iVolaIndex = "VXGOG"
else if syminfo.root == "IBM"
    iVolaIndex = "VXIBM"
else if syminfo.root == "DEU40" or syminfo.root == "DAX"  or syminfo.root == "DE40"
    iVolaIndex = "DV1X"
else
    iVolaIndex = ""

cVolaIndex = not(iVolaIndex == "") ? request.security(iVolaIndex, '', close) : na

// Calculate Conditions
//Symbol
bSymbolDown = cSymbol[0] < cSymbol[1] ? 1 : 0
bSymbolUp = cSymbol[0] > cSymbol[1] ? 1 : 0

bSymbolDown2 = cSymbol[0] < cSymbol[1] and cSymbol[1] < cSymbol[2] ? 1 : 0
bSymbolUp2 = cSymbol[0] > cSymbol[1] and cSymbol[1]> cSymbol[2] ? 1 : 0

bSymbolDown3 = cSymbol[0] < cSymbol[1] and cSymbol[1] < cSymbol[2] and cSymbol[2] < cSymbol[3] ? 1 : 0
bSymbolUp3 = cSymbol[0] > cSymbol[1] and cSymbol[1]> cSymbol[2] and cSymbol[2]> cSymbol[3] ? 1 : 0

//Vola
bVolaDown = cVolaIndex[0] < cVolaIndex[1] ? 1 : 0
bVolaUp = cVolaIndex[0] > cVolaIndex[1] ? 1 : 0

bVolaDown2 = cVolaIndex[0] < cVolaIndex[1] and cVolaIndex[1] < cVolaIndex[2] ? 1 : 0
bVolaUp2 = cVolaIndex[0] > cVolaIndex[1] and cVolaIndex[1] > cVolaIndex[2] ? 1 : 0

bVolaDown3 = cVolaIndex[0] < cVolaIndex[1] and cVolaIndex[1] < cVolaIndex[2] and cVolaIndex[2] < cVolaIndex[3] ? 1 : 0
bVolaUp3 = cVolaIndex[0] > cVolaIndex[1] and cVolaIndex[1] > cVolaIndex[2] and cVolaIndex[2] > cVolaIndex[3] ? 1 : 0

// calculate Divergencies
bRedDiv = bSymbolDown and bVolaDown ? 1 : 0
bGreenDiv = bSymbolUp and bVolaUp ? 1 : 0

bRedDiv2 = bSymbolDown2 and bVolaDown2 ? 1 : 0
bGreenDiv2 = bSymbolUp2 and bVolaUp2 ? 1 : 0

bRedDiv3 = bSymbolDown3 and bVolaDown3 ? 1 : 0
bGreenDiv3 = bSymbolUp3 and bVolaUp3 ? 1 : 0


plotshape(bRedDiv and not bRedDiv2 and not bRedDiv3 ? bar_index : na, style=shape.triangleup, location=location.belowbar, color= color.red, text='1', size=size.tiny, title='RedDiv1')
plotshape(bGreenDiv and not bGreenDiv2 and not bGreenDiv3 ? bar_index : na, style=shape.triangledown, location=location.abovebar, color= color.green, text='1', size=size.tiny, title='GreenDiv1')

plotshape(bRedDiv2 and not bRedDiv3 ? bar_index : na, style=shape.triangleup, location=location.belowbar, color= color.red, text='2', size=size.small, title='RedDiv2')
plotshape(bGreenDiv2 and not bGreenDiv3 ? bar_index : na, style=shape.triangledown, location=location.abovebar, color= color.green, text='2', size=size.small, title='GreenDiv2')

plotshape(bRedDiv3 ? bar_index : na, style=shape.triangleup, location=location.belowbar, color= color.red, text='3', size=size.normal, title='RedDiv3')
plotshape(bGreenDiv3 ? bar_index : na, style=shape.triangledown, location=location.abovebar, color= color.green, text='3', size=size.normal, title='GreenDiv3')
