BTC Supply in Profits and Losses (BTCSPL) [AlgoAlpha]
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Algoalpha X © Sushiboi77

//@version=5
indicator("BTC Supply in Profits and Losses (BTCSPL) [AlgoAlpha]", precision = 2)
ad = input.bool(title = 'Alpha Decay Adjusted', defval = true)
ma = input.bool(title = 'Show Moving Average', defval = true)
sh = input.bool(title = 'Show Short-Term Trend', defval = false)
ch = input.bool(title = 'Show Rolling Change', defval = false)
va = input.bool(title = 'Show BTCSPL Value Score (-1 to 1)', defval = false)
malen = input.int(title = 'Moving Average Length', defval = 200, minval = 1)
len = input.int(title = 'Rolling Change Length', defval = 90, minval = 1)
valuerange = input.int(title = 'Value Range',defval = 1, minval = 1)
aroonlength = input.int(title = 'AROON Length',defval = 20, minval = 1)

bullcolor = #00ffbb
bearcolor = #ff1100

p = request.security('BTC_PROFITADDRESSESPERCENTAGE', 'D', close)
l = request.security('BTC_LOSSESADDRESSESPERCENTAGE', 'D', close)
//plot(p, color = color.orange)
//plot(l, color = color.blue)
//c = ((bar_index/(6*365))*0.18)

lowerbound = ta.lowest(p-l, 140*7)
decayadjusted = ((p-l) - lowerbound)/(1 - lowerbound)
lowerbound := request.security(syminfo.tickerid, "1D", lowerbound)
decayadjusted := request.security(syminfo.tickerid, "1D", decayadjusted)

upper = 100 * (ta.highestbars(ad ? decayadjusted : p-l, aroonlength) + aroonlength)/aroonlength
lower = 100 * (ta.lowestbars(ad ? decayadjusted : p-l, aroonlength) + aroonlength)/aroonlength

trendup = upper > lower
trenduplong = (ad ? decayadjusted : p-l) > ta.ema((ad ? decayadjusted : p-l), malen)
precondition = (sh ? trendup : trenduplong)

osupper = plot(0, color = bullcolor, display = display.none)
osmid = plot(0.1, color = bullcolor, display = display.none)
oslower = plot(0.2, color = bullcolor, display = display.none)
obupper = plot(0.8, color = bearcolor, display = display.none)
obmid = plot(0.9, color = bullcolor, display = display.none)
oblower = plot(1, color = bearcolor, display = display.none)
hline(0.5, color = color.gray)

plotshape(ta.crossover(decayadjusted,0.2) and trendup ? -0.1 : na, style = shape.triangleup, color = bullcolor, location = location.absolute, size = size.tiny)
plotshape(ta.crossunder(decayadjusted,0.8) and not trendup ? 1.1 : na, style = shape.triangledown, color = bearcolor, location = location.absolute, size = size.tiny)

plot(ad ? decayadjusted : na, color = precondition ? bullcolor : bearcolor, title = "Supply in Profits and Losses")
plot(ad ? na : (p-l), color = precondition ? bullcolor : bearcolor, title = "Supply in Profits and Losses")
plot(ma ? ta.ema((ad ? decayadjusted : p-l), malen) : na, color = color.gray)
plot(ch ? ta.change(ad ? decayadjusted : p-l, len) : na, color = color.new(color.yellow, 70), title = "Change", style = plot.style_columns)
plot( va ? valuerange-(ad ? decayadjusted : p-l)*2*valuerange : na, color = color.gray, title = "Value")

fill(obupper, oblower, color.new(bearcolor, 80))
fill (oblower, obmid, color.new(bearcolor, 87))
fill(osupper, oslower, color.new(bullcolor, 87))
fill(osupper, osmid, color.new(bullcolor, 93))

barcolor(trenduplong ? bullcolor : bearcolor)