User Defined Momentum Change with Swing Visuals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Crypto_Moses

//@version=5
indicator(title="User Defined Momentum Change with Swing Visuals", shorttitle="Momentum Swing Visualizer", overlay=true, max_bars_back=1000)

// ----- Variables from the first script -----
len = input.int(21, minval=1, title='Length')
src1 = input(close, title='Source')
off = input(3, title='Offset')
smooth = input(2, title='Smooth Fast')
smoothSlow = input(21, title='Smooth Slow')

// Add user-defined high and low signal offsets
high_offset = input.int(0, title="swing range Signal Offset")
low_offset = input.int(0, title="swing range Signal Offset")

// Use EMA instead of SMA
out = ta.ema(src1, len)

// Modify high and low signal conditions with user-defined offsets
deup = ta.crossover(close, out[3 + high_offset]) and close[1] < out[4 + high_offset] and close[2] < out[5 + high_offset] and close[3] < out[6 + high_offset] and close[4] < out[7 + high_offset] and close[5] < out[8 + high_offset] and close[6] < out[9 + high_offset] and close[7] < out[10 + high_offset] and close[8] < out[11 + high_offset]
delo = ta.crossunder(close, out[3 + low_offset]) and close[1] > out[4 + low_offset] and close[2] > out[5 + low_offset] and close[3] > out[6 + low_offset] and close[4] > out[7 + low_offset] and close[5] > out[8 + low_offset] and close[6] > out[9 + low_offset] and close[7] > out[10 + low_offset] and close[8] > out[11 + low_offset]

// Calculate Stochastic %D instead of %K
stoch_k = ta.stoch(close, high, low, len)
stoch_d = ta.sma(stoch_k, 3)
fast = ta.ema(stoch_d, smooth)
slow = ta.ema(fast, smoothSlow)

// ----- Stochastic Bar Coloring -----
stoch_length = input.int(14, title="Stochastic Length", minval=1)
bar_color_transparency = input.int(80, title="Bar Color Transparency", minval=0, maxval=100)

color_upper1 = input(color.green, title="Upper Range Color 1")
color_upper2 = input(color.lime, title="Upper Range Color 2")
color_upper3 = input(color.aqua, title="Upper Range Color 3")
color_middle = input(color.yellow, title="Middle Range Color")
color_lower1 = input(color.red, title="Lower Range Color 1")
color_lower2 = input(color.maroon, title="Lower Range Color 2")
color_lower3 = input(color.purple, title="Lower Range Color 3")

upper_threshold1 = input.int(80, title="Upper Range Threshold 1", minval=0, maxval=100)
upper_threshold2 = input.int(90, title="Upper Range Threshold 2", minval=0, maxval=100)
upper_threshold3 = input.int(95, title="Upper Range Threshold 3", minval=0, maxval=100)
lower_threshold1 = input.int(50, title="Lower Range Threshold 1", minval=0, maxval=100)
lower_threshold2 = input.int(20, title="Lower Range Threshold 2", minval=0, maxval=100)
lower_threshold3 = input.int(10, title="Lower Range Threshold 3", minval=0, maxval=100)

// Calculate stochastic %D and get color based on the value
stoch = stoch_d
stoch_color = stoch >= upper_threshold3 ? color.new(color_upper1, bar_color_transparency) : stoch >= upper_threshold2 ? color.new(color_upper2, bar_color_transparency) : stoch >= upper_threshold1 ? color.new(color_upper3, bar_color_transparency) : stoch >= lower_threshold1 ? color.new(color_middle, bar_color_transparency) : stoch >= lower_threshold2 ? color.new(color_lower1, bar_color_transparency) : stoch >= lower_threshold3 ? color.new(color_lower2, bar_color_transparency) : color.new(color_lower3, bar_color_transparency)
barcolor(stoch_color)

// User-definable vertical line options
line_color_high = input(color.green, title="High Line Color")
line_color_low = input(color.red, title="Low Line Color")
line_width = input.int(1, title="Line Width", minval=1)
line_offset = input.int(0, title="Line Offset")

for i = 1 to 1000
    if deup[i]
        line.new(x1=bar_index[i + line_offset], y1=ta.lowest(i), x2=bar_index[i + line_offset], y2=ta.highest(i), width=line_width, color=line_color_high)
    if delo[i]
        line.new(x1=bar_index[i + line_offset], y1=ta.lowest(i), x2=bar_index[i + line_offset], y2=ta.highest(i), width=line_width, color=line_color_low)