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

//@version=5
indicator("energies_correlation_zscore", overlay = true)

timeframe   = input.timeframe("30")
window      = input(48, "window")
type        = input.string(title = "type", options = [ "zscore", "correlation" ], defval = "zscore")

ho = request.security("HO1!", timeframe, close)
rb = request.security("RB1!", timeframe, close)
cl = request.security("CL1!", timeframe, close)
ng = request.security("NG1!", timeframe, close)

var ho_rb_arr = array.new_float()
var ho_cl_arr = array.new_float()
var ho_ng_arr = array.new_float()

var rb_ho_arr = array.new_float()
var rb_cl_arr = array.new_float()
var rb_ng_arr = array.new_float()

var cl_ho_arr = array.new_float()
var cl_rb_arr = array.new_float()
var cl_ng_arr = array.new_float()

var ng_ho_arr = array.new_float()
var ng_rb_arr = array.new_float()
var ng_cl_arr = array.new_float()

ho_rb = ta.correlation(ho, rb, window)
ho_cl = ta.correlation(ho, cl, window)
ho_ng = ta.correlation(ho, ng, window)

array.push(ho_rb_arr, ho_rb)
array.push(ho_cl_arr, ho_cl)
array.push(ho_ng_arr, ho_ng)

rb_ho = ta.correlation(rb, ho, window)
rb_cl = ta.correlation(rb, cl, window)
rb_ng = ta.correlation(rb, ng, window)

array.push(rb_ho_arr, rb_ho)
array.push(rb_cl_arr, rb_cl)
array.push(rb_ng_arr, rb_ng)

cl_ho = ta.correlation(cl, ho, window)
cl_rb = ta.correlation(cl, rb, window)
cl_ng = ta.correlation(cl, ng, window)

array.push(cl_ho_arr, cl_ho)
array.push(cl_rb_arr, cl_rb)
array.push(cl_ng_arr, cl_ng)

ng_ho = ta.correlation(ng, ho, window)
ng_rb = ta.correlation(ng, rb, window)
ng_cl = ta.correlation(ng, cl, window)

array.push(ng_ho_arr, ng_ho)
array.push(ng_rb_arr, ng_rb)
array.push(ng_cl_arr, ng_cl)

if barstate.islast
    
    fmt = "#.##"
    
    t = table.new(position.bottom_right, 5, 5)
    
    table.cell(t, 0, 0, type)
    
    table.cell(t, 0, 1, "ho")
    table.cell(t, 0, 2, "rb")
    table.cell(t, 0, 3, "ng")
    table.cell(t, 0, 4, "cl")
    
    table.cell(t, 1, 0, "ho")
    table.cell(t, 2, 0, "rb")
    table.cell(t, 3, 0, "ng")
    table.cell(t, 4, 0, "cl")
    
    
    table.cell(t, 1, 1, "-")
    table.cell(t, 2, 2, "-")
    table.cell(t, 3, 3, "-")
    table.cell(t, 4, 4, "-")
    
    if type == "zscore"
    
        table.cell(t, 1, 2, str.tostring((ho_rb - array.avg(ho_rb_arr)) / array.stdev(ho_rb_arr), fmt))
        table.cell(t, 1, 3, str.tostring((ho_cl - array.avg(ho_cl_arr)) / array.stdev(ho_cl_arr), fmt))
        table.cell(t, 1, 4, str.tostring((ho_ng - array.avg(ho_ng_arr)) / array.stdev(ho_ng_arr), fmt))
        
        table.cell(t, 2, 1, str.tostring((rb_ho - array.avg(rb_ho_arr)) / array.stdev(rb_ho_arr), fmt))
        table.cell(t, 2, 3, str.tostring((rb_cl - array.avg(rb_cl_arr)) / array.stdev(rb_cl_arr), fmt))
        table.cell(t, 2, 4, str.tostring((rb_ng - array.avg(rb_ng_arr)) / array.stdev(rb_ng_arr), fmt))
        
        table.cell(t, 3, 1, str.tostring((cl_ho - array.avg(cl_ho_arr)) / array.stdev(cl_ho_arr), fmt))
        table.cell(t, 3, 2, str.tostring((cl_rb - array.avg(cl_rb_arr)) / array.stdev(cl_rb_arr), fmt))
        table.cell(t, 3, 4, str.tostring((cl_ng - array.avg(cl_ng_arr)) / array.stdev(cl_ng_arr), fmt))
    
        table.cell(t, 4, 1, str.tostring((ng_ho - array.avg(ng_ho_arr)) / array.stdev(ng_ho_arr), fmt))
        table.cell(t, 4, 2, str.tostring((ng_rb - array.avg(ng_rb_arr)) / array.stdev(ng_rb_arr), fmt))
        table.cell(t, 4, 3, str.tostring((ng_cl - array.avg(ng_cl_arr)) / array.stdev(ng_cl_arr), fmt))

    else
    
        table.cell(t, 1, 2, str.tostring(ho_rb, fmt))
        table.cell(t, 1, 3, str.tostring(ho_cl, fmt))
        table.cell(t, 1, 4, str.tostring(ho_ng, fmt))
        
        table.cell(t, 2, 1, str.tostring(rb_ho, fmt))
        table.cell(t, 2, 3, str.tostring(rb_cl, fmt))
        table.cell(t, 2, 4, str.tostring(rb_ng, fmt))
        
        table.cell(t, 3, 1, str.tostring(cl_ho, fmt))
        table.cell(t, 3, 2, str.tostring(cl_rb, fmt))
        table.cell(t, 3, 4, str.tostring(cl_ng, fmt))
    
        table.cell(t, 4, 1, str.tostring(ng_ho, fmt))
        table.cell(t, 4, 2, str.tostring(ng_rb, fmt))
        table.cell(t, 4, 3, str.tostring(ng_cl, fmt))
    