GSI (Gap Size Indicator)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © noop42

//@version=5
indicator('GSI (Gap Size Indicator)', overlay=false)

// Options
mode = input.string("%", "Mode", options=["%", "Price"])
trig_value = input.float(1.0, "Variation trigger", minval=0.001, maxval=100.0)
show_lines = input.bool(true, "Show trigger lines")
color_signals_only = input.bool(false, "Color signals only")
signals_only = input.bool(false, "Show signals only")

// Gap calculation
gap_side = close[1] > open ? -1 : close[1] < open ? 1 : 0
gap_pct = (1 - (close[1] / open)) * 100
gap_price = (open - close[1])
gap_value = mode == "%" ? gap_pct : gap_price

// Signal
triggered = (gap_side * gap_value) >= trig_value

// plots
lineCol = (color_signals_only and triggered) or (color_signals_only == false) ? (gap_side == 1 ? color.blue : gap_side == -1 ? color.red : na) : color.gray
gap = (signals_only and triggered) or (signals_only == false) ? gap_value : na

hline(show_lines ? trig_value : na, color=color.blue, title="Positive trigger")
hline(show_lines ? trig_value * -1 : na, color=color.red, title="Negative trigger")

plot(gap, color=lineCol, style=plot.style_columns, title="Gap value")

// Alerts
alertcondition(triggered, title="Any Gap", message="Gap detected")
alertcondition(triggered and gap_side == 1, title="Bullish Gap", message="Bullish gap")
alertcondition(triggered and gap_side == -1, title="Bearish Gap", message="Bearish gap")



