Realized price for BTC, ETH, LTC
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kard1nal001

//@version=5
indicator("Realized Price by Baal Hadad", overlay = true)

emaLength = input.int(30, title="EMA Length")
bandsOn = input.bool(true, title="Realized Price Bands", tooltip = "Band levels: -70%, -50%, -30%, 200%, 300%, 400%, 500% from Realized Price")
signalsOn = input.bool(true, title="Signals")
// regLength = input.int(30)

// === INPUT BACKTEST RANGE ===
// startDate = input.time(defval = timestamp("05 Jan 15 00:00 +0000"), title = "From")
startDate = timestamp("05 Jan 15 00:00 +0000")


// === FUNCTION EXAMPLE ===
rightDate() => time >= startDate // create date function "within window of time"

baseCurrency = syminfo.basecurrency

request_supply_intotheblock = request.security('INTOTHEBLOCK:' + baseCurrency + '_CIRCULATINGSUPPLY',"D", close, ignore_invalid_symbol = true)
request_supply_glassnode = request.security('GLASSNODE:' + baseCurrency + '_SUPPLY',"D", close, ignore_invalid_symbol = true)
supply  = na(request_supply_intotheblock)?request_supply_glassnode:request_supply_intotheblock

qu_mvrv = 'INTOTHEBLOCK:' + baseCurrency + '_MVRV'
float mvrv  = request.security(qu_mvrv,"D", close)

qu_marketCap = 'CRYPTOCAP:' + baseCurrency
float marketCap  = request.security(qu_marketCap,"D", close)

float realizedCap = 1 / mvrv * marketCap

realizedPrice = realizedCap / supply
realizedPriceEMA = ta.ema(realizedCap / supply, emaLength)

lb30 = realizedPriceEMA * 0.7
lb50 = realizedPriceEMA * 0.5
lb70 = realizedPriceEMA * 0.3

ub2 = realizedPriceEMA * 2
ub3 = realizedPriceEMA * 3
ub4 = realizedPriceEMA * 4
ub5 = realizedPriceEMA * 5

rightDate = rightDate()

buySingal1 = ta.crossunder(lb30, close)
buySingal2 = ta.crossunder(lb50, close)
buySingal3 = ta.crossunder(lb70, close)

sellSingal1 = ta.crossover(ub2, close)
sellSingal2 = ta.crossover(ub3, close)
sellSingal3 = ta.crossover(ub4, close)
sellSingal4 = ta.crossover(ub5, close)

bgcolor(buySingal3 and signalsOn?color.rgb(0, 255, 0, 50):buySingal2 and signalsOn?color.rgb(0, 255, 0, 70):buySingal1 and signalsOn?color.rgb(0, 100, 0, 70):na)
bgcolor(sellSingal4 and signalsOn?color.rgb(255, 0, 0, 50):sellSingal3 and signalsOn?color.rgb(255, 0, 0, 50):sellSingal2 and signalsOn?color.rgb(255, 0, 0, 70):sellSingal1 and signalsOn?color.rgb(100, 0, 0, 70):na)

plot(rightDate?realizedPriceEMA:na, color = color.orange, title = "RealizedPrice EMA 30", linewidth = 2)

plot(rightDate and bandsOn?lb30:na, color = color.rgb(169, 255, 171, 50), title = "Falling 30%", linewidth = 2)
plot(rightDate and bandsOn?lb50:na, color = color.rgb(169, 255, 171, 50), title = "Falling 50%", linewidth = 2)
plot(rightDate and bandsOn?lb70:na, color = color.rgb(169, 255, 171, 50), title = "Falling 70%", linewidth = 2)

plot(rightDate and bandsOn?ub2:na, color = color.rgb(255, 0, 0, 50), title = "Growth x2", linewidth = 2)
plot(rightDate and bandsOn?ub3:na, color = color.rgb(255, 0, 0, 50), title = "Growth x3", linewidth = 2)
plot(rightDate and bandsOn?ub4:na, color = color.rgb(255, 0, 0, 50), title = "Growth x4", linewidth = 2)
plot(rightDate and bandsOn?ub5:na, color = color.rgb(255, 0, 0, 50), title = "Growth x5", linewidth = 2)