Nav Premium/Discount
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © calebsandfort

//@version=5
indicator("Nav Premium/Discount", format = format.percent, precision = 2)

underlying_symbol = input.symbol("COINBASE:ETHUSD", "Underlying")
underlying = request.security(underlying_symbol, 'D', close)

divisor = input.float(100.0, "Divisor", minval=0.0)
premium_cap = input.float(20.0, "Premium Cap")
discount_cap = input.float(-20.0, "Discount Cap")
premium_gradient_cap = input.float(15.0, "Premium Gradient Cap")
discount_gradient_cap = input.float(-15.0, "Discount Gradient Cap")

text_color = input.color(color.white, "Text Color")
premium_color = input.color(color.green, "Premium Color")
mid_color = input.color(color.yellow, "Mid Color")
discount_color = input.color(color.red, "Discount Color")

nav = underlying / divisor

delta = (close - nav) / nav * 100.0

capped_delta = delta > premium_cap ? premium_cap : delta < discount_cap ? discount_cap : delta

color_pos = color.from_gradient(capped_delta, 0.0, premium_gradient_cap, mid_color, premium_color)
color_neg = color.from_gradient(capped_delta, discount_gradient_cap, 0.0, discount_color, mid_color)
the_color = delta > 0.0 ? color_pos : color_neg

dod_delta = delta[1] - delta[0]
dod_delta_pct = (delta[0] - delta[1]) / delta[1] * 100.0

plotchar(delta, "Premium/Discount", "", location.top)
plot(capped_delta, style = plot.style_histogram, linewidth = 4, color = the_color, title = "Premium/Discount Capped")
plotchar(dod_delta, "Premium/Discount DoD Δ", "", location.top)
plotchar(dod_delta_pct, "Premium/Discount DoD Δ %", "", location.top)

var table infoTable = table.new(position.middle_right, 2, 4, frame_color=text_color, frame_width = 1, border_color=text_color, border_width = 1)

if barstate.islast
    table.cell(infoTable, 0, 0, "Underlying", bgcolor= delta > 0 ? premium_color : discount_color, text_color = text_color, text_halign = text.align_left)
    table.cell(infoTable, 1, 0, str.format("${0,number,#.##}", underlying[0]), bgcolor= delta > 0 ? premium_color : discount_color, text_color = text_color, text_halign = text.align_left)
    
    table.cell(infoTable, 0, 1, delta > 0 ? "Premium" : "Discount", bgcolor= delta > 0 ? premium_color : discount_color, text_color = text_color, text_halign = text.align_left)
    table.cell(infoTable, 1, 1, str.format("{0,number,#.##}%", delta), bgcolor= delta > 0 ? premium_color : discount_color, text_color = text_color, text_halign = text.align_left)
    
    table.cell(infoTable, 0, 2, "DoD Δ", bgcolor= delta > 0 ? premium_color : discount_color, text_color = text_color, text_halign = text.align_left)
    table.cell(infoTable, 1, 2, str.format("{0,number,#.##}", dod_delta), bgcolor= delta > 0 ? premium_color : discount_color, text_color = text_color, text_halign = text.align_left)
    
    table.cell(infoTable, 0, 3, "DoD Δ %", bgcolor= delta > 0 ? premium_color : discount_color, text_color = text_color, text_halign = text.align_left)
    table.cell(infoTable, 1, 3, str.format("{0,number,#.##}%", dod_delta_pct), bgcolor= delta > 0 ? premium_color : discount_color, text_color = text_color, text_halign = text.align_left)
