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

//@version=4
study("vol_premia", overlay = true)

len = 252

rv20(x) =>
    r = log(x / x[1])
    stdev(r, 20) * sqrt(252) * 100

es = security("ES1!", "D", close)
nq = security("NQ1!", "D", close)
rty = security("RTY1!", "D", close)
ym = security("YM1!", "D", close)
tlt = security("TLT", "D", close)
fxi = security("FXI", "D", close)
efa = security("EFA", "D", close)
eem = security("EEM", "D", close)
ewz = security("EWZ", "D", close)
uso = security("USO", "D", close)
gc = security("GC", "D", close)
gdx = security("GDX", "D", close)
slv = security("SLV", "D", close)
fxe = security("FXE", "D", close)
aapl = security("AAPL", "D", close)
amzn = security("AMZN", "D", close)
goog = security("GOOG", "D", close)
gs = security("GS", "D", close)
ibm = security("IBM", "D", close)

vix = security("VIX", "D", close)
vxn = security("VXN", "D", close)
rvx = security("RVX", "D", close)
vxd = security("VXD", "D", close)
vxtlt = security("VXTLT", "D", close)
vxfxi = security("VXFXI", "D", close)
vxefa = security("VXEFA", "D", close)
vxeem = security("VXEEM", "D", close)
vxewz = security("VXEWZ", "D", close)
ovx = security("OVX", "D", close)
gvz = security("GVZ", "D", close)
vxgdx = security("VXGDX", "D", close)
vxslv = security("VXSLV", "D", close)
evz = security("EVZ", "D", close)
vxapl = security("VXAPL", "D", close)
vxazn = security("VXAZN", "D", close)
vxgog = security("VXGOG", "D", close)
vxgs = security("VXGS", "D", close)
vxibm = security("VXIBM", "D", close)

es_rv = rv20(es)
nq_rv = rv20(nq)
rty_rv = rv20(rty)
ym_rv = rv20(ym)
tlt_rv = rv20(tlt)
fxi_rv = rv20(fxi)
efa_rv = rv20(efa)
eem_rv = rv20(eem)
ewz_rv = rv20(ewz)
uso_rv = rv20(uso)
gc_rv = rv20(gc)
gdx_rv = rv20(gdx)
slv_rv = rv20(slv)
fxe_rv = rv20(fxe)
aapl_rv = rv20(aapl)
amzn_rv = rv20(amzn)
goog_rv = rv20(goog)
gs_rv = rv20(gs)
ibm_rv = rv20(ibm)

es_rp = vix - es_rv
nq_rp = vxn - nq_rv
rty_rp = rvx - rty_rv
ym_rp = vxd - ym_rv
tlt_rp = vxtlt - tlt_rv
fxi_rp = vxfxi - fxi_rv
efa_rp = vxefa - efa_rv
eem_rp = vxeem - eem_rv
ewz_rp = vxewz - ewz_rv
uso_rp = ovx - uso_rv
gc_rp = gvz - gc_rv
gdx_rp = vxgdx - gdx_rv
slv_rp = vxslv - slv_rv
fxe_rp = evz - fxe_rv
aapl_rp = vxapl - aapl_rv
amzn_rp = vxazn - amzn_rv
goog_rp = vxgog - goog_rv
gs_rp = vxgs - gs_rv
ibm_rp = vxibm - ibm_rv

es_rp_pct = percentrank(es_rp, len)
nq_rp_pct = percentrank(nq_rp, len)
rty_rp_pct = percentrank(rty_rp, len)
ym_rp_pct = percentrank(ym_rp, len)
tlt_rp_pct = percentrank(tlt_rp, len)
fxi_rp_pct = percentrank(fxi_rp, len)
efa_rp_pct = percentrank(efa_rp, len)
eem_rp_pct = percentrank(eem_rp, len)
ewz_rp_pct = percentrank(ewz_rp, len)
uso_rp_pct = percentrank(uso_rp, len)
gc_rp_pct = percentrank(gc_rp, len)
gdx_rp_pct = percentrank(gdx_rp, len)
slv_rp_pct = percentrank(slv_rp, len)
fxe_rp_pct = percentrank(fxe_rp, len)
aapl_rp_pct = percentrank(aapl_rp, len)
amzn_rp_pct = percentrank(amzn_rp, len)
goog_rp_pct = percentrank(goog_rp, len)
gs_rp_pct = percentrank(gs_rp, len)
ibm_rp_pct = percentrank(ibm_rp, len)


