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

//@version=5
indicator("GBTC Premium")

// 1 GBTC = 0.00093207 BTC
// Under 10%  = Strong buy
// Under 25%  = Buy
// Under 75%  = Neutral
// Under 100% = Sell
// Over 100%  = Strong Sell

// Amount of BTC backing each share of GBTC
gbtcSharePerBtc = 0.00090193

// Percent premium levels define the colors
int discountUnderPercent = 0
int strongBuyUnderPercent = 10
int buyUnderPercent = 25
int neutralUnderPercent = 75
int sellUnderPercent = 100
int strongSellOverPercent = 100

// Colors defining the line color at different levels
color discountUnderColor = color.purple
color strongBuyUnderColor = color.blue
color buyUnderColor = color.green
color neutralUnderColor = color.orange
color sellUnderColor = color.red
color strongSellOverColor = color.maroon

// Get the GBTC ticker
gbtcTicker = request.security(syminfo.tickerid, "", close)

// Get the BTC ticker
btcTicker = ticker.new("BITSTAMP", "BTCUSD", syminfo.session)

// Get the close for the specified timeframe
timeframeClose = request.security(btcTicker, "", close)

// Get the factional size per share for GBTC
gbtcFractionalShare = timeframeClose * gbtcSharePerBtc

// Get the % deflection from GBTC
deflection = ((gbtcTicker - gbtcFractionalShare) / gbtcTicker) * 100

// Get a string representation of the deflection
deflectionStr = str.tostring(math.round(deflection))

// Determine the line color
color lineColor = if (deflection < discountUnderPercent)
    discountUnderColor
else if ((discountUnderPercent <= deflection) and (deflection < strongBuyUnderPercent))
    strongBuyUnderColor
else if ((strongBuyUnderPercent <= deflection) and (deflection < buyUnderPercent))
    buyUnderColor
else if ((buyUnderPercent <= deflection) and (deflection < neutralUnderPercent))
    neutralUnderColor
else if ((neutralUnderPercent <= deflection) and (deflection < sellUnderPercent))
    sellUnderColor
else if (strongSellOverPercent <= deflection)
    strongSellOverColor

// Paint a dotted line at 0 - Discount
hline(discountUnderPercent, title="DOSCOUNT!", color=color.red, linestyle=hline.style_dashed)

// Paint a dotted line at the Buy
hline(buyUnderPercent, title="Buy Under", color=color.gray, linestyle=hline.style_dashed)

var discountText = "Discount"
color discountColor = color.green
var premiumText = "Premium"
color premiumColor = color.red

// Label the current premium
var label myLabel = label.new(x=bar_index, y=high, textcolor=color.white, color=color.red)

// On the last bar, show the chart's bar count
if (barstate.islast)

    // Set the label text
    labelText = if (deflection < discountUnderPercent)
        discountText
    else 
        premiumText
    
    // Set the label color
    labelColor = if (deflection < discountUnderPercent)
        discountColor
    else 
        premiumColor


    // Set the label content
    label.set_text(id=myLabel, text=labelText + ": " + deflectionStr + "%")
    label.set_color(id=myLabel, color=labelColor)

    // Update the label's location
    label.set_x(id=myLabel, x=bar_index)
    label.set_y(id=myLabel, y=deflection)

    // Add a label at the buy level buyUnderPercent
    var label buyLabel = label.new(x=bar_index - 50, y=buyUnderPercent, textcolor=color.white, color=color.gray)
    label.set_text(id=buyLabel, text="BuyLine")
    label.set_style(id=buyLabel, style=label.style_label_up)
    
    // Add a label at the buy level buyUnderPercent
    var label discountLabel = label.new(x=bar_index - 100, y=discountUnderPercent, textcolor=color.white, color=color.purple)
    label.set_text(id=discountLabel, text="Discount")
    label.set_style(id=discountLabel, style=label.style_label_up)

// Plot
plot(deflection, color=lineColor)
