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

//@version=5

indicator(title='REALIZED PRICE', overlay=true)

coin = input.string("BTC", "Coin", ["BTC", "LTC", "ETH"])
dev = input.bool(false, "Plot deviations bands")
supply = request.security("GLASSNODE:BTC_SUPPLY", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
sopr = request.security("GLASSNODE:BTC_SOPR", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
vol = request.security("GLASSNODE:BTC_UTXOVALUETOTALSPENTUSD", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
LTC_supply = request.security("GLASSNODE:LTC_SUPPLY", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
LTC_sopr = request.security("GLASSNODE:LTC_SOPR", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
LTC_vol = request.security("GLASSNODE:LTC_UTXOVALUETOTALSPENTUSD", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
ETH_supply = request.security("GLASSNODE:ETH_SUPPLY", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
ETH_sopr = request.security("GLASSNODE:ETH_SOPR", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
ETH_vol = request.security("GLASSNODE:ETH_TOTALVOLUMEUSD", timeframe.period, nz(close, open), lookahead = barmerge.lookahead_off)
if coin=="LTC"
    supply:=LTC_supply
    sopr:=LTC_sopr
    vol:=LTC_vol
if coin=="ETH"
    supply:=ETH_supply
    sopr:=ETH_sopr
    vol:=ETH_vol
var float realized_cap = na
realized_cap := nz(realized_cap[1], hl2) + vol * (1 - 1 / sopr) + (supply - nz(supply[1])) * nz(hl2, open)
realized_price = realized_cap / supply
delta = ta.cum(realized_price) / (bar_index + 1)
delta_price_m = realized_price - delta
delta_price_p = realized_price + delta
plot(realized_price, "Realized Price", color.orange, 2)
a = plot(dev ? delta_price_m : na, "RP - deviaton", color = na, display = display.none)
b = plot(dev ? delta_price_p : na, "RP + deviaton", color = na, display = display.none)
fill(a, b, color = color.rgb(255, 204, 110, 56), title = "RP deviaton band")