Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.7 kB
5
Indexable
length_Beta_TOTAL = input(600,"Beta measurement length on Benchmark #1",tooltip = "The longer the period the better",group = "Lengths")

length_Alpha_TOTAL = input(600,"Alpha measurement length on Benchmark #1",tooltip = "The longer the period the better",group = "Lengths")

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

benchmark_TOTAL = request.security('CRYPTOCAP:TOTAL', 'D', barstate.isconfirmed ? close:close[1])

Beta_Alpha_calculation(name_of_coins) =>
    instrument = request.security(name_of_coins, 'D', barstate.isconfirmed ? close:close[1])
    inst_return = return_percent(instrument)
    bench_return = return_percent(benchmark_TOTAL)
    avg_inst_return = ta.sma(inst_return, length_Beta_TOTAL)
    avg_bench_return = ta.sma(bench_return, length_Beta_TOTAL)
    //function would be better but I leave this like that for more clarity
    sum = 0.0
    for idx = length_Beta_TOTAL to 0 by 1
        inst_variance = inst_return[idx] - avg_inst_return
        bench_variance = bench_return[idx] - avg_bench_return
        sum += inst_variance * bench_variance
        sum
    covariance = sum / (length_Beta_TOTAL - 1)
    beta_TOTAL = covariance / ta.variance(bench_return, length_Beta_TOTAL)

    float daily_return = instrument / instrument[1] - 1
    float daily_base_return = benchmark_TOTAL / benchmark_TOTAL[1] - 1
    returns_array = array.new_float(0)
    returns_base_array = array.new_float(0)
    for i = 0 to length_Alpha_TOTAL
        array.push(returns_array, daily_return[i])
        array.push(returns_base_array, daily_base_return[i])

    alpha_TOTAL = array.sum(returns_array) - array.sum(returns_base_array) * beta_TOTAL

    [beta_TOTAL,instrument,alpha_TOTAL]
Leave a Comment