BETA Benchmark - Tables!
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Mishari_Algos

//@version=5
indicator(title='BETA Benchmark - Tables!', overlay=false, precision = 2)

smooth = input.int(1, title="Smooth")

period1 = input.int(30, title="First Period")
period2 = input.int(60, title="Second Period")
period3 = input.int(90, title="Third Period")
period4 = input.int(120, title="Fourth Period")
usePeriod5 = input.bool(false, title="Use Fifth Period?")
period5 = usePeriod5 ? input.int(180, title="Fifth Period") : na

return_percent(src) =>
    ta.change(src) * 100 / src[1]

instrument = ta.ema(close, smooth)
benchmark = request.security(input.symbol('CRYPTOCAP:TOTAL', title="Benchmark"), timeframe.period, ta.ema(close, smooth))
inst_return = return_percent(instrument)
bench_return = return_percent(benchmark)

// Function to calculate beta for given length
calculate_beta(_length) =>
    avg_inst_return = ta.sma(inst_return, _length)
    avg_bench_return = ta.sma(bench_return, _length)
    sum = 0.0
    varianceSum = 0.0
    for idx = 0 to _length - 1
        inst_variance = inst_return[idx] - avg_inst_return
        bench_variance = bench_return[idx] - avg_bench_return
        sum := sum + inst_variance * bench_variance
        varianceSum := varianceSum + bench_variance * bench_variance
    covariance = sum / (_length - 1)
    _beta = covariance / (varianceSum / (_length - 1))
    _beta

beta1 = calculate_beta(period1)
beta2 = calculate_beta(period2)
beta3 = calculate_beta(period3)
beta4 = calculate_beta(period4)
beta5 = usePeriod5 ? calculate_beta(period5) : na

// Calculate average beta
average_beta = usePeriod5 ? (beta1 + beta2 + beta3 + beta4 + beta5) / 5 : (beta1 + beta2 + beta3 + beta4) / 4

// Plot the average beta
plot(average_beta, color=color.blue, title="Average Beta")

// Display betas in a table
var table betas = table.new(position.top_right, usePeriod5 ? 6 : 5, 2)
table.cell(betas, 0, 0, "Period", bgcolor=color.gray, text_color=color.white)
table.cell(betas, 0, 1, "Beta Value", bgcolor=color.gray, text_color=color.white)
table.cell(betas, 1, 0, str.tostring(period1) + " Days", bgcolor=color.silver, text_color=color.black)
table.cell(betas, 1, 1, str.tostring(math.round(beta1,2)), bgcolor=color.silver, text_color=color.black)
table.cell(betas, 2, 0, str.tostring(period2) + " Days", bgcolor=color.silver, text_color=color.black)
table.cell(betas, 2, 1, str.tostring(math.round(beta2,2)), bgcolor=color.silver, text_color=color.black)
table.cell(betas, 3, 0, str.tostring(period3) + " Days", bgcolor=color.silver, text_color=color.black)
table.cell(betas, 3, 1, str.tostring(math.round(beta3,2)), bgcolor=color.silver, text_color=color.black)
table.cell(betas, 4, 0, str.tostring(period4) + " Days", bgcolor=color.silver, text_color=color.black)
table.cell(betas, 4, 1, str.tostring(math.round(beta4,2)), bgcolor=color.silver, text_color=color.black)
if usePeriod5
    table.cell(betas, 5, 0, str.tostring(period5) + " Days", bgcolor=color.silver, text_color=color.black)
    table.cell(betas, 5, 1, str.tostring(math.round(beta5,2)), bgcolor=color.silver, text_color=color.black)
