Vector Scaler
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("VS")
diff(_src, enable) =>
    derivative = (_src - _src[2])/2
    enable ? derivative : _src
// Define the vector_scale() function
vector_scale(sr, len, smo, diff, rsi, rsi_len) =>
    // Calculate the average of the vector
    src = rsi ? ta.sma(diff(ta.rsi(close, rsi_len), diff), smo) : ta.sma(diff(sr, diff), smo)
    avg = rsi ? (diff ? 0 : 50) : (diff ? 0 : ta.sma(src, len))

    // Calculate the magnitude of the vector
    magnitude = math.sqrt(math.pow(src - avg, 2) + math.pow(src[1] - avg[1], 2) + math.pow(src[2] - avg[2], 2))

    // Normalize the vector
    normalizedVec = (src - avg) / magnitude

    // Smooth the normalized vector
    smoothedVec = (normalizedVec + 1) * 50

    // Return the smoothed, normalized vector
    smoothedVec

src = input.source(close, "Source")
len = input.int(12, "Average Length")
smo = input.int(3, "Smoothing", 1) 
rsi = input.bool(false, "RSI Mode")
rsi_len = input.int(14, "RSI Length")
dif = input.bool(false, "Delta Mode")
out = vector_scale(src, len, smo, dif, rsi, rsi_len)
lag = ta.sma(out, smo == 1 ? smo + 1 : smo == 3 ? 2 : smo > 3 ? int(smo/2) : smo)

f = plot(out)
s = plot(lag, color = color.orange)

hline(50)
top = hline(80)
bot = hline(20)
fill(top, bot, color.new(color.navy, 80))