Radar Rider
// 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("Radar Rider", max_lines_count = 500)
import peacefulLizard50262/Spider_Plot/1 as s

normalize(close, len) =>
    (close - ta.lowest(close, len))/(ta.highest(close, len) - ta.lowest(close, len))


bes(source, alpha)=>
    var float smoothed = na
    smoothed := na(smoothed) ? source : alpha * source + (1 - alpha) * nz(smoothed[1])

max(src)=>
    var float max = na
    max := math.max(nz(max[1]), src)

min(src)=>
    var float min = na
    min := math.min(nz(min[1]), src)

min_max(src)=>
    (src - min(src))/(max(src) - min(src)) * 100

slope(source, length, n_len, pre_smoothing = 0.15, post_smoothing = 0.7)=>
    src = bes(source, pre_smoothing)
    avg = array.new<float>(length, na)
    for i = 1 to length
        array.push(avg, (src - src[i]))
    slope = normalize(bes(array.avg(avg), post_smoothing), n_len)

percent(x, y) =>
    ((y - x)/x)*100

derivative(_close, len_mad) =>
    switch len_mad
        1 => (_close - _close[1]) / 1
        2 => (_close - _close[2]) / 2
        3 => (_close + _close[1] - _close[2] - _close[3]) / 4
        4 => (_close + 2 * _close[1] - 2 * _close[3] - _close[4]) / 8
        5 => (_close + 3 * _close[1] + 2 * _close[2] - 2 * _close[3] - 3 * _close[4] - _close[5]) / 16
        6 => (_close + 4 * _close[1] + 5 * _close[2] - 5 * _close[4] - 4 * _close[5] - _close[6]) / 32
        7 => (_close + 5 * _close[1] + 9 * _close[2] + 5 * _close[3] - 5 * _close[4] - 9 * _close[5] - 5 * _close[6] - _close[7]) / 64
        8 => (_close + 6 * _close[1] + 14 * _close[2] + 14 * _close[3] - 14 * _close[5] - 14 * _close[6] - 6 * _close[7] - _close[8]) / 128
        9 => (_close + 7 * _close[1] + 20 * _close[2] + 28 * _close[3] + 14 * _close[4] - 14 * _close[5] - 28 * _close[6] - 20 * _close[7] - 7 * _close[8] - _close[9]) / 256
        10 => (_close + 8 * _close[1] + 27 * _close[2] + 48 * _close[3] + 42 * _close[4] - 42 * _close[6] - 48 * _close[7] - 27 * _close[8] - 8 * _close[9] - _close[10]) / 512


stdev(x, n)=>
    out = math.sqrt(math.sum(math.pow(x - ta.ema(x, n), 2), n) * 0.5) * 0.5
    out

filter(src, filter)=>
    filter ? (src + (src[1] * 2) + (src[2] * 2) + src[3])/6 : src

z(src, len, filter, back)=>
    normalize((filter(src, filter) - ta.ema(filter(src, filter), len))/stdev(filter(src, filter), len), back)

rsi(close, len) =>
    rsi = ta.rsi(close, len)
    f = -math.pow(math.abs(math.abs(rsi - 50) - 50), 1 + math.pow(len / 14, 0.618) - 1) / math.pow(50, math.pow(len / 14, 0.618) - 1) + 50
    rsia = if rsi > 50
        f + 50
    else
        -f + 50
    rsia

ema_diff(length, close, filter, len_norm) =>
    normalize(ta.ema(filter(close, filter), 3) - ta.ema(filter(close, filter), length), len_norm)

percent_rank_ema_diff(length, close, filter, len_norm) =>
    s.data_normalize(ta.percentrank(ta.ema(filter(close, filter), 5) - ta.ema(filter(close, filter), length), len_norm), "Custom", 1, 1, 1, 0, 100)

ema_diff_longer(length, close, filter, len_norm) =>
    normalize(ta.ema(filter(close, filter), 15) - ta.ema(filter(close, filter), 20 + length), len_norm)

rsi_filter(length, close, filter) =>
    s.data_normalize(filter(rsi(filter(close, true), length), filter), "Custom", 1, 1, 1, 0, 100)

