Volatility Adjusted Composite RSI with SMA and EMA Signals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jagandeep

//@version=5
indicator("VAC - RSI with SMA and EMA Signals", overlay=false)

// User inputs
priceWeight = input.float(65, title="Price RSI Weightage (%)", minval=0, maxval=100)
timeWeight = 100 - priceWeight
RSI_T_RSI_Period = input.int(14, title="Unified Period for RSI & T-RSI", minval=1)
smaPeriod = input.int(13, title="C-RSI SMA Period", minval=1)
emaPeriod = input.int(33, title="C-RSI EMA Period", minval=1)
bull_sup = input.int(35, title="C-RSI Bull Trend Support", minval=0, maxval=100)
bear_res = input.int(65, title="C-RSI Bear Trend Resistance", minval=0, maxval=100)
useStdDev = input.bool(true, title="Use Volatility Adjusted C-RSI (VAC-RSI)?")
stdDevPeriod = input.int(14, title="Standard Deviation Period", minval=1)
alpha = input.float(5, title="Volatility Scaling Factor (α)", minval=0.1)

// RSI Calculation
Delta = close - close[1]
Gain = Delta > 0 ? Delta : 0
Loss = Delta < 0 ? -Delta : 0
AvgGain = ta.sma(Gain, RSI_T_RSI_Period)
AvgLoss = ta.sma(Loss, RSI_T_RSI_Period)
RS = AvgGain / AvgLoss
TraditionalRSI = 100 - (100 / (1 + RS))

// Time-based RSI (T-RSI) using unified period
TimeScore = math.sum(close > close[1] ? 1 : close < close[1] ? -1 : 0, RSI_T_RSI_Period)
TRSI = (TimeScore + RSI_T_RSI_Period) * (100 / (2 * RSI_T_RSI_Period))

// Combine Traditional RSI and T-RSI
C_RSI = (TraditionalRSI * priceWeight/100) + (TRSI * timeWeight/100)

// Volatility Adjustment using Z-Score
meanClose = ta.sma(close, stdDevPeriod)
stdDevValue = ta.stdev(close, stdDevPeriod)
zScore = (close - meanClose) / stdDevValue
VAC_RSI = useStdDev ? C_RSI + alpha * zScore : C_RSI

// SMA & EMA for VAC-RSI
smaVAC_RSI = ta.sma(VAC_RSI, smaPeriod)
emaVAC_RSI = ta.ema(VAC_RSI, emaPeriod)

// Plotting
prsi = plot(VAC_RSI, title='VAC-RSI', color=color.new(color.purple, 0), style=plot.style_line, linewidth=2)
overbought = hline(80, title="Overbought", color=color.new(color.purple, 20), linestyle=hline.style_dashed, linewidth=1)
oversold = hline(20, title="Oversold", color=color.new(color.purple, 20), linestyle=hline.style_dashed, linewidth=1)
fill(overbought, oversold, color=color.new(color.purple, 80), title="VAC-RSI Fill")

psma = plot(smaVAC_RSI, title='SMA', color=color.new(color.gray, 35), style=plot.style_line, linewidth=2)
pema = plot(emaVAC_RSI, title="EMA", color=color.new(color.gray, 35), style=plot.style_line, linewidth=2)

fillColor = emaVAC_RSI < smaVAC_RSI ? color.new(color.green, 65) : color.new(color.red, 65)
fill(psma, pema, color=fillColor, title="EMA vs SMA Fill")
fill(psma, prsi, color=color.new(color.gray, 80), title="VAC-RSI vs SMA Fill")

hline(bull_sup, title="Bull Market VAC-RSI Support", color=color.new(color.green, 35), linestyle=hline.style_solid, linewidth=3)
hline(bear_res, title="Bear Market VAC-RSI Resistance", color=color.new(color.red, 65), linestyle=hline.style_solid, linewidth=3)
