Exceptional Volume Spike - Potential Trend Reversal Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Tradager

//@version=5
indicator(title="Trend Reversal Indicator", shorttitle="TRI", overlay=true)

// Input parameters with default values
relative_volume_threshold = input(4.0, title="Relative Volume Threshold")
ema_length = input(20, title="EMA Length")
lookback_period = input(200, title="Support/Resistance Lookback Period")

// Calculate average volume over the lookback period
avg_volume = ta.sma(volume, lookback_period)

// Calculate relative volume
rel_volume = volume / avg_volume

// Identify exceptional volume spikes
is_spike = rel_volume > relative_volume_threshold

// Calculate EMA of exceptional volume spikes
ema_spikes = ta.ema(is_spike ? volume : 0, ema_length)

// Determine trend direction based on crossover of EMA of exceptional volume spikes
trend_up = ta.crossover(ema_spikes, ta.ema(is_spike ? 0 : volume, ema_length))
trend_down = ta.crossunder(ema_spikes, ta.ema(is_spike ? 0 : volume, ema_length))

// Calculate highest high and lowest low
hh = ta.highest(high, lookback_period)
ll = ta.lowest(low, lookback_period)

// Plot triangles and trend reversal markers with larger size
plotshape(series=is_spike and trend_up, style=shape.triangleup, location=location.belowbar, color=color.new(color.red, 0), size=size.small)
plotshape(series=is_spike and trend_down, style=shape.triangledown, location=location.abovebar, color=color.new(color.green, 0), size=size.small)

// Plot support and resistance levels
plot(hh, title="Potential Resistance", color=color.new(color.orange, 0), linewidth=2)
plot(ll, title="Potential Support", color=color.new(color.purple, 0), linewidth=2)

// Calculate and plot EMA of closing price
ema_close = ta.ema(close, ema_length)
plot(ema_close, title="EMA", color=ema_close > ema_close[1] ? color.new(color.green, 0) : ema_close < ema_close[1] ? color.new(color.red, 0) : color.new(color.blue, 0))