rsi_diff_normalized(length, close, filter, len_mad, len_norm) =>
    normalize(derivative(rsi(filter(close, filter), length), len_mad), len_norm)

z_score(length, close, filter, len_norm) =>
    z(close, length, filter, len_norm)

ema_normalized(length, close, filter, len_norm) =>
    normalize((length == 1 ? filter(close, filter) : ta.ema(filter(close, filter), (length - 1))), len_norm)

wma_volume_normalized(length, volume, filter, len_norm) =>
    normalize(filter(ta.wma(volume, (length - 1)), filter), len_norm)

ema_close_diff_normalized(length, close, filter, len_mad, len_norm) =>
    normalize(filter(derivative(ta.ema(close, length), len_mad), filter), len_norm)

momentum_normalized(length, close, filter, len_norm) =>
    normalize(ta.mom(filter(close, filter), length), len_norm)

sma_diff_ratio_normalized(length, close, filter, len_norm) =>
    normalize((ta.sma(filter(close[(-1 + length)], filter), len_norm) - ta.sma(filter(close, filter), len_norm))/-(length - 1), len_norm)

wma_close_open_diff_normalized(length, close, open, filter, len_norm) =>
    normalize(filter(ta.wma(close, length) - ta.wma(open, length), filter), len_norm)

rma_high_low_diff_normalized(length, high, low, filter, len_norm) =>
    normalize(filter(ta.rma(high - low, length), filter), len_norm)

roc_normalized(length, close, filter, len_norm) =>
    normalize(ta.roc(filter(close, filter), length), len_norm)

obv_diff_normalized(length, obv, filter, len_norm) =>
    normalize(filter(obv, filter) - ta.wma(filter(obv, filter), 2 + length), len_norm)

mfi_normalized(length, hlc3, filter, len_norm) =>
    normalize(filter(ta.mfi(hlc3, length), filter), len_norm)

bb_ratio_normalized(length, close, filter, len_norm) =>
    normalize(((ta.sma(filter(close, filter), length) + ta.stdev(filter(close, filter), length)) - (ta.sma(filter(close, filter), length) - ta.stdev(filter(close, filter), length)))/ta.sma(filter(close, filter), length), len_norm)

bb_ratio_percent_rank(length, close, filter, len_norm) =>
    s.data_normalize(ta.percentrank(((ta.sma(filter(close, filter), length) + ta.stdev(filter(close, filter), length)) - (ta.sma(filter(close, filter), length) - ta.stdev(filter(close, filter), length)))/ta.sma(filter(close, filter), length), len_norm), "Custom", 1, 1, 1, 0, 100)

percent_diff_normalized(length, close, open, filter, len_norm) =>
    normalize(percent(open[length], filter(close, filter)), len_norm)

slope_normalized(close, length, len_norm, filter) =>
    slope(close, length, len_norm, 0.7)

stoch_sma(length, close, high, low, filter, stochSmaNormalizatoinLength) =>
    normalize(ta.sma(ta.stoch(filter(close, filter), filter(high, filter), filter(low, filter), length), 3), stochSmaNormalizatoinLength)

emaDiffLength = input.int(14, "EMA Diff Length", 1)
emaDiffFilterStrength = input.int(2, "EMA Diff Filter Strength")
emaDiffLenNorm = input.int(25, "EMA Diff Length Normalization")

percentRankLength = input.int(14, "Percent Rank Length", 1)
percentRankEmaDiffFilterStrength = input.int(1, "Percent Rank EMA Diff Filter Strength")
percentRankEmaDiffLenNorm = input.int(25, "Percent Rank EMA Diff Length Normalization")

emaDiffLongerLength = input.int(14, "EMA Diff Longer Length", 1)
emaDiffLongerFilterStrength = input.int(2, "EMA Diff Longer Filter Strength")
emaDiffLongerLenNorm = input.int(25, "EMA Diff Longer Length Normalization")

rsiFilterLength = input.int(14, "RSI Filter Length", 1)
rsiFilterFilterStrength = input.int(1, "RSI Filter Filter Strength")

