Gap SMA
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jdavenport416

//@version=5
indicator("Gap SMA", overlay=true)

n = input(5, title="Number of gaps to consider")
multiplier = input.float(1.5, title="Multiplier for Gap", step=0.1)

var float avg_gap_up = na
var float avg_gap_down = na

gap_ups = open > close[1] ? (open - close[1]) * multiplier : na
gap_downs = open < close[1] ? (close[1] - open) * multiplier : na

avg_gap_up := ta.sma(na(gap_ups) ? avg_gap_up[1] : gap_ups, n)
avg_gap_down := ta.sma(na(gap_downs) ? avg_gap_down[1] : gap_downs, n)

if bar_index > n
    avg_gap_up := avg_gap_up
    avg_gap_down := avg_gap_down

close_plus_gap_up_avg = close + avg_gap_up
close_minus_gap_down_avg = close - avg_gap_down

plot(close_plus_gap_up_avg, color=color.green, title="Close + Avg Gap Up")
plot(close_minus_gap_down_avg, color=color.red, title="Close - Avg Gap Down")

// Alert conditions
alertcondition(close > close_plus_gap_up_avg, "Close Crosses Above Avg Gap Up", "Close crosses above the 'Close + Avg Gap Up' line")
alertcondition(close < close_minus_gap_down_avg, "Close Crosses Below Avg Gap Down", "Close crosses below the 'Close - Avg Gap Down' line")