if barstate.islast

    var ot = table.new(position.middle_right, 3, 20, bgcolor = color.white)
    
    fmt = "#.##"
    r = 0
    
    lbl_clr = color.new(color.blue, 70)
    idx_clr = color.new(color.green, 70)
    bnd_clr = color.new(color.orange, 70)
    for_clr = color.new(color.fuchsia, 70)
    oil_clr = color.new(color.black, 70)
    gld_clr = color.new(color.yellow, 70)
    slv_clr = color.new(color.gray, 70)
    crn_clr = color.new(color.navy, 70)
    stk_clr = color.new(color.aqua, 70)
    
    table.cell(ot, 0, r, "instrument", bgcolor = lbl_clr)
    table.cell(ot, 1, r, "risk premium", bgcolor = lbl_clr)
    table.cell(ot, 2, r, "percentile", bgcolor = lbl_clr)

    r := r + 1
    table.cell(ot, 0, r, "ES", bgcolor = idx_clr)
    table.cell(ot, 1, r, tostring(es_rp, fmt), bgcolor = idx_clr)
    table.cell(ot, 2, r, tostring(es_rp_pct, fmt), bgcolor = idx_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "NQ", bgcolor = idx_clr)
    table.cell(ot, 1, r, tostring(nq_rp, fmt), bgcolor = idx_clr)
    table.cell(ot, 2, r, tostring(nq_rp_pct, fmt), bgcolor = idx_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "RTY", bgcolor = idx_clr)
    table.cell(ot, 1, r, tostring(rty_rp, fmt), bgcolor = idx_clr)
    table.cell(ot, 2, r, tostring(rty_rp_pct, fmt), bgcolor = idx_clr)

    r := r + 1
    table.cell(ot, 0, r, "YM", bgcolor = idx_clr)
    table.cell(ot, 1, r, tostring(ym_rp, fmt), bgcolor = idx_clr)
    table.cell(ot, 2, r, tostring(ym_rp_pct, fmt), bgcolor = idx_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "TLT", bgcolor = bnd_clr)
    table.cell(ot, 1, r, tostring(tlt_rp, fmt), bgcolor = bnd_clr)
    table.cell(ot, 2, r, tostring(tlt_rp_pct, fmt), bgcolor = bnd_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "FXI", bgcolor = for_clr)
    table.cell(ot, 1, r, tostring(fxi_rp, fmt), bgcolor = for_clr)
    table.cell(ot, 2, r, tostring(fxi_rp_pct, fmt), bgcolor = for_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "EFA", bgcolor = for_clr)
    table.cell(ot, 1, r, tostring(efa_rp, fmt), bgcolor = for_clr)
    table.cell(ot, 2, r, tostring(efa_rp_pct, fmt), bgcolor = for_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "EEM", bgcolor = for_clr)
    table.cell(ot, 1, r, tostring(eem_rp, fmt), bgcolor = for_clr)
    table.cell(ot, 2, r, tostring(eem_rp_pct, fmt), bgcolor = for_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "EWZ", bgcolor = for_clr)
    table.cell(ot, 1, r, tostring(ewz_rp, fmt), bgcolor = for_clr)
    table.cell(ot, 2, r, tostring(ewz_rp_pct, fmt), bgcolor = for_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "USO", bgcolor = oil_clr)
    table.cell(ot, 1, r, tostring(uso_rp, fmt), bgcolor = oil_clr)
    table.cell(ot, 2, r, tostring(uso_rp_pct, fmt), bgcolor = oil_clr)

    r := r + 1
    table.cell(ot, 0, r, "GC", bgcolor = gld_clr)
    table.cell(ot, 1, r, tostring(gc_rp, fmt), bgcolor = gld_clr)
    table.cell(ot, 2, r, tostring(gc_rp_pct, fmt), bgcolor = gld_clr)

    r := r + 1
    table.cell(ot, 0, r, "GDX", bgcolor = gld_clr)
    table.cell(ot, 1, r, tostring(gdx_rp, fmt), bgcolor = gld_clr)
    table.cell(ot, 2, r, tostring(gdx_rp_pct, fmt), bgcolor = gld_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "SLV", bgcolor = slv_clr)
    table.cell(ot, 1, r, tostring(slv_rp, fmt), bgcolor = slv_clr)
    table.cell(ot, 2, r, tostring(slv_rp_pct, fmt), bgcolor = slv_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "FXE", bgcolor = crn_clr)
    table.cell(ot, 1, r, tostring(fxe_rp, fmt), bgcolor = crn_clr)
    table.cell(ot, 2, r, tostring(fxe_rp_pct, fmt), bgcolor = crn_clr)

    r := r + 1
    table.cell(ot, 0, r, "AAPL", bgcolor = stk_clr)
    table.cell(ot, 1, r, tostring(aapl_rp, fmt), bgcolor = stk_clr)
    table.cell(ot, 2, r, tostring(aapl_rp_pct, fmt), bgcolor = stk_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "AMZN", bgcolor = stk_clr)
    table.cell(ot, 1, r, tostring(amzn_rp, fmt), bgcolor = stk_clr)
    table.cell(ot, 2, r, tostring(amzn_rp_pct, fmt), bgcolor = stk_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "GOOG", bgcolor = stk_clr)
    table.cell(ot, 1, r, tostring(goog_rp, fmt), bgcolor = stk_clr)
    table.cell(ot, 2, r, tostring(goog_rp_pct, fmt), bgcolor = stk_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "GS", bgcolor = stk_clr)
    table.cell(ot, 1, r, tostring(gs_rp, fmt), bgcolor = stk_clr)
    table.cell(ot, 2, r, tostring(gs_rp_pct, fmt), bgcolor = stk_clr)
    
    r := r + 1
    table.cell(ot, 0, r, "IBM", bgcolor = stk_clr)
    table.cell(ot, 1, r, tostring(ibm_rp, fmt), bgcolor = stk_clr)
    table.cell(ot, 2, r, tostring(ibm_rp_pct, fmt), bgcolor = stk_clr)