Currency Strength
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HayeTrading

//@version=5
indicator("Currency Strength", overlay=true)
rsilen = input.int(14, "RSI Length")

rsi = ta.rsi(close, rsilen)

// Reverse the ticker value to make standard base currency
rev(sec) =>
    _r = 100 - sec
    _r

// USD
usdjpy = request.security("USDJPY", "", rsi)
usdcad = request.security("USDCAD", "", rsi)
eurusd = request.security("EURUSD", "", rsi)
eurusd_r = rev(eurusd)
gbpusd = request.security("GBPUSD", "", rsi)
gbpusd_r = rev(gbpusd)
audusd = request.security("AUDUSD", "", rsi)
audusd_r = rev(audusd)
c_usd = math.avg(usdjpy,usdcad,eurusd_r,gbpusd_r,audusd_r)

// EUR
eurjpy = request.security("EURJPY", "", rsi)
eurcad = request.security("EURCAD", "", rsi)
eurgbp = request.security("EURGBP", "", rsi)
// eurusd
euraud = request.security("EURAUD", "", rsi)
c_eur = math.avg(eurjpy,eurcad,eurgbp,eurusd,euraud)

// GBP
// gbpusd
eurgbp_r = rev(eurgbp)
gbpjpy = request.security("GBPJPY", "", rsi)
gbpcad = request.security("GBPCAD", "", rsi)
gbpaud = request.security("GBPAUD", "", rsi)
c_gbp = math.avg(gbpusd,eurgbp_r,gbpjpy,gbpcad,gbpaud)

// CAD
usdcad_r = rev(usdcad)
eurcad_r = rev(eurcad)
gbpcad_r = rev(gbpcad)
cadjpy = request.security("CADJPY", "", rsi)
audcad = request.security("AUDCAD", "", rsi)
audcad_r = rev(audcad)
c_cad = math.avg(usdcad_r,eurcad_r,gbpcad_r,cadjpy,audcad_r)

// AUD
// audusd
euraud_r = rev(euraud)
gbpaud_r = rev(gbpaud)
// audcad
audjpy = request.security("AUDJPY", "", rsi)
c_aud = math.avg(audusd,euraud_r,gbpaud_r,audcad,audjpy)

// JPY
usdjpy_r = rev(usdjpy)
eurjpy_r = rev(eurjpy)
gbpjpy_r = rev(gbpjpy)
cadjpy_r = rev(cadjpy)
audjpy_r = rev(audjpy)
c_jpy = math.avg(usdjpy_r,eurjpy_r,gbpjpy_r,cadjpy_r,audjpy_r)
    
// Round RSI average values to 1 decimal place
r_usd = math.round(c_usd, 1)
r_eur = math.round(c_eur, 1)
r_gbp = math.round(c_gbp, 1)
r_cad = math.round(c_cad, 1)
r_aud = math.round(c_aud, 1)
r_jpy = math.round(c_jpy, 1)

// Calculate background color scale
f_col(val) =>
    g_base = 155
    g_scale = g_base + math.min(((val - 50) * 8), 100)
    g_col = color.rgb(0, g_scale, 7)
    r_base = 90
    r_rev_val = 100 - val
    r_scale = r_base - math.min(((r_rev_val - 50) * 3), 90)
    r_col = color.rgb(255, r_scale, r_scale)
    _col = val >= 50 ? g_col : r_col
    _col

usd_col = f_col(r_usd)
eur_col = f_col(r_eur)
gbp_col = f_col(r_gbp)
cad_col = f_col(r_cad)
aud_col = f_col(r_aud)
jpy_col = f_col(r_jpy)

// Higher or lower than the value X bars ago
difflen = input.int(5, "X Bar Change")

highlow(val, difflen) =>
    arrow = val > val[difflen] ? "▲" : val < val[difflen] ? "▼" : "-"
    arrow

usd_hl = highlow(r_usd, difflen)
eur_hl = highlow(r_eur, difflen)
gbp_hl = highlow(r_gbp, difflen)
cad_hl = highlow(r_cad, difflen)
aud_hl = highlow(r_aud, difflen)
jpy_hl = highlow(r_jpy, difflen)

i_tableYpos = input.string("top", "Panel position", options = ["top", "middle", "bottom"])
i_tableXpos = input.string("right", "", options = ["left", "center", "right"])
i_size = input.string("Large", "Text Size", options = ["Large", "Normal", "Auto"])
t_size = i_size == "Large" ? size.large : i_size == "Normal" ? size.normal : size.auto
var table panel = table.new(i_tableYpos + "_" + i_tableXpos, 3, 7, frame_color=color.black, frame_width=1, border_color=color.black, border_width=1)
if barstate.islast
    // Table header.
    table.cell(panel, 0, 0, "Ticker", bgcolor = color.orange)
    table.cell(panel, 1, 0, "RSI", bgcolor = color.orange)
    table.cell(panel, 2, 0, "Dir.(" + str.tostring(difflen) + ")", bgcolor = color.orange)
    // Column 1
    table.cell(panel, 0, 1, "USD", bgcolor = usd_col, text_size=t_size)
    table.cell(panel, 0, 2, "EUR", bgcolor = eur_col, text_size=t_size)
    table.cell(panel, 0, 3, "GBP", bgcolor = gbp_col, text_size=t_size)
    table.cell(panel, 0, 4, "CAD", bgcolor = cad_col, text_size=t_size)
    table.cell(panel, 0, 5, "AUD", bgcolor = aud_col, text_size=t_size)
    table.cell(panel, 0, 6, "JPY", bgcolor = jpy_col, text_size=t_size)
    // Column 2
    table.cell(panel, 1, 1, str.tostring(r_usd), bgcolor = usd_col, text_size=t_size)
    table.cell(panel, 1, 2, str.tostring(r_eur), bgcolor = eur_col, text_size=t_size)
    table.cell(panel, 1, 3, str.tostring(r_gbp), bgcolor = gbp_col, text_size=t_size)
    table.cell(panel, 1, 4, str.tostring(r_cad), bgcolor = cad_col, text_size=t_size)
    table.cell(panel, 1, 5, str.tostring(r_aud), bgcolor = aud_col, text_size=t_size)
    table.cell(panel, 1, 6, str.tostring(r_jpy), bgcolor = jpy_col, text_size=t_size)
    // Column 3
    table.cell(panel, 2, 1, usd_hl, bgcolor = usd_col, text_size=t_size)
    table.cell(panel, 2, 2, eur_hl, bgcolor = eur_col, text_size=t_size)
    table.cell(panel, 2, 3, gbp_hl, bgcolor = gbp_col, text_size=t_size)
    table.cell(panel, 2, 4, cad_hl, bgcolor = cad_col, text_size=t_size)
    table.cell(panel, 2, 5, aud_hl, bgcolor = aud_col, text_size=t_size)
    table.cell(panel, 2, 6, jpy_hl, bgcolor = jpy_col, text_size=t_size)
    
