Blockchain Fundamentals - Active Address Sentiment Osc. [CR]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © theheirophant
//@version=5

/////////////////////////////////////////////////////////////////////////////////////////\
///////////////////////////////////ALERT//////////////////////////////////////////////////\
//use only on DAILY timeframe I did not implement MTF functions for the calculations yet///|>
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
indicator("Blockchain Fundamentals: Active Address Sentiment Oscillator [CR]")

//choose MA function
getMA(src, length, maSelect) =>
    ma = 0.0
    if maSelect == "EMA"
        ma := ta.ema(src, length)
        ma
    if maSelect == "SMA"
        ma := ta.sma(src, length)
        ma
    if maSelect == "VWMA"
        ma := ta.vwma(src, length)
        ma
    if maSelect == "WMA"
        ma := ta.wma(src, length)
        ma
    if maSelect == "RMA"
        ma := ta.rma(src, length)
        ma
    if maSelect == "ALMA"
        ma := ta.alma(src, length, 0.85, 6)
        ma
    if maSelect == "SWMA"
        ma := ta.swma(src)
        ma
    if maSelect == "HMA"
        ma := ta.hma(src, length)
        ma    
    ma

//are you naughty traders on the daily chart or not?
daily = timeframe.isdaily
if not daily 
    runtime.error("USE DAILY CHART =)")
    
//data
numuniqaddr   = request.security('quandl:bchain/naddu', 'D', close)
pnumuniqaddr  = request.security('quandl:bchain/naddu', 'D', close[28])
price         = request.security('BNC:BLX', 'D', close)
pprice        = request.security('BNC:BLX', 'D', close[28])

//inputs
maSelect      = input.session(title="Standard Deviation Bands MA Type (SMA is default)", defval="SMA", options=["EMA", "SMA", "VWMA", "WMA", "HMA", "RMA", "ALMA", "SWMA"], group = "SD Settings")
length2       = input.int(28, minval=2, title='Standard Deviation Bands MA Length (28 is default)', group = "SD Settings")
multi         = input.float(2, minval=0.1, title='Standard Deviation Bands Multi (2 is default)', group = "SD Settings")
//conditionals
tru           = input.bool(false, "Plot background coloration for over/under valued?", group = "Settings")
tru2          = input.bool(false, "Plot candle coloration for over/under valued?", group = "Settings")
tru3          = input.bool(true, "Apply smoothing to Price Change plot? (WMA - 3 period)", group = "Settings")

//calculation
priceMA  = tru3 ? ta.wma((price - pprice) / pprice, 3) : (price - pprice) / pprice
daa      = (numuniqaddr - pnumuniqaddr) / pnumuniqaddr
bandU    = getMA(daa + (multi * ta.stdev(daa, 20)),length2, maSelect)
bandL    = getMA(daa - (multi * ta.stdev(daa, 20)),length2, maSelect)
colorBG  = priceMA > bandU ? color.new(color.lime, 90) : priceMA < bandL ? color.new(color.red,90) : color.new(color.yellow,100)
colorBG2 = priceMA > .40 or priceMA < -.30 ? color.new(color.white, 0) : priceMA > bandU ? color.new(color.lime, 0) : priceMA < bandL ? color.new(color.red, 0) : color.new(color.yellow,100)
colorBG3 = priceMA > .40 or priceMA < -.30 ? color.new(color.white, 90) : color.new(color.yellow,100)


//plots
bgcolor(tru ? colorBG : na)
bgcolor(tru ? colorBG3 : na)
barcolor(tru2 ? colorBG2 : na)

plot(daa*100, 'DAA Change', color=color.new(color.gray, 30))
plot(priceMA*100, 'Price Change', color=color.new(color.yellow, 0))
plot(bandU*100, 'Upper Band', color=color.new(color.red, 0))
plot(bandL*100, 'Lower Band', color=color.new(color.lime, 0))