rsiDiffNormalizedLength = input.int(14, "RSI Diff Normalized Length", 1)
rsiDiffNormalizedFilterStrength = input.int(2, "RSI Diff Normalized Filter Strength")
rsiDiffNormalizedLenNorm = input.int(25, "RSI Diff Normalized Length Normalization")
rsiDiffNormalizedLenMed = input.int(4, "RSI Diff Normalized Diff Length", 1, 10)

zScoreLength = input.int(14, "Z Score Length", 1)
zScoreFilterStrength = input.int(2, "Z Score Filter Strength")
zScoreLenNorm = input.int(25, "Z Score Length Normalization")

emaNormalizedLength = input.int(14, "EMA Normalized Length", 1)
emaNormalizedFilterStrength = input.int(2, "EMA Normalized Filter Strength")
emaNormalizedLenNorm = input.int(25, "EMA Normalized Length Normalization")

wmaVolumeNormalizedLength = input.int(14, "WMA Volume Normalized Length", 1)
wmaVolumeNormalizedFilterStrength = input.int(2, "WMA Volume Normalized Filter Strength")
wmaVolumeNormalizedLenNorm = input.int(25, "WMA Volume Normalized Length Normalization")

emaCloseDiffNormalizedLength = input.int(14, "EMA Close Diff Normalized Length", 1)
emaCloseDiffNormalizedFilterStrength = input.int(2, "EMA Close Diff Normalized Filter Strength")
emaCloseDiffNormalizedLenNorm = input.int(25, "EMA Close Diff Normalized Length Normalization")
emaCloseDiffNormalizedLenMed = input.int(4, "Ema Close Diff Normalized Diff Length", 1, 10)

momentumNormalizedLength = input.int(14, "Momentum Normalized Length", 1)
momentumNormalizedFilterStrength = input.int(2, "Momentum Normalized Filter Strength")
momentumNormalizedLenNorm = input.int(25, "Momentum Normalized Length Normalization")

smaDiffRatioNormalizedLength = input.int(14, "SMA Diff Ratio Normalized Length", 1)
smaDiffRatioNormalizedFilterStrength = input.int(2, "SMA Diff Ratio Normalized Filter Strength")
smaDiffRatioNormalizedLenNorm = input.int(25, "SMA Diff Ratio Normalized Length Normalization")

wmaCloseOpenDiffNormalizedLength = input.int(14, "WMA Close Open Diff Normalized Length", 1)
wmaCloseOpenDiffNormalizedFilterStrength = input.int(2, "WMA Close Open Diff Normalized Filter Strength")
wmaCloseOpenDiffNormalizedLenNorm = input.int(25, "WMA Close Open Diff Normalized Length Normalization")

rmaHighLowDiffNormalizedLength = input.int(14, "RMA High Low Diff Normalized Length", 1)
rmaHighLowDiffNormalizedFilterStrength = input.int(2, "RMA High Low Diff Normalized Filter Strength")
rmaHighLowDiffNormalizedLenNorm = input.int(25, "RMA High Low Diff Normalized Length Normalization")

rocNormalizedLength = input.int(14, "ROC Normalized Length", 1)
rocNormalizedFilterStrength = input.int(2, "ROC Normalized Filter Strength")
rocNormalizedLenNorm = input.int(25, "ROC Normalized Length Normalization")

obvDiffNormalizedLength = input.int(14, "OBV Diff Normalized Length", 1)
obvDiffNormalizedFilterStrength = input.int(2, "OBV Diff Normalized Filter Strength")
obvDiffNormalizedLenNorm = input.int(25, "OBV Diff Normalized Length Normalization")

mfiNormalizedLength = input.int(14, "MFI Normalized Length", 1)
mfiNormalizedFilterStrength = input.int(2, "MFI Normalized Filter Strength")
mfiNormalizedLenNorm = input.int(25, "MFI Normalized Length Normalization")

bbRatioNormalizedLength = input.int(14, "BB Ratio Normalized Length", 1)
bbRatioNormalizedFilterStrength = input.int(2, "BB Ratio Normalized Filter Strength")
bbRatioNormalizedLenNorm = input.int(25, "BB Ratio Normalized Length Normalization")

