intraday_bonds
// 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("intraday_bonds", overlay = true)

stdevs      = input(2.0, "stdevs")
half_hour   = 6.0
one_hour    = 2.0 * half_hour
one_day     = 23.0 * one_hour
one_year    = 252.0 * one_day

zt_c = request.security("ZT1!", "5", close)
zf_c = request.security("ZF1!", "5", close)
zn_c = request.security("ZN1!", "5", close)
zb_c = request.security("ZB1!", "5", close)

zt_h = request.security("ZT1!", "5", high)
zf_h = request.security("ZF1!", "5", high)
zn_h = request.security("ZN1!", "5", high)
zb_h = request.security("ZB1!", "5", high)

zt_l = request.security("ZT1!", "5", low)
zf_l = request.security("ZF1!", "5", low)
zn_l = request.security("ZN1!", "5", low)
zb_l = request.security("ZB1!", "5", low)

var float[] zt_rvs   = array.new_float()
var float[] zf_rvs   = array.new_float()
var float[] zn_rvs   = array.new_float()
var float[] zb_rvs   = array.new_float()

rv(sym) => 
    squared_returns     = math.pow(math.log(sym / sym[1]), 2.0)
    smoothed_returns    = ta.ema(squared_returns, int(one_hour))
    math.sqrt(smoothed_returns * one_year)

hour_ticks(rv, c, h, l) =>
    rv_hour     = c * stdevs * rv * math.sqrt(one_hour / one_year)
    atr_hour    = ta.highest(h, int(one_hour)) - ta.lowest(l, int(one_hour))
    
    [ rv_hour, atr_hour ]

zt_rv = rv(zt_c)
zf_rv = rv(zf_c)
zn_rv = rv(zn_c)
zb_rv = rv(zb_c)

array.push(zt_rvs, zt_rv)
array.push(zf_rvs, zf_rv)
array.push(zn_rvs, zn_rv)
array.push(zb_rvs, zb_rv)

[ zt_rv_hour, zt_atr_hour ] = hour_ticks(zt_rv, zt_c, zt_h, zt_l)
[ zf_rv_hour, zf_atr_hour ] = hour_ticks(zf_rv, zf_c, zf_h, zf_l)
[ zn_rv_hour, zn_atr_hour ] = hour_ticks(zn_rv, zn_c, zn_h, zn_l)
[ zb_rv_hour, zb_atr_hour ] = hour_ticks(zb_rv, zb_c, zb_h, zb_l)

zt_d = zt_c - zt_c[1]
zf_d = zf_c - zf_c[1]
zn_d = zn_c - zn_c[1]
zb_d = zb_c - zb_c[1]

zt_zf = ta.correlation(zt_d, zf_d, int(one_day))
zt_zn = ta.correlation(zt_d, zn_d, int(one_day))
zt_zb = ta.correlation(zt_d, zb_d, int(one_day))

zf_zt = ta.correlation(zf_d, zt_d, int(one_day))
zf_zn = ta.correlation(zf_d, zn_d, int(one_day))
zf_zb = ta.correlation(zf_d, zb_d, int(one_day))

zn_zt = ta.correlation(zn_d, zt_d, int(one_day))
zn_zf = ta.correlation(zn_d, zf_d, int(one_day))
zn_zb = ta.correlation(zn_d, zb_d, int(one_day))

zb_zt = ta.correlation(zb_d, zt_d, int(one_day))
zb_zf = ta.correlation(zb_d, zf_d, int(one_day))
zb_zn = ta.correlation(zb_d, zn_d, int(one_day))


