Forex Radar
// 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("Forex Radar", "FR", false, max_lines_count = 500, max_labels_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))

ema_diff_state(state, a, b) =>
    state ? a : b
 
state = input.bool(false, "Switch to the other set.")
style = input.bool(true, "Select Style", "True is rsi, false is ema divergance.")
axes_color = input.color(color.gray, "Axes Color")
scale = input.int(30, "Scale", 1)
length = input.int(200, "EMA Divergance Length", 5)
length_rsi = input.int(14, "RSI Length", 3)
filter = input.int(2, "Smooting", 1)
len_norm = input.int(200, "Normalization Lookback", 5)

color_AUD = input.color(color.new(#FF9900, 70), "AUD Color") // Australia - Orange
color_CAD = input.color(color.new(#FF0000, 70), "CAD Color") // Canada - Red
color_CHF = input.color(color.new(#FFD700, 70), "CHF Color") // Switzerland - Gold
color_EUR = input.color(color.new(#0000FF, 70), "EUR Color") // European Union - Blue
color_GBP = input.color(color.new(#228B22, 70), "GBP Color") // United Kingdom - Green
color_JPY = input.color(color.new(#B22222, 70), "JPY Color") // Japan - Maroon
color_NZD = input.color(color.new(#663388, 70), "NZD Color") // New Zealand - Purple
color_USD = input.color(color.new(#1E90FF, 70), "USD Color") // United States - Light Blue

ema_diff() =>
    not style ? normalize(ta.ema(close, 3) - ta.ema(close, length), len_norm) : ta.rsi(ta.sma(close, filter), length_rsi) / 100


// AUD pairs
selected_ema_diff_AUD1 = ema_diff_state(state, request.security("OANDA:AUDCAD", timeframe.period, ema_diff()), request.security("OANDA:AUDCHF", timeframe.period, ema_diff()))
selected_ema_diff_AUD2 = ema_diff_state(state, request.security("OANDA:AUDJPY", timeframe.period, ema_diff()), request.security("OANDA:AUDNZD", timeframe.period, ema_diff()))
selected_ema_diff_AUD3 = ema_diff_state(state, request.security("OANDA:AUDUSD", timeframe.period, ema_diff()), request.security("OANDA:EURAUD", timeframe.period, ema_diff()))
selected_ema_diff_AUD4 = ema_diff_state(state, request.security("OANDA:GBPAUD", timeframe.period, ema_diff()), request.security("OANDA:GBPAUD", timeframe.period, ema_diff()))

// CAD pairs
selected_ema_diff_CAD1 = ema_diff_state(state, request.security("OANDA:AUDCAD", timeframe.period, ema_diff()), request.security("OANDA:CADCHF", timeframe.period, ema_diff()))
selected_ema_diff_CAD2 = ema_diff_state(state, request.security("OANDA:CADJPY", timeframe.period, ema_diff()), request.security("OANDA:EURCAD", timeframe.period, ema_diff()))
selected_ema_diff_CAD3 = ema_diff_state(state, request.security("OANDA:GBPCAD", timeframe.period, ema_diff()), request.security("OANDA:NZDCAD", timeframe.period, ema_diff()))

// CHF pairs
selected_ema_diff_CHF1 = ema_diff_state(state, request.security("OANDA:CHFJPY", timeframe.period, ema_diff()), request.security("OANDA:EURCHF", timeframe.period, ema_diff()))
selected_ema_diff_CHF2 = ema_diff_state(state, request.security("OANDA:GBPCHF", timeframe.period, ema_diff()), request.security("OANDA:NZDCHF", timeframe.period, ema_diff()))

// EUR pairs
selected_ema_diff_EUR1 = ema_diff_state(state, request.security("OANDA:EURCAD", timeframe.period, ema_diff()), request.security("OANDA:EURCHF", timeframe.period, ema_diff()))
selected_ema_diff_EUR2 = ema_diff_state(state, request.security("OANDA:EURGBP", timeframe.period, ema_diff()), request.security("OANDA:EURJPY", timeframe.period, ema_diff()))
selected_ema_diff_EUR3 = ema_diff_state(state, request.security("OANDA:EURNZD", timeframe.period, ema_diff()), request.security("OANDA:EURUSD", timeframe.period, ema_diff()))

// GBP pairs
selected_ema_diff_GBP1 = ema_diff_state(state, request.security("OANDA:GBPCAD", timeframe.period, ema_diff()), request.security("OANDA:GBPCHF", timeframe.period, ema_diff()))
selected_ema_diff_GBP2 = ema_diff_state(state, request.security("OANDA:GBPJPY", timeframe.period, ema_diff()), request.security("OANDA:GBPNZD", timeframe.period, ema_diff()))
selected_ema_diff_GBP3 = ema_diff_state(state, request.security("OANDA:GBPUSD", timeframe.period, ema_diff()), request.security("OANDA:GBPUSD", timeframe.period, ema_diff()))

// JPY pairs
selected_ema_diff_JPY1 = ema_diff_state(state, request.security("OANDA:CADJPY", timeframe.period, ema_diff()), request.security("OANDA:CHFJPY", timeframe.period, ema_diff()))
selected_ema_diff_JPY2 = ema_diff_state(state, request.security("OANDA:EURJPY", timeframe.period, ema_diff()), request.security("OANDA:GBPJPY", timeframe.period, ema_diff()))
selected_ema_diff_JPY3 = ema_diff_state(state, request.security("OANDA:NZDJPY", timeframe.period, ema_diff()), request.security("OANDA:USDJPY", timeframe.period, ema_diff()))

// NZD pairs
selected_ema_diff_NZD1 = ema_diff_state(state, request.security("OANDA:NZDCAD", timeframe.period, ema_diff()), request.security("OANDA:NZDCHF", timeframe.period, ema_diff()))
selected_ema_diff_NZD2 = ema_diff_state(state, request.security("OANDA:NZDJPY", timeframe.period, ema_diff()), request.security("OANDA:NZDUSD", timeframe.period, ema_diff()))

// USD pairs
selected_ema_diff_USD1 = ema_diff_state(state, request.security("OANDA:USDCAD", timeframe.period, ema_diff()), request.security("OANDA:USDCHF", timeframe.period, ema_diff()))
selected_ema_diff_USD2 = ema_diff_state(state, request.security("OANDA:USDJPY", timeframe.period, ema_diff()), request.security("OANDA:USDCAD", timeframe.period, ema_diff()))

data = array.from(
 selected_ema_diff_AUD1, selected_ema_diff_AUD2, selected_ema_diff_AUD3, selected_ema_diff_AUD4,
 selected_ema_diff_CAD1, selected_ema_diff_CAD2, selected_ema_diff_CAD3,
 selected_ema_diff_CHF1, selected_ema_diff_CHF2,
 selected_ema_diff_EUR1, selected_ema_diff_EUR2, selected_ema_diff_EUR3,
 selected_ema_diff_GBP1, selected_ema_diff_GBP2, selected_ema_diff_GBP3,
 selected_ema_diff_JPY1, selected_ema_diff_JPY2, selected_ema_diff_JPY3,
 selected_ema_diff_NZD1, selected_ema_diff_NZD2,
 selected_ema_diff_USD1, selected_ema_diff_USD2
 )

color_data = array.from(
 color_AUD, color_AUD, color_AUD, color_AUD,
 color_CAD, color_CAD, color_CAD,
 color_CHF, color_CHF,
 color_EUR, color_EUR, color_EUR,
 color_GBP, color_GBP, color_GBP,
 color_JPY, color_JPY, color_JPY,
 color_NZD, color_NZD,
 color_USD, color_USD
 )

label_color = array.from(color_AUD, color_CAD, color_CHF, color_EUR, color_GBP, color_JPY, color_NZD, color_USD)

// Define labels for both sets

labels_AUD_set1 = array.from("AUDCAD", "AUDJPY", "AUDUSD", "GBPAUD")
labels_AUD_set2 = array.from("AUDCHF", "AUDNZD", "EURAUD", "GBPAUD")
labels_CAD_set1 = array.from("AUDCAD", "CADJPY", "GBPCAD")
labels_CAD_set2 = array.from("CADCHF", "EURCAD", "NZDCAD")
labels_CHF_set1 = array.from("CHFJPY", "GBPCHF")
labels_CHF_set2 = array.from("EURCHF", "NZDCHF")
labels_EUR_set1 = array.from("EURCAD", "EURGBP", "EURNZD")
labels_EUR_set2 = array.from("EURCHF", "EURJPY", "EURUSD")
labels_GBP_set1 = array.from("GBPCAD", "GBPJPY", "GBPUSD")
labels_GBP_set2 = array.from("GBPCHF", "GBPNZD", "GBPUSD")
labels_JPY_set1 = array.from("CADJPY", "EURJPY", "NZDJPY")
labels_JPY_set2 = array.from("CHFJPY", "GBPJPY", "USDJPY")
labels_NZD_set1 = array.from("NZDCAD", "NZDJPY")
labels_NZD_set2 = array.from("NZDCHF", "NZDUSD")
labels_USD_set1 = array.from("USDCAD", "USDJPY")
labels_USD_set2 = array.from("USDCHF", "USDCAD")

// Labels
var label_array = array.new_label(0)

// Function to draw labels
draw_labels(_labels_set, _label_colors, _offset, colour) =>
    centerX = bar_index - 75 + _offset
    y = -0.9
    for i = 0 to array.size(_labels_set) - 1
        array.push(label_array, label.new(centerX, y, text=array.get(_labels_set, i), color=array.get(_label_colors, colour), style=label.style_label_center, textcolor=color.white))
        y := y + 0.5
        if y >= -0.9 + 0.5 * 7
            y := -0.9
            centerX := centerX
    if barstate.isconfirmed
        for i = 0 to array.size(label_array) - 1
            label.delete(array.get(label_array, i))
            
if barstate.islast
    array.clear(label_array)
    if state
        draw_labels(labels_AUD_set1, label_color, 0, 0)
        draw_labels(labels_CAD_set1, label_color, 6, 1)
        draw_labels(labels_CHF_set1, label_color, 12, 2)
        draw_labels(labels_EUR_set1, label_color, 18, 3)
        draw_labels(labels_GBP_set1, label_color, 24, 4)
        draw_labels(labels_JPY_set1, label_color, 30, 5)
        draw_labels(labels_NZD_set1, label_color, 36, 6)
        draw_labels(labels_USD_set1, label_color, 42, 7)
    else
        draw_labels(labels_AUD_set2, label_color, 0, 0)
        draw_labels(labels_CAD_set2, label_color, 6, 1)
        draw_labels(labels_CHF_set2, label_color, 12, 2)
        draw_labels(labels_EUR_set2, label_color, 18, 3)
        draw_labels(labels_GBP_set2, label_color, 24, 4)
        draw_labels(labels_JPY_set2, label_color, 30, 5)
        draw_labels(labels_NZD_set2, label_color, 36, 6)
        draw_labels(labels_USD_set2, label_color, 42, 7)
else
    for lbl in label_array
        label.delete(lbl)
    array.clear(label_array)



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