Currency Strength V2
// 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)
    
    
/// Second timeframe /////////
i_res = input.timeframe('60', "Resolution", options=['3', '5', '15', "30", "60", "120", "240", "D", "W"])

// USD
usdjpy2 = request.security("USDJPY", i_res, rsi)
usdcad2 = request.security("USDCAD", i_res, rsi)
eurusd2 = request.security("EURUSD", i_res, rsi)
eurusd_r2 = rev(eurusd2)
gbpusd2 = request.security("GBPUSD", i_res, rsi)
gbpusd_r2 = rev(gbpusd2)
audusd2 = request.security("AUDUSD", i_res, rsi)
audusd_r2 = rev(audusd2)
c_usd2 = math.avg(usdjpy2,usdcad2,eurusd_r2,gbpusd_r2,audusd_r2)

// EUR
eurjpy2 = request.security("EURJPY", i_res, rsi)
eurcad2 = request.security("EURCAD", i_res, rsi)
eurgbp2 = request.security("EURGBP", i_res, rsi)
// eurusd
euraud2 = request.security("EURAUD", i_res, rsi)
c_eur2 = math.avg(eurjpy2,eurcad2,eurgbp2,eurusd2,euraud2)

// GBP
// gbpusd
eurgbp_r2 = rev(eurgbp2)
gbpjpy2 = request.security("GBPJPY", i_res, rsi)
gbpcad2 = request.security("GBPCAD", i_res, rsi)
gbpaud2 = request.security("GBPAUD", i_res, rsi)
c_gbp2 = math.avg(gbpusd2,eurgbp_r2,gbpjpy2,gbpcad2,gbpaud2)

// CAD
usdcad_r2 = rev(usdcad2)
eurcad_r2 = rev(eurcad2)
gbpcad_r2 = rev(gbpcad2)
cadjpy2 = request.security("CADJPY", i_res, rsi)
audcad2 = request.security("AUDCAD", i_res, rsi)
audcad_r2 = rev(audcad2)
c_cad2 = math.avg(usdcad_r2,eurcad_r2,gbpcad_r2,cadjpy2,audcad_r2)

// AUD
// audusd
euraud_r2 = rev(euraud2)
gbpaud_r2 = rev(gbpaud2)
// audcad
audjpy2 = request.security("AUDJPY", i_res, rsi)
c_aud2 = math.avg(audusd2,euraud_r2,gbpaud_r2,audcad2,audjpy2)

// JPY
usdjpy_r2 = rev(usdjpy2)
eurjpy_r2 = rev(eurjpy2)
gbpjpy_r2 = rev(gbpjpy2)
cadjpy_r2 = rev(cadjpy2)
audjpy_r2 = rev(audjpy2)
c_jpy2 = math.avg(usdjpy_r2,eurjpy_r2,gbpjpy_r2,cadjpy_r2,audjpy_r2)
    

// 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)
r_usd2 = math.round(c_usd2, 1)
r_eur2 = math.round(c_eur2, 1)
r_gbp2 = math.round(c_gbp2, 1)
r_cad2 = math.round(c_cad2, 1)
r_aud2 = math.round(c_aud2, 1)
r_jpy2 = math.round(c_jpy2, 1)


// Higher or lower than the value X bars ago
difflen = input.int(3, "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)


// Calculate background color scale
transp=10
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, transp)
    _col = val >= 50 ? g_col : r_col
    _col

// Table colors
alt1 = color.rgb(236,216,64)
alt2 = color.rgb(240,185,74)

// Flat green / red
flat_col(val) =>
    _col = val >= 50 ? color.rgb(98,216,98) : color.rgb(229,92,89)
    _col

// Use value-based colors or flat colors
scale = input.string("Scale Color", options=["Scale Color", "Flat Color"])
use_scale = scale == "Scale Color" ? true : false

usd_col = use_scale ? f_col(r_usd) : flat_col(r_usd)
eur_col = use_scale ? f_col(r_eur) : flat_col(r_eur)
gbp_col = use_scale ? f_col(r_gbp) : flat_col(r_gbp)
cad_col = use_scale ? f_col(r_cad) : flat_col(r_cad)
aud_col = use_scale ? f_col(r_aud) : flat_col(r_aud)
jpy_col = use_scale ? f_col(r_jpy) : flat_col(r_jpy)
usd_col2 = use_scale ? f_col(r_usd2) : flat_col(r_usd2)
eur_col2 = use_scale ? f_col(r_eur2) : flat_col(r_eur2)
gbp_col2 = use_scale ? f_col(r_gbp2) : flat_col(r_gbp2)
cad_col2 = use_scale ? f_col(r_cad2) : flat_col(r_cad2)
aud_col2 = use_scale ? f_col(r_aud2) : flat_col(r_aud2)
jpy_col2 = use_scale ? f_col(r_jpy2) : flat_col(r_jpy2)


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, 4, 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 = alt2)
    table.cell(panel, 1, 0, "RSI " + timeframe.period, bgcolor =alt2)
    table.cell(panel, 2, 0, "RSI " + str.tostring(i_res) , bgcolor =alt2)
    // Column 1
    table.cell(panel, 0, 1, "USD", bgcolor = alt1, text_size=t_size)
    table.cell(panel, 0, 2, "EUR", bgcolor = alt2, text_size=t_size)
    table.cell(panel, 0, 3, "GBP", bgcolor = alt1, text_size=t_size)
    table.cell(panel, 0, 4, "CAD", bgcolor = alt2, text_size=t_size)
    table.cell(panel, 0, 5, "AUD", bgcolor = alt1, text_size=t_size)
    table.cell(panel, 0, 6, "JPY", bgcolor = alt2, text_size=t_size)
    // Column 2
    table.cell(panel, 1, 1, str.tostring(r_usd) + usd_hl, text_halign=text.align_right, bgcolor = usd_col, text_size=t_size)
    table.cell(panel, 1, 2, str.tostring(r_eur) + eur_hl, text_halign=text.align_right, bgcolor = eur_col, text_size=t_size)
    table.cell(panel, 1, 3, str.tostring(r_gbp) + gbp_hl, text_halign=text.align_right, bgcolor = gbp_col, text_size=t_size)
    table.cell(panel, 1, 4, str.tostring(r_cad) + cad_hl, text_halign=text.align_right, bgcolor = cad_col, text_size=t_size)
    table.cell(panel, 1, 5, str.tostring(r_aud) + aud_hl, text_halign=text.align_right, bgcolor = aud_col, text_size=t_size)
    table.cell(panel, 1, 6, str.tostring(r_jpy) + jpy_hl, text_halign=text.align_right, bgcolor = jpy_col, text_size=t_size)

    // Column 3
    table.cell(panel, 2, 1, str.tostring(r_usd2), bgcolor = usd_col2, text_size=t_size)
    table.cell(panel, 2, 2, str.tostring(r_eur2), bgcolor = eur_col2, text_size=t_size)
    table.cell(panel, 2, 3, str.tostring(r_gbp2), bgcolor = gbp_col2, text_size=t_size)
    table.cell(panel, 2, 4, str.tostring(r_cad2), bgcolor = cad_col2, text_size=t_size)
    table.cell(panel, 2, 5, str.tostring(r_aud2), bgcolor = aud_col2, text_size=t_size)
    table.cell(panel, 2, 6, str.tostring(r_jpy2), bgcolor = jpy_col2, text_size=t_size)
    