JZ_Chaikin HTF Volatility Breakout
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JazzByrd

// All Credit to Harry Potter's initial Chaikin Volatility Indicator (Copyright by HPotter v1.0 13/08/2014), of which this is only a slight modification.
// Added Higher Timeframe and HMA for smoother signal and for use as confirmation of the faster signal.
// Added a Volatility Breakout Signal based on HTF signals.
// Breakouts try to capture zero crosses from a swing low to high in volatility.
// Added a Range Highlight options to identify range periods and potential no trade zones. Defval is off.
// The HTF Chaikin breakout levels can both be changed in inputs to set your own parameters for the breakout signals.

//@version=5

indicator(title='JZ_Chaikin HTF Volatility Breakout', shorttitle = "CVB")

// INPUTS:

chk_inp         = "Chaikin Volatility Inputs:"
chk_Len         = input.int(10, minval=1, title = "Chaikin Volatility Length", group = chk_inp)
ROCLength       = input.int(12, minval=1, title = "Rate of Change Length", group = chk_inp)
chk_Br_lvl_h    = input.int(defval=15, title = "Breakout Range - High", group = chk_inp, tooltip = "Sets higher threshold for breakout. When HTF Chaikin line above this level will not give Breakout signal")
chk_Br_lvl_l    = input.int(defval = -10, title = "Breakout Range - Low", group = chk_inp, tooltip = "Sets lower threshold for breakout. When HTF Chaikin below this level will not give breakout signal")
chk_hma_len     = input.int(defval = 21, title = "HMA Length", tooltip = "Sets HMA smoothing line length for both Chart and HTF", group = chk_inp)

chk_htf_inp = "Chaikin Vol HTF Options:"
cv_Htf = input.timeframe(title = "Higher Timeframe:", defval = "D", group = chk_htf_inp)

chk_hl      = "Chaikin Highlight Conditions:"
cv_range    = input.bool(defval = false, title = "Highlight HTF Range/Chop Zones?", tooltip = "Chaikin HTF Line is < 0 and it's HMA is decreasing", group = chk_hl)
col_ch      = input.bool(defval = true, title = "Color Change on HMA?", tooltip = "Chaikin Volatility HMA line changes color with direction changes", group = chk_hl)
chk_bo      = input.bool(defval=true, title = "Highlight Volatility Breakouts?", group = chk_hl, tooltip = "HTF line rising up close to zero, while Htf HMA also increasing from below zero")

// CALCULATIONS:

// Chaikin Volatility:

xPrice1 = high
xPrice2 = low
xPrice = xPrice1 - xPrice2
cv1 = ta.roc(ta.ema(xPrice, chk_Len), ROCLength)

// HMA/MA on Chaikin Vol:

cv1_Hma = ta.hma(cv1, chk_hma_len)


// Higher Timeframe:

// DECLARE CUSTOM SECURITY FUNCTION:

f_sec(_market, _res, _exp) => request.security(_market, _res, _exp[barstate.isconfirmed ? 0 : 1])

// GET Chaikin Vol & HMA on Higher Timeframes:

cv2_Htf = f_sec(syminfo.tickerid, cv_Htf, cv1)
cv2_Hma = f_sec(syminfo.tickerid, cv_Htf, cv1_Hma)

// CHAIKIN COLOR CHANGE:

ch1_col = cv1_Hma > cv1_Hma[1] ? color.new(color.green, 75) : cv1_Hma < cv1_Hma[1] ? color.new(color.red, 50) : color.new(color.yellow, 50)
ch2_col = cv2_Hma > cv2_Hma[1] ? color.new(color.aqua, 75) : cv2_Hma < cv2_Hma[1] ? color.new(color.orange, 50) : color.new(color.white, 50)


// HIGHLIGHT CONDITIONS:

// HTF Chaikin Line is coming up from low (below zero) and getting close to 0 line range (between breakout levels of input).
// Confirmation is HMA changing to positive momentum (increasing) and coming from a swing low (< -15).
// basically capturing the breakout moment just before or after zero line cross --  a swing low to high in volatility.

ch_htf_BO = (cv2_Htf < chk_Br_lvl_h and cv2_Htf > chk_Br_lvl_l) and (cv2_Hma < 0 and cv2_Hma > cv2_Hma[1] and cv2_Hma < -15) and chk_bo and (cv2_Htf > cv2_Htf[1]) 
plotshape(ch_htf_BO, title = "Volatility Breakout", style = shape.diamond, color = color.aqua, text = "B", location = location.bottom, textcolor = color.white)

// RANGE:

// NO TRADE ZONE:

NoTrade = (cv2_Hma < 0 and cv2_Hma < cv2_Hma[1]) and cv2_Htf < 0
bgcolor(color = cv_range and NoTrade ? color.new(color.white, 50) : na, title = "HTF No Trade / Low Volatility Zone")


//PLOT:
plot(cv1, color=color.new(color.white, 0), title='Chaikin Volatility')
plot(cv1_Hma, title="HMA of C.V.", color = col_ch ? ch1_col : color.new(color.white, 50), style=plot.style_area)


// Plot HTF:

plot(cv2_Htf, color=color.blue, title = "HTF C.V.", linewidth = 2)
plot(cv2_Hma, color = col_ch ? ch2_col : color.new(color.orange, 50), title = "HTF HMA", style = plot.style_histogram, linewidth = 3)

hline(0, color=color.white, linestyle=hline.style_dotted)
