BTC Pair Change %
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © backslash-f
//
// This script makes it easier to quickly check how the BTC pair of the current symbol is performing.
// It adds a "change percentage widget" to the top right of the chart with two rows, one for each timeframe.
// The default timeframes are: 1. current chart timeframe; 2. 1D timeframe. These can be customized.
//
// The calculation is performed as described here:
// https://www.tradingview.com/support/solutions/43000635852-how-are-the-most-popular-filters-calculated/
//
// In short, this script:
//  - Searches for the BTC pair of the current symbol
//  - Calculates the change % using the above described logic (links) for the two target timeframes
//  - Adds a change percentage "widget" to the top right of the chart with 2 rows, one for each timeframe
//
// Note to self: If the system detects that a required symbol doesn't exist or cannot be asked by any reason,
// then the script will not execute any subsequent logic. This makes it impossible to plot something when a
// BTC pair is non existent (e.g. "No BTC Pair"). Source: https://stackoverflow.com/a/60132079/584548

//@version=5
indicator("BTC Pair Change %", overlay=true)

/////////
// INPUT
/////////
var timeframeGroup      = "Timeframes"
timeframe1              = input.timeframe(title="Timeframe 1", defval="", group=timeframeGroup, tooltip = "The target timeframe for the first row. Default is the current timeframe.")
timeframe2              = input.timeframe(title="Timeframe 2", defval="1D", group=timeframeGroup, tooltip = "The target timeframe for the second row. Default is 1 day.")

var tableColorGroup     = "Table Colors"
tableBackgroundColor    = input.color(title="Background", defval=color.new(color.black, 100), group=tableColorGroup)
tableBorderColor        = input.color(title="Border", defval=color.new(color.black, 10), group=tableColorGroup)
tableStaticTextColor    = input.color(title="Text", defval=color.new(color.white, 20), group=tableColorGroup, tooltip = "Color for the table's static text, like header and cell titles. Does not apply to the change percentage text, which is dynamic between red, green and white.")

/////////////
// FUNCTIONS
/////////////
currentPrice(symbol, timeframe) =>
    request.security(symbol, timeframe, close)

previousPrice(symbol, timeframe) =>
    request.security(symbol, timeframe, close[1])

priceChangePercentage(btcPairPriceCurrent, btcPairPricePrevious) =>
    btcPairPriceChange = btcPairPriceCurrent - btcPairPricePrevious
    btcPairPriceChangePercent = (btcPairPriceChange / btcPairPricePrevious) * 100
    btcPairPriceChangePercentString = str.tostring(btcPairPriceChangePercent, '#.##') + "%"

priceChangeTextColor(btcPairPriceCurrent, btcPairPricePrevious) =>
    btcPairPriceChange = btcPairPriceCurrent - btcPairPricePrevious
    btcPairPriceChangePercent = (btcPairPriceChange / btcPairPricePrevious) * 100
    btcPairPriceChangePercent == 0 ? color.white : btcPairPriceChangePercent > 0 ? color.green : color.red

//////////////
// PROPERTIES
//////////////
tickerPrefix                                    = syminfo.basecurrency // E.g: ETH
btcPair                                         = tickerPrefix + "BTC" // E.g: ETHBTC

// Timeframe 1
btcPairPriceTimeframe1Current                   = currentPrice(btcPair, timeframe1)
btcPairPriceTimeframe1Previous                  = previousPrice(btcPair, timeframe1)
btcPairPriceTimeframe1ChangePercent             = priceChangePercentage(btcPairPriceTimeframe1Current, btcPairPriceTimeframe1Previous)
btcPairPriceTimeframe1ChangePercentTextColor    = priceChangeTextColor(btcPairPriceTimeframe1Current, btcPairPriceTimeframe1Previous)
timeframe1String                                = (timeframe1 == "") ? str.tostring(timeframe.period) : str.tostring(timeframe1)

// Timeframe 2
btcPairPriceTimeframe2Current                   = currentPrice(btcPair, timeframe2)
btcPairPriceTimeframe2Previous                  = previousPrice(btcPair, timeframe2)
btcPairPriceTimeframe2ChangePercent             = priceChangePercentage(btcPairPriceTimeframe2Current, btcPairPriceTimeframe2Previous)
btcPairPriceTimeframe2ChangePercentTextColor    = priceChangeTextColor(btcPairPriceTimeframe2Current, btcPairPriceTimeframe2Previous)

/////////
// TABLE
/////////
if barstate.islast
    table = table.new(position = position.top_right, columns = 2, rows = 4, bgcolor = tableBackgroundColor, border_width = 1, border_color = tableBorderColor)
    
    // Header
    table.cell(table, 0, 0, btcPair, text_color = tableStaticTextColor)
    table.merge_cells(table, start_column = 0, start_row = 0, end_row = 0, end_column = 1)

    // Timeframe column
    table.cell(table, 0, 1, "Timeframe", text_color = tableStaticTextColor)
    table.cell(table, 0, 2, timeframe1String, text_color = tableStaticTextColor)
    table.cell(table, 0, 3, str.tostring(timeframe2), text_color = tableStaticTextColor)

    // Change column
    table.cell(table, 1, 1, "Chg%", text_color = tableStaticTextColor)
    table.cell(table, 1, 2, btcPairPriceTimeframe1ChangePercent, text_color = btcPairPriceTimeframe1ChangePercentTextColor)
    table.cell(table, 1, 3, btcPairPriceTimeframe2ChangePercent, text_color = btcPairPriceTimeframe2ChangePercentTextColor)