Asset Performance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hisatomi

//@version=5
indicator("Asset Performance", overlay = false, format = format.percent, explicit_plot_zorder = true)
bars = input.int(200, "Bars", minval = 0, step = 100)

GROUP_NAME = "Assets for comparison"
a1_color = input.color(color.new(#0077FF, 20), "1.", inline = "1", group = GROUP_NAME)
a2_color = input.color(color.new(#FF0000, 20), "2.", inline = "2", group = GROUP_NAME)
a3_color = input.color(color.new(#00BB00, 20), "3.", inline = "3", group = GROUP_NAME)
a4_color = input.color(color.new(#33BBFF, 20), "4.", inline = "4", group = GROUP_NAME)
a5_color = input.color(color.new(#FF77FF, 20), "5.", inline = "5", group = GROUP_NAME)
a6_color = input.color(color.new(#FFBB33, 20), "6.", inline = "6", group = GROUP_NAME)
a1_input = input.string("", "", ["", "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", "BTC", "ETH"], inline = "1", group = GROUP_NAME)
a2_input = input.string("", "", ["", "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", "BTC", "ETH"], inline = "2", group = GROUP_NAME)
a3_input = input.string("", "", ["", "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", "BTC", "ETH"], inline = "3", group = GROUP_NAME)
a4_input = input.symbol("", "", inline = "4", group = GROUP_NAME)
a5_input = input.symbol("", "", inline = "5", group = GROUP_NAME)
a6_input = input.symbol("", "", inline = "6", group = GROUP_NAME)

currency_symbol(currency_name) =>
    symbol = ""
    switch currency_name
        "USD" => symbol := "TVC:DXY"
        "EUR" => symbol := "FX_IDC:EURUSD"
        "JPY" => symbol := "FX_IDC:JPYUSD"
        "GBP" => symbol := "FX_IDC:GBPUSD"
        "AUD" => symbol := "FX_IDC:AUDUSD"
        "CAD" => symbol := "FX_IDC:CADUSD"
        "CHF" => symbol := "FX_IDC:CHFUSD"
        "NZD" => symbol := "FX_IDC:NZDUSD"
        "BTC" => symbol := "CRYPTO:BTCUSD"
        "ETH" => symbol := "CRYPTO:ETHUSD"
    symbol

modify_symbol(symbol) =>
    id = ticker.modify(symbol, session = session.extended)
    if str.contains(syminfo.tickerid, "dividends")
        id := ticker.modify(id, adjustment = adjustment.dividends)
    id

a1_symbol = currency_symbol(a1_input)
a2_symbol = currency_symbol(a2_input)
a3_symbol = currency_symbol(a3_input)
a4_symbol = modify_symbol(a4_input)
a5_symbol = modify_symbol(a5_input)
a6_symbol = modify_symbol(a6_input)

symbol_value(symbol) => syminfo.ticker(symbol) == "DXY" ? 1.0 : close

dxy = request.security("TVC:DXY", "", close)
a1usd = request.security(a1_symbol, "", symbol_value(a1_symbol), ignore_invalid_symbol = true)
a2usd = request.security(a2_symbol, "", symbol_value(a2_symbol), ignore_invalid_symbol = true)
a3usd = request.security(a3_symbol, "", symbol_value(a3_symbol), ignore_invalid_symbol = true)
a4usd = request.security(a4_symbol, "", symbol_value(a4_symbol), ignore_invalid_symbol = true, currency = currency.USD)
a5usd = request.security(a5_symbol, "", symbol_value(a5_symbol), ignore_invalid_symbol = true, currency = currency.USD)
a6usd = request.security(a6_symbol, "", symbol_value(a6_symbol), ignore_invalid_symbol = true, currency = currency.USD)
csusd = request.security(syminfo.tickerid, "",
  syminfo.basecurrency == "USD" ? 1.0 : symbol_value(syminfo.tickerid),
  currency = syminfo.basecurrency == "USD" ? currency.NONE : currency.USD)

var float dxy_start = na
var float a1usd_start = na
var float a2usd_start = na
var float a3usd_start = na
var float a4usd_start = na
var float a5usd_start = na
var float a6usd_start = na
var float csusd_start = na

if bar_index == last_bar_index - bars
    line.new(bar_index, 0.0001, bar_index, -0.0001, extend = extend.both, color = color.gray, style = line.style_dotted)
    dxy_start := dxy
    a1usd_start := a1usd
    a2usd_start := a2usd
    a3usd_start := a3usd
    a4usd_start := a4usd
    a5usd_start := a5usd
    a6usd_start := a6usd
    csusd_start := csusd

usd_change = dxy / dxy_start
a1 = a1_input == "" ? na : 100 * (usd_change * a1usd / a1usd_start - 1)
a2 = a2_input == "" ? na : 100 * (usd_change * a2usd / a2usd_start - 1)
a3 = a3_input == "" ? na : 100 * (usd_change * a3usd / a3usd_start - 1)
a4 = a4_input == "" ? na : 100 * (usd_change * a4usd / a4usd_start - 1)
a5 = a5_input == "" ? na : 100 * (usd_change * a5usd / a5usd_start - 1)
a6 = a6_input == "" ? na : 100 * (usd_change * a6usd / a6usd_start - 1)
cs = 100 * (usd_change * csusd / csusd_start - 1)

hline(0, "Zero Line", color.new(color.gray, 50))
plot(a1, "Asset 1", a1_color, editable = false)
plot(a2, "Asset 2", a2_color, editable = false)
plot(a3, "Asset 3", a3_color, editable = false)
plot(a4, "Asset 4", a4_color, editable = false)
plot(a5, "Asset 5", a5_color, editable = false)
plot(a6, "Asset 6", a6_color, editable = false)
plot(cs, "Symbol", color.black)

label_copy(template, labeltext, textcolor) =>
    id = label.copy(template)
    label.set_text(id, labeltext)
    label.set_textcolor(id, textcolor)
    id

var a1_label = label.new(na, na, a1_input, color = #00000000, style = label.style_label_left, textcolor = a1_color, size = size.small)
var a2_label = label_copy(a1_label, a2_input, a2_color)
var a3_label = label_copy(a1_label, a3_input, a3_color)
var a4_label = label_copy(a1_label, syminfo.ticker(a4_symbol), a4_color)
var a5_label = label_copy(a1_label, syminfo.ticker(a5_symbol), a5_color)
var a6_label = label_copy(a1_label, syminfo.ticker(a6_symbol), a6_color)

if barstate.islast
    label.set_xy(a1_label, bar_index, a1)
    label.set_xy(a2_label, bar_index, a2)
    label.set_xy(a3_label, bar_index, a3)
    label.set_xy(a4_label, bar_index, a4)
    label.set_xy(a5_label, bar_index, a5)
    label.set_xy(a6_label, bar_index, a6)
