Combo Z Score
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jroche1973



//@version=4
study("Combo Z Score", precision=6, overlay=false)
Length      = input(252) //252
v_StdDevs   = input(defval = 2.0, title="VIX Z Std Dev")
m_StdDevs   = input(defval = 2.0, title="MOVE Z Std Dev")
show_m      = input(type=input.bool, title="Show MOVE", defval=true)
show_v      = input(type=input.bool, title="Show VIX", defval=true)
//show_b      = input(type=input.bool, title="Apply %B", defval=true)

_bb_mult    = input(title="BB mult", type=input.float, defval=2)
_bb_range   = input(title="BB mean range", type=input.integer, defval=20)

//src = input(close)
MOVE = security("TVC:MOVE", timeframe.period, close)
VIX = security("VIX", timeframe.period, close)
VVIX = security("VVIX", timeframe.period, close)

[_bb_mid, _bb_hig, _bb_low] = bb(close, _bb_range, _bb_mult)

_bb_percent = (close - _bb_low) / (_bb_hig - _bb_low)

//Proxy of M of Move ie alt VVIX
iVIX = 1/VIX
//MOVE calcs
divM = MOVE/iVIX
m_basis = sma(divM, Length)

//zscore = (src - basis) / stdev(src, Length)

m_zscore = (divM - m_basis) / stdev(divM, Length)

plot(show_m? m_zscore: na, color=color.new(#55FF55, 0))

//plot( _bb_percent , color = color.new(color.yellow,0))

hline(m_StdDevs, color=color.white, linestyle=hline.style_dotted)
hline(-1 * m_StdDevs, color=color.white, linestyle=hline.style_dotted)


//m_color = color.new(color.black, 0)

//if show_b
//    color m_color = m_zscore < 0 and _bb_percent > 0.5 ? color.new(color.white, 70): color.new(color.blue, 70) //we want white if not active
//else
//    color m_color = m_zscore < 0 ? color.new(color.white, 70): color.new(color.blue, 70) //we want white if not active

color m_color = m_zscore < 0 ? color.new(color.white, 70): color.new(color.blue, 70) //we want white if not active

color mb_color = m_zscore < 0 and _bb_percent > 0.5 ? color.new(color.white, 70): color.new(color.blue, 70) //we want white if not active

//color apply_b = show_b ? mb_color : m_color

//is_b =  _bb_percent < 0.5? true: false
use_b = _bb_percent < 0.5 ? true: false


//bgcolor(show_m ? color.new(m_color,70) : color.new(color.white, 70)) // we want whatever color our status stipulates if 'on' or white if 'off'
//bgcolor(show_m ? color.new(apply_b,70) : color.new(color.white, 70)) // we want whatever color our status stipulates if 'on' or white if 'off'
bgcolor(show_m and use_b ? color.new(m_color,70) : color.new(color.white, 70)) // we want whatever color our status stipulates if 'on' or white if 'off'

//bgcolor(m_zscore < 0 ? color.new(color.white,70) : color.new(color.blue, 70)) // we want whatever color our status stipulates if 'on' or white if 'off'

//VIX Calcs
divV = VIX/VVIX
v_basis = sma(divV, Length)

//zscore = (src - basis) / stdev(src, Length)

v_zscore = (divV - v_basis) / stdev(divV, Length)

plot(show_v? v_zscore: na, color=color.new(#7d070c, 0))

hline(v_StdDevs, color=color.white, linestyle=hline.style_dotted)
hline(-1 * v_StdDevs, color=color.white, linestyle=hline.style_dotted)

color v_color = v_zscore < 0 ? color.new(color.white, 70): color.new(color.red, 70) //we want white if not active
bgcolor(show_v and use_b? color.new(v_color,70) : color.new(color.white, 70)) // we want whatever color our status stipulates if 'on' or white if 'off'