bbRatioPercentRankLength = input.int(14, "BB Ratio Percent Rank Length", 1)
bbRatioPercentRankFilterStrength = input.int(2, "BB Ratio Percent Rank Filter Strength")
bbRatioPercentRankLenNorm = input.int(25, "BB Ratio Percent Rank Length Normalization")

percentDiffNormalizedLength = input.int(14, "Percent Diff Normalized Length", 1)
percentDiffNormalizedFilterStrength = input.int(2, "Percent Diff Normalized Filter Strength")
percentDiffNormalizedLenNorm = input.int(25, "Percent Diff Normalized Length Normalization")

slopeNormalizedLength = input.int(14, "Slope Normalized Length", 1)
slopeNormalizedFilterStrength = input.int(2, "Slope Normalized Filter Strength")
slopeNormalizedLenNorm = input.int(25, "Slope Normalized Length Normalization")

stochSmaLength = input.int(14, "Stoch SMA Length", 1)
stochSmaFilterStrength = input.int(2, "Stoch SMA Filter Strength")
stochSmaNormalizatoinLength = input.int(25, "Stoch SMA Normalizatoin Length")

axes_color = input.color(color.gray, "Axes Color")
scale = input.int(30, "Scale")
color_emaDiff = input.color(#FF6666, "EMA Diff Color")
color_percentRankEmaDiff = input.color(#66FF66, "Percent Rank EMA Diff Color")
color_emaDiffLonger = input.color(#FFA500, "EMA Diff Longer Color")
color_rsiFilter = input.color(#00FFFF, "RSI Filter Color")
color_rsiDiffNormalized = input.color(#9B30FF, "RSI Diff Normalized Color")
color_zScore = input.color(#4444ff, "Z Score Color")
color_emaNormalized = input.color(#32CD32, "EMA Normalized Color")
color_wmaVolumeNormalized = input.color(#FF4500, "WMA Volume Normalized Color")
color_emaCloseDiffNormalized = input.color(#4169E1, "EMA Close Diff Normalized Color")
color_momentumNormalized = input.color(#8B008B, "Momentum Normalized Color")
color_smaDiffRatioNormalized = input.color(#EE82EE, "SMA Diff Ratio Normalized Color")
color_wmaCloseOpenDiffNormalized = input.color(#2E8B57, "WMA Close Open Diff Normalized Color")
color_rmaHighLowDiffNormalized = input.color(#FFD700, "RMA High Low Diff Normalized Color")
color_rocNormalized = input.color(#BA55D3, "ROC Normalized Color")
color_obvDiffNormalized = input.color(#FF6347, "OBV Diff Normalized Color")
color_mfiNormalized = input.color(#3CB371, "MFI Normalized Color")
color_bbRatioNormalized = input.color(#B22222, "BB Ratio Normalized Color")
color_bbRatioPercentRank = input.color(#008080, "BB Ratio Percent Rank Color")
color_percentDiffNormalized = input.color(#6A5ACD, "Percent Diff Normalized Color")
color_slopeNormalized = input.color(#556B2F, "Slope Normalized Color")
color_stochSma = input.color(#800080, "Stoch SMA Color")

emaDiffVar = ema_diff(emaDiffLength, close, emaDiffFilterStrength, emaDiffLenNorm)
percentRankEmaDiffVar = percent_rank_ema_diff(percentRankLength, close, percentRankEmaDiffFilterStrength, percentRankEmaDiffLenNorm)
emaDiffLongerVar = ema_diff_longer(emaDiffLongerLength, close, emaDiffLongerFilterStrength, emaDiffLongerLenNorm)
rsiFilterVar = rsi_filter(rsiFilterLength, close, rsiFilterFilterStrength)
rsiDiffNormalizedVar = rsi_diff_normalized(rsiDiffNormalizedLength, close, rsiDiffNormalizedFilterStrength, rsiDiffNormalizedLenMed,rsiDiffNormalizedLenNorm)   ///
zScoreVar = z_score(zScoreLength, close, zScoreFilterStrength, zScoreLenNorm)
emaNormalizedVar = ema_normalized(emaNormalizedLength, close, emaNormalizedFilterStrength, emaNormalizedLenNorm)
wmaVolumeNormalizedVar = wma_volume_normalized(wmaVolumeNormalizedLength, volume, wmaVolumeNormalizedFilterStrength, wmaVolumeNormalizedLenNorm)
emaCloseDiffNormalizedVar = ema_close_diff_normalized(emaCloseDiffNormalizedLength, close, emaCloseDiffNormalizedFilterStrength, emaCloseDiffNormalizedLenMed,emaCloseDiffNormalizedLenNorm)   ///
momentumNormalizedVar = momentum_normalized(momentumNormalizedLength, close, momentumNormalizedFilterStrength, momentumNormalizedLenNorm)
smaDiffRatioNormalizedVar = sma_diff_ratio_normalized(smaDiffRatioNormalizedLength, close, smaDiffRatioNormalizedFilterStrength, smaDiffRatioNormalizedLenNorm)
wmaCloseOpenDiffNormalizedVar = wma_close_open_diff_normalized(wmaCloseOpenDiffNormalizedLength, close, open, wmaCloseOpenDiffNormalizedFilterStrength, wmaCloseOpenDiffNormalizedLenNorm)
rmaHighLowDiffNormalizedVar = rma_high_low_diff_normalized(rmaHighLowDiffNormalizedLength, high, low, rmaHighLowDiffNormalizedFilterStrength, rmaHighLowDiffNormalizedLenNorm)
rocNormalizedVar = roc_normalized(rocNormalizedLength, close, rocNormalizedFilterStrength, rocNormalizedLenNorm)
obvDiffNormalizedVar = obv_diff_normalized(obvDiffNormalizedLength, ta.obv, obvDiffNormalizedFilterStrength, obvDiffNormalizedLenNorm)
mfiNormalizedVar = mfi_normalized(mfiNormalizedLength, hlc3, mfiNormalizedFilterStrength, mfiNormalizedLenNorm)
bbRatioNormalizedVar = bb_ratio_normalized(bbRatioNormalizedLength, close, bbRatioNormalizedFilterStrength, bbRatioNormalizedLenNorm)
bbRatioPercentRankVar = bb_ratio_percent_rank(bbRatioPercentRankLength, close, bbRatioPercentRankFilterStrength, bbRatioPercentRankLenNorm)
percentDiffNormalizedVar = percent_diff_normalized(percentDiffNormalizedLength, close, open, percentDiffNormalizedFilterStrength, percentDiffNormalizedLenNorm)
slopeNormalizedVar = slope_normalized(close, slopeNormalizedLength, slopeNormalizedLenNorm, slopeNormalizedFilterStrength)
stochSmaVar = stoch_sma(stochSmaLength, close, high, low, stochSmaFilterStrength, stochSmaNormalizatoinLength)

data = array.from(
   emaDiffVar
 , percentRankEmaDiffVar
 , emaDiffLongerVar
 , rsiFilterVar
 , rsiDiffNormalizedVar
 , zScoreVar
 , emaNormalizedVar
 , wmaVolumeNormalizedVar
 , emaCloseDiffNormalizedVar
 , momentumNormalizedVar
 , smaDiffRatioNormalizedVar
 , wmaCloseOpenDiffNormalizedVar
 , rmaHighLowDiffNormalizedVar
 , rocNormalizedVar
 , obvDiffNormalizedVar
 , mfiNormalizedVar
 , bbRatioNormalizedVar
 , bbRatioPercentRankVar
 , percentDiffNormalizedVar
 , slopeNormalizedVar
 , stochSmaVar
 )

data_color = array.from(
 color_emaDiff,
 color_percentRankEmaDiff,
 color_emaDiffLonger,
 color_rsiFilter,
 color_rsiDiffNormalized,
 color_zScore,
 color_emaNormalized,
 color_wmaVolumeNormalized,
 color_emaCloseDiffNormalized,
 color_momentumNormalized,
 color_smaDiffRatioNormalized,
 color_wmaCloseOpenDiffNormalized,
 color_rmaHighLowDiffNormalized,
 color_rocNormalized,
 color_obvDiffNormalized,
 color_mfiNormalized,
 color_bbRatioNormalized,
 color_bbRatioPercentRank,
 color_percentDiffNormalized,
 color_slopeNormalized,
 color_stochSma
 )

s.draw_spider_plot(data, data_color, axes_color, scale)
