Newzage - Fed Net Liquidity
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © azurablev

//@version=5
indicator(title='Fed Net Liquidity', shorttitle='Net Liq.', overlay=false) 

factor = input.float(1.5,"factor: ",1,2,0.01)
minus_value = input.float(1655,"minus value: ",1000,20000,10)

fairVal = request.security("((FRED:WALCL-FRED:WTREGEN-FRED:RRPONTSYD)/100000000)","", ta.ema(close, 1))
fv = (fairVal / factor - minus_value) / 10 

spx = request.security("SPX","D", ta.ema(close, 3)) 
lema  = spx - fv

spy = request.security("SPY","D", ta.ema(close, 1))
len = input.int(12, minval=1,  title="RSI Length")
len2 = input.int(9, minval=1, title="EMA of RSI Length")
emaRSI = ta.ema(ta.rsi(spy,len),len2) 

plot(lema, title='Net Liquidity', style=plot.style_line, linewidth=2, color=color.new(color.white, 0))

band1 = hline(280, title='Upper Line', linestyle=hline.style_dashed, linewidth=1, color=color.gray)
band3 = hline(-40, title='Lower Line', linestyle=hline.style_dashed, linewidth=1, color=color.white)
band0 = hline(-150, title='Lower Line', linestyle=hline.style_dashed, linewidth=1, color=color.gray)

rsi_col1 = lema>=281 and emaRSI>=50 ? color.red : lema<=-150 and emaRSI<=42.5 ? color.green : color.black 
bgcolor(rsi_col1, transp=30)

mult1 = spx < fv and emaRSI <= 36 ? 1.08: (spx < fv and emaRSI >= 36 ? 1.035 : 1)
mult2 = spx > fv and emaRSI >= 60 ? 0.94: (spx < fv and emaRSI <= 60 ? 0.97 : 1)

lblTxt = spx < fv ? str.tostring(math.round(spx/10)) + "->" + str.tostring(math.round(fv/10*mult1)) :
 str.tostring(math.round(spx/10)) + "->" + str.tostring(math.round(fv/10*mult2))

showLabel = (lema >= 281 and emaRSI >= 50) or (lema <= -150 and emaRSI <= 38.5)

if showLabel and not showLabel[1]
    label.new(bar_index, lema, text=lblTxt, style=label.style_label_down, color=color.orange, size=size.small) 
