Volume Spikes
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tradeforopp

//@version=5
indicator("Volume Spikes [TFO]", "Volume Spikes [TFO]", overlay=true)


// Variables and constants

vol_x = input.float(1.5, step=0.1, title="Volume Multiplier", tooltip="This multiplier will act as a threshold to determine which volume spikes are shown. A volume multiplier of 2 will only show volume spikes that are 2x the value of the volume SMA")
vol_ma = input(100, "Volume SMA Length")
only_valid_hl = input(true, "Only Use Valid Highs & Lows", tooltip="Only show long signals when the candle in question forms a local low, and vice versa")
only_hammers_shooters = input(true, "Only Use Hammers & Shooters")
only_same_color = input(false, "Only Use Same-Close Volume Spikes", tooltip="Only show long signals when the volume spike appears on an up-close candle, and vice versa")
session = input.session("0000-0000", "Session Time")
bull_color = input.color(color.green)
bear_color = input.color(color.red)

t = not na(time(timeframe.period, session))


// Candle calculations

valid_high = only_valid_hl ? high[1] > high[2] and high[1] > high[0] : true
valid_low = only_valid_hl ? low[1] < low[2] and low[1] < low[0] : true

distance_hl = high - low
valid_hammer = open > low + distance_hl / 2 and close > low + distance_hl / 2
valid_shooter = open < low + distance_hl / 2 and close < low + distance_hl / 2

if only_hammers_shooters
    valid_high := valid_high and valid_shooter[1]
    valid_low := valid_low and valid_hammer[1]

if only_same_color
    valid_high := valid_high and close[1] < open[1]
    valid_low := valid_low and close[1] > open[1]


// Volume check

// Is volume sufficiently greater than its MA?
vol_check = volume > ta.sma(volume, vol_ma) * vol_x and t

result_bearish = valid_high and vol_check[1]
result_bullish = valid_low and vol_check[1]

alertcondition(result_bearish, "Bearish Volume Spike", "Bearish Volume Spike")
alertcondition(result_bullish, "Bullish Volume Spike", "Bullish Volume Spike")

plotshape(result_bearish, color=bear_color, style=shape.circle, location=location.abovebar, size=size.tiny, offset = -1)
plotshape(result_bullish, color=bull_color, style=shape.circle, location=location.belowbar, size=size.tiny, offset = -1)