Fear and Greed Intraday Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © barnabygraham

// 1 to 7 are the original variables which this indicator is based upon. I've added others to add range, diversification and to improve the greed signals so that they (are more likely to) occur before downward moves. 

//@version=5
indicator("Fear and Greed Intraday Index", overlay=false, timeframe="", timeframe_gaps=true)
length=input(1, "Length (Smoothing)")
LengthSMA=input(125,"Length of SMAs")
compression=input(5,"Compression")

// 1. Advancers minus decliners - the original index uses NYSE 52 week high/lows. That's now variable #14.
ADD = request.security('ADD', "", close)
ADD2 = ADD/30
ADDma= ta.ema(ADD2,5)
ADDavg=((ADD2*2)+ADDma)/3
ADD3 = ADDavg > 100 ? 100 : ADDavg < -100 ? -100 : ADDavg
//plot(ADD3)

// 2. Volume Breadth - VOLD
VOLD = request.security('VOLD', '', hlc3)
VOLD2 = VOLD/4000000
VOLDma= ta.ema(VOLD2,5)
VOLDavg=((VOLD2*2)+VOLDma)/3
VOLD3 = VOLDavg > 100 ? 100 : VOLDavg < -100 ? -100 : VOLDavg
//plot(VOLD3)

// 3. Market momentum - Changing this to use RSI instead of to compare against an SMA
SPY = request.security('SPY', '', close)
momentum = (ta.rsi(SPY,14)-50)*2
momo = momentum > 100 ? 100 : momentum < -100 ? -100 : momentum
//plot(momo)

// 5. Safe haven demand; stocks vs bonds
TLT = request.security('TLT','',close)
TLTma = ((-TLT/ta.sma(-TLT,LengthSMA))*-800)+800
TLT2 = na(TLTma) ? 0 : TLTma
SvB2 = (TLT2 + momentum)
SvB = SvB2 > 100 ? 100 : SvB2 < -100 ? -100 : SvB2
//plot(SvB)

// 6. HYG vs TLT, high yeild vs low yeild
HYG2 = request.security('HYG','',close)
HYG = na(HYG2) ? 0 : HYG2
HYGma = ((HYG/ta.sma(HYG,LengthSMA))*800)-800
HTG2 = na(HYGma) ? 0 : HYGma
HvL2 = HTG2+TLT2 > 100 ? 100 : HTG2+TLT2 < -100 ? -100 : HTG2+TLT2
HvL = (ta.rsi(HvL2,14)-50)*2
//plot(HvL)

// 7. VIX - Volatility
VIX = request.security('VIX', '', close)
VIX2=(((VIX-10)*5)-100)*-1
VIX4 = VIX2 > 100 ? 100 : VIX2 < -100 ? -100 : VIX2
VIX3 = (ta.rsi(VIX4,5)-50)*2
//plot(VIX3)

// 8. Risk Sentiment - XBI vs XLP; Biotech vs staples
XBI = request.security('XBI','',close)
XLP = request.security('XLP','',close)
xlprsi = (ta.rsi(XLP,14)-50)*-2
XBIrsi = (ta.rsi(XBI,5)-50)*2
XVIvsXLP2 = ta.hma(XBIrsi+xlprsi,50)
XVIvsXLP = XVIvsXLP2 > 100 ? 100 : XVIvsXLP2 < -100 ? -100 : XVIvsXLP2
//plot(XVIvsXLP)

// 9.  VUG vs VTV; growth vs value
VUG = request.security('VUG','',close)
VTV = request.security('VTV','',close)
GvV = (ta.rsi((ta.roc(VTV,30)+ta.roc(VUG,30)),5)-50)*2
GvVz= GvV > 100 ? 100 : GvV < -100 ? -100 : GvV
GvVzFin=na(GvVz) ? 0 : GvVz
//plot(GvVzFin)

// 10. DXY rate of change
DXY = request.security('DXY', '', close)
DXYROC=ta.roc(DXY,50)*-50
DXYz = DXYROC > 100 ? 100 : DXYROC < -100 ? -100 : DXYROC
//plot(DXYz)

// 11. Aussie Dollar rate of change
AUDUSD = request.security('AUDUSD','',close)
AUDROC = ta.roc(AUDUSD,200)*50
AUDz = AUDROC > 100 ? 100 : AUDROC < -100 ? -100 : AUDROC
//plot(AUDz)


// 12. SPY / VIX 10-day correlation - Early greed signals - (I felt this indicator was lacking appropriate greed signals before adding these)
SPYVIXa=ta.correlation(SPY,VIX,100)
SPYVIX10=(((SPYVIXa)*69)+60)
SPYVIXa10=SPYVIX10 > 100 ? 100 : SPYVIX10 < -100 ? -100 : SPYVIX10
//plot(SPYVIXa10)


// 13. SPY / VIX 20-day correlation - Early greed signals - (I felt this indicator was lacking appropriate greed signals before adding these)
SPYVIXb=ta.correlation(SPY,VIX,200)
SPYVIX20=(((SPYVIXb)*69)+60)
SPYVIXb20=SPYVIX20 > 100 ? 100 : SPYVIX20 < -100 ? -100 : SPYVIX20
//plot(SPYVIXb20)



// // 18. this is just an idea for now and may be better to use GS or some other large investment bank.... let me know if you have any ideas about this
// IBKR = request.financial('NASDAQ:IBKR', 'TOTAL_LIABILITIES' ,'FQ', barmerge.gaps_on)
// IBKRSMA = (ta.ema(IBKR,2))
// IBKRfin = IBKR/IBKRSMA
// IBKRfin2=ta.highest((((IBKRfin)-1.01)*2000),10)
// plot(IBKRfin2)

subTotal = ((ADD3 + VOLD3 + momo + SvB + HvL + VIX3 + XVIvsXLP + GvVzFin + DXYz + AUDz + SPYVIXa10 + SPYVIXb20) / compression)
Total = subTotal > 100 ? 100 : subTotal < -100 ? -100 : subTotal
Final = ta.ema(Total,length)


plot = plot(Final, color=color.from_gradient(Total, -100, 100, #17CC00, #FF0000))

hline(0, linestyle=hline.style_dotted, color=color.new(color.white,85))
hline(50, linestyle=hline.style_dotted, color=color.new(color.red,70))
hline(100, linestyle=hline.style_dotted, color=color.new(color.red,50))
hline(-50, linestyle=hline.style_dotted, color=color.new(color.green,70))
hline(-100, linestyle=hline.style_dotted, color=color.new(color.green,50))

//Fill colouring
// fillColorHigh =color.from_gradient(Final, 75, 100, color.new(#FF0000,100), #FF0000)
// fillColorLow = color.from_gradient(Final, -100, -75, #17CC00,color.new(#17CC00,100))
// p1 = plot(75, color=color.new(color.gray,100))
// p2 = plot(-75,color=color.new(color.gray,100))
// fill(plot,p1,fillColorHigh)
// fill(plot,p2,fillColorLow)