Z-Score Based Momentum Zones with Advanced Volatility Channels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © federalTacos5392b
//@version=5
indicator("Z-Score Based Momentum Zones with Advanced Volatility Channels", overlay = true)
look_back_length = input(5, title=" Volatility Correction Look Back Length")

lookback_period = input(5, title="Relative Price Change Look Back Length")
zscore_period = input(500, title="Z Score Look Back Period")
avgZscore_length=input(14, title="Momentum Block Length")

// Calculate Intraday Volatility 
IDV = ta.sma((high - low) / close, look_back_length) / 2

a_high = (ta.highest(high, 5) + ta.highest(high, 8) + ta.highest(high, 13) + ta.highest(high, 21) + ta.highest(high, 34)) / 5
b_high = (ta.sma(high, 5) + ta.sma(high, 8) + ta.sma(high, 13)) / 3
high_c = (a_high + b_high) / 2

a_low = (ta.lowest(low, 5) + ta.lowest(low, 8) + ta.lowest(low, 13) + ta.lowest(low, 21) + ta.lowest(low, 34)) / 5
b_low = (ta.sma(low, 5) + ta.sma(low, 8) + ta.sma(low, 13)) / 3
low_c = (a_low + b_low) / 2

c_high = high_c * (1 + IDV)
c_low = low_c * (1 - IDV)
basis = (c_high + c_low) / 2

basisIncreasing = basis > basis[1]
basisDecreasing = basis < basis[1]

plot_c_high = plot(c_high, color=color.blue, title="Composite High")
plot_c_low = plot(c_low, color=color.red, title="Composite Low")

// Calculate average z-score value (you need to calculate this value)
// Calculate RPD
sma_hlc3 = ta.sma(hlc3, lookback_period)
psma_hlc3 = ta.valuewhen(true, sma_hlc3, 1)
sma_volume = ta.sma(volume, lookback_period)
psma_volume = ta.valuewhen(true, sma_volume, 1)
std_hlc3 = ta.stdev(hlc3, lookback_period)
rpdev = (hlc3 - psma_hlc3)

// Input for Volume Correction
include_vc = input(false, title="Include Volume Correction")

// Calculate Volume Correction if include_vc is true
vc = include_vc ? volume / psma_volume : 1

// Calculate Volume Corrected RPD
vcrpd = rpdev * vc
// Calculate Mean and Standard Deviation of VCRPD using all available data points
mean_vcrpd = ta.sma(vcrpd, zscore_period)
stddev_vcrpd = ta.stdev(vcrpd, zscore_period)

// Calculate Z-Score for VCRPD using all available data points
zScores = (vcrpd - mean_vcrpd) / stddev_vcrpd
avgZScore=ta.sma(zScores, avgZscore_length)

// Define color scheme based on z-score
var color fillColor = na
if (avgZScore >= 0.25 and avgZScore < 1)
    fillColor := color.new(#b5eeb7, 40)
else if (avgZScore >= 1 and avgZScore < 2)
    fillColor := color.new(#4ad64f, 40)
else if (avgZScore >= 2)
    fillColor := color.new(#328f35, 40)
else if (avgZScore <= -0.25 and avgZScore > -1)
    fillColor := color.new(#e69292, 40)
else if (avgZScore <= -1 and avgZScore > -2)
    fillColor := color.new(#ac4343, 40)
else if (avgZScore <= -2)
    fillColor := color.new(#c03535, 40)
else if (avgZScore >= -0.25 and avgZScore <= 0.25)
    fillColor := color.new(color.yellow, 70)

fill(plot_c_high, plot_c_low, color=fillColor, title="Fill Area")

plot(basis, color=basisIncreasing ? color.green : basisDecreasing ? color.red : color.black, title="mid_line", linewidth=3)
