TSI Market Timer + Volatility Meter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SpreadEagle71

//@version=5
indicator("TSI Market Timer + Volatility Meter", shorttitle="TSI+VM", overlay=false)

fast = input(5, title="TSI Fast")
slow = input(15, title="TSI slow")
//length = input.int(20, title="TTM SQZ length")
compare = input.string("SPY", title="Comparison Index/ETF/Stock")

v1 = close
v3 = high
v4 = low


v2 = request.security(compare, timeframe.period, close)
v14 = request.security('DXY', timeframe.period, close)


dxy = ta.tsi(v14, fast, slow)
spy = ta.tsi(v2, fast, slow)
xxx = ta.tsi(v1, fast, slow)
xxh = ta.tsi(v3, fast, slow)
xxl = ta.tsi(v4, fast, slow)

lookback = input(252, title="Lookback (252 approx 52 week high)")
dxyfill = input(false, title="Fill lines between Stock and Trigger line, green up, red down?")
spyfill = input(false, title="Fill lines between index and stock, green=up,red=down?")
showx = input(true, title="Highlight stock/trigger crossover upqward?")
showy = input(true, title="Highlight stock/trigger crossover downward?")
up = xxx > 0
down = xxx < 0
newh = ta.highest(high, lookback)
newhigh = high >= newh
newl = ta.lowest(low, lookback)
newlow = low<= newl

mycolor = newhigh ? color.new(color.lime, 5) : newlow ? color.new(#d9dc24, 5) : up ? color.new(#118e15, 1) : down ? color.new(color.red, 0.95) : color.new(color.blue, 0.95)
xplot=plot(xxx, color=mycolor, linewidth=2,transp=0)
xxplot=plot(xxx,color= newhigh ? mycolor : na,linewidth=3)


uplot= plot(dxy, color=color.new(color.rgb(243, 174, 14),transp=60), title='Trigger', linewidth=3)

sp = plot(spy, color=color.new(color.purple, transp=10), title="INDEX", linewidth=2)


transparency = dxyfill ? 80 : 100
fill(xplot, uplot, color = xxx > dxy ? color.new(color.green, transparency) : color.new(color.red, transparency))


spy_transparency = spyfill ? 80 :100
fill(xplot, sp, color = xxx > spy ? color.new(color.green, spy_transparency) : color.new(color.red, spy_transparency))

xover = ta.crossover(xxx, dxy)
bgcolor(xover? showx ? color.rgb(8, 77, 10) : na : na, transp=75)

xdown = ta.crossover(dxy,xxx)
bgcolor(xdown? showy ? color.rgb(121, 40, 40) : na : na, transp=65)

// Volatility Colors
vlength = input.int(10, minval=1)

annual = 365
per = timeframe.isintraday or timeframe.isdaily and timeframe.multiplier == 1 ? 1 : 7
hv = 100 * ta.stdev(math.log(close / close[1]), vlength) * math.sqrt(annual / per)
cv= hv/ta.sma(close,vlength)
ave = ta.sma(cv,30)
low30=ta.lowest(cv,30)
low100=ta.lowest(cv,100)
high30=ta.highest(cv,30)
high100=ta.highest(cv,100)

mid_color= cv ==low100 ? color.blue :cv==low30? color.aqua : cv==high30 ? color.red : #ea9a04 
plot(0, color=mid_color, style=plot.style_circles, linewidth=3)
// Checking if the condition xxx > spy > dxy is met
condition = xxx > dxy and xxx > spy and dxy < 0
plotshape(condition,"Stacked Condition", shape.triangleup, location.bottom, color=color.white, transp=0)

hline(0.5)
hline(-0.5)