if barstate.islast

    zt_rv_z = (zt_rv - array.avg(zt_rvs)) / array.stdev(zt_rvs)
    zf_rv_z = (zf_rv - array.avg(zf_rvs)) / array.stdev(zf_rvs)
    zn_rv_z = (zn_rv - array.avg(zn_rvs)) / array.stdev(zn_rvs)
    zb_rv_z = (zb_rv - array.avg(zb_rvs)) / array.stdev(zb_rvs)
    
    fmt = "#.##"
    
    t = table.new(position.top_right, 5, 11)
    
    table.cell(t, 0, 0)
    
    table.cell(t, 0, 1, "zt", text_color = color.white)
    table.cell(t, 0, 2, "zf", text_color = color.white)
    table.cell(t, 0, 3, "zn", text_color = color.white)
    table.cell(t, 0, 4, "zb", text_color = color.white)
    
    table.cell(t, 1, 0, "rv", text_color = color.white)
    table.cell(t, 2, 0, "z(rv) ", text_color = color.white)
    table.cell(t, 3, 0, "vol rng", text_color = color.white)
    table.cell(t, 4, 0, "atr rng", text_color = color.white)
    
    table.cell(t, 1, 1, str.tostring(zt_rv * 100, fmt) + "%", text_color = color.white)
    table.cell(t, 1, 2, str.tostring(zf_rv * 100, fmt) + "%", text_color = color.white)
    table.cell(t, 1, 3, str.tostring(zn_rv * 100, fmt) + "%", text_color = color.white)
    table.cell(t, 1, 4, str.tostring(zb_rv * 100, fmt) + "%", text_color = color.white)
    
    table.cell(t, 2, 1, str.tostring(zt_rv_z, fmt), text_color = color.white)
    table.cell(t, 2, 2, str.tostring(zf_rv_z, fmt), text_color = color.white)
    table.cell(t, 2, 3, str.tostring(zn_rv_z, fmt), text_color = color.white)
    table.cell(t, 2, 4, str.tostring(zb_rv_z, fmt), text_color = color.white)
    
    table.cell(t, 3, 1, str.tostring(math.ceil(zt_rv_hour * 256.0)), text_color = color.white)
    table.cell(t, 3, 2, str.tostring(math.ceil(zf_rv_hour * 128.0)), text_color = color.white)
    table.cell(t, 3, 3, str.tostring(math.ceil(zn_rv_hour * 64.0)), text_color = color.white)
    table.cell(t, 3, 4, str.tostring(math.ceil(zb_rv_hour * 32.0)), text_color = color.white)
    
    table.cell(t, 4, 1, str.tostring(math.ceil(zt_atr_hour * 256.0)), text_color = color.white)
    table.cell(t, 4, 2, str.tostring(math.ceil(zf_atr_hour * 128.0)), text_color = color.white)
    table.cell(t, 4, 3, str.tostring(math.ceil(zn_atr_hour * 64.0)), text_color = color.white)
    table.cell(t, 4, 4, str.tostring(math.ceil(zb_atr_hour * 32.0)), text_color = color.white)
    
    table.cell(t, 0, 5)
    table.cell(t, 1, 5)
    table.cell(t, 2, 5)
    table.cell(t, 3, 5)
    table.cell(t, 4, 5)
    
    table.cell(t, 0, 6)
    
    table.cell(t, 1, 6, "zt", text_color = color.white)
    table.cell(t, 2, 6, "zf", text_color = color.white)
    table.cell(t, 3, 6, "zn", text_color = color.white)
    table.cell(t, 4, 6, "zb", text_color = color.white)
    
    table.cell(t, 0, 7, "zt", text_color = color.white)
    table.cell(t, 0, 8, "zf", text_color = color.white)
    table.cell(t, 0, 9, "zn", text_color = color.white)
    table.cell(t, 0, 10, "zb", text_color = color.white)
    
    table.cell(t, 1, 7, "1", text_color = color.white)
    table.cell(t, 1, 8, str.tostring(zt_zf, fmt), text_color = color.white)
    table.cell(t, 1, 9, str.tostring(zt_zn, fmt), text_color = color.white)
    table.cell(t, 1, 10, str.tostring(zt_zb, fmt), text_color = color.white)
    
    table.cell(t, 2, 7, str.tostring(zf_zt, fmt), text_color = color.white)
    table.cell(t, 2, 8, "1", text_color = color.white)
    table.cell(t, 2, 9, str.tostring(zf_zn, fmt), text_color = color.white)
    table.cell(t, 2, 10, str.tostring(zf_zb, fmt), text_color = color.white)
    
    table.cell(t, 3, 7, str.tostring(zn_zt, fmt), text_color = color.white)
    table.cell(t, 3, 8, str.tostring(zn_zf, fmt), text_color = color.white)
    table.cell(t, 3, 9, "1", text_color = color.white)
    table.cell(t, 3, 10, str.tostring(zn_zb, fmt), text_color = color.white)
    
    table.cell(t, 4, 7, str.tostring(zb_zt, fmt), text_color = color.white)
    table.cell(t, 4, 8, str.tostring(zb_zf, fmt), text_color = color.white)
    table.cell(t, 4, 9, str.tostring(zb_zn, fmt), text_color = color.white)
    table.cell(t, 4, 10, "1", text_color = color.white)