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

//@version=5
indicator("Improved Z-Score Overlay", overlay=true)

z_score_type            = input.string(defval='Close', title='Ticker Type', options=['Open', 'High', 'Low', 'Close', 'OHLC4'])
z_score_smooth          = input.bool(defval=false, title='Ticker Smoothing?', tooltip='Whether or not you would like the ticker raw data to be smoothened or not by a particular period. Sometimes 3 or 5 period smoothing helps filter noise.')
z_score_smooth_period   = input.int(defval=3, title='Ticker Smoothing Period', tooltip='This is the smoothing period for the ticker if "Ticker Smoothing?" is set to True. I recommend using 3 or 5 period smoothing to filter noise.')
z_score_ma_type         = input.string(defval='SMA', title='Z-Score Type', options=['SMA', 'EMA'])
z_score_ma_period       = input.int(defval=21, title='Moving Average Period', tooltip='The length (in bars) for normalizing with a moving average. Standard is 21 (one month of trading days) but is customizable.')
z_score_stdev_period    = input.int(defval=252, title='Standard Deviation Period', tooltip='The length of time (in bars) for calculating standard deviation. Standard is 252 for use on Daily chart (1 trading year)')

ticker_raw_data         = z_score_type == 'Close' ? close : z_score_type == 'Open' ? open : z_score_type == 'High' ? high : z_score_type == 'Low' ? low : z_score_type == 'OHLC4' ? ohlc4 : na
ticker_smooth           = z_score_smooth ? ta.ema(ticker_raw_data, z_score_smooth_period) : ticker_raw_data
ticker_ma               = z_score_ma_type == 'SMA' ? ta.sma(ticker_raw_data, z_score_ma_period) : z_score_ma_type == 'EMA' ? ta.ema(ticker_raw_data, z_score_ma_period) : na
ticker_stdev            = ta.stdev(ticker_raw_data, z_score_stdev_period)

final_zscore            = (ticker_smooth - ticker_ma) / ticker_stdev

ticker_zscore_p3        = ticker_ma + 3*ticker_stdev
ticker_zscore_p2        = ticker_ma + 2*ticker_stdev
ticker_zscore_p1        = ticker_ma + 1*ticker_stdev
ticker_zscore_n3        = ticker_ma - 3*ticker_stdev
ticker_zscore_n2        = ticker_ma - 2*ticker_stdev
ticker_zscore_n1        = ticker_ma - 1*ticker_stdev
ticker_zscore_0         = ticker_ma

plot(final_zscore, title='Z-Score', color=color.new(color.red, 30), style=plot.style_area, display=display.none)
plot(ticker_zscore_p3, color=color.new(color.red, 50), linewidth=3, title='+3S')
plot(ticker_zscore_p2, color=color.new(color.red, 50), linewidth=2, title='+2S')
plot(ticker_zscore_p1, color=color.new(color.red, 50), linewidth=1, title='+1S')
plot(ticker_zscore_n3, color=color.new(color.green, 50), linewidth=3, title='-3S')
plot(ticker_zscore_n2, color=color.new(color.green, 50), linewidth=2, title='-2S')
plot(ticker_zscore_n1, color=color.new(color.green, 50), linewidth=1, title='-1S')
plot(ticker_zscore_0, color=color.new(color.black, 50), linewidth=1, title='CL')