Abz US Real rates
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/. Original version by © MrDevDevy

//FED Funds Rate vs Inflation
//New: 
  //I have added the US 10 year yield. When this is higher than inflation and the Fed Funds Rate and the US10Y is trending downwards, then this is going generally be good for TLT.
  //TLT demand will change based on being a flight to safety in a crash such as the Covid crash and this could occur despite other factors. Also if the government wants to sell more bonds than others will buy then TLT may go down in price despite the US10Y not going up as much or otherwise not reflecting that change.

//@version=5
indicator("Abz US Real rates", shorttitle = "Real rates")

showUS10Y = input.bool(true, "Show US 10 year yield rate - Strong green good for TLT")

FedRateColor      = input.color(defval=color.rgb(149, 255, 0, 77),  title="Fed Funds Rate color")
InflationColor    = input.color(defval=color.rgb(122, 0, 221, 52),  title="US Inflation rate color")

FedFundsRate    = request.security("FEDFUNDS",  "D", close)
USinflation     = request.security("USIRYY",  "D", close)
US10Y           = request.security("US10Y",  "D", close)
US10ma = ta.sma(ta.sma(US10Y,3),4)

FFRplot = plot(FedFundsRate,  color=FedRateColor, title = "Fed Fund Rates plot")
Inflation = plot(USinflation,  color=InflationColor, title = "US inflation plot")
Inversionplot = plot(US10ma,  color= not showUS10Y ? na : FedFundsRate > US10Y or (FedFundsRate > FedFundsRate[1] and FedFundsRate > (US10Y + US10ma)/2) or (FedFundsRate > FedFundsRate[1] and USinflation> (US10Y + US10ma)/2) ? color.rgb(221, 0, 0, 35) : na, linewidth = 6, title = "Yield Inversion plot")
TLTtimeplot = plot(US10ma,  color= not showUS10Y  or (FedFundsRate > FedFundsRate[1] and FedFundsRate > (US10Y + US10ma)/2) or (FedFundsRate > FedFundsRate[1] and USinflation> (US10Y + US10ma)/2) ? na : FedFundsRate < US10Y and (US10Y + US10ma)/2 > FedFundsRate and (US10Y + US10ma)/2 - 0.7 > USinflation ? color.rgb(20, 255, 47, 35) : na, linewidth = 6, title = "Time for TLT plot")
US10maplot = plot(US10ma,  color= not showUS10Y ? na : (US10Y + US10ma) > (US10Y[1] + US10ma[1]) ? color.rgb(255, 0, 255, 10) : color.rgb(30, 255, 0, 10), linewidth = 2, title = "US 10 Year Bond Yield % plot")
fill(FFRplot, Inflation, color =FedFundsRate > USinflation ? color.rgb(60, 60, 255, 5) :  color.rgb(188, 217, 42, 45), title = "Fed Fund Rates vs US inflation FILL")
bgcolor(FedFundsRate > US10Y ? color.new(color.rgb(221, 0, 0),90) : na, title = "Yield Inversion BGcolor")



//============================================
//Plot Indicator Label
showIndicatorLabel = true
indicatorLabel = table.new(position.top_left, columns=1, rows=1, bgcolor=color.rgb(0, 0, 0, 50))
if showIndicatorLabel and barstate.islast
    table.cell(table_id=indicatorLabel, column=0, row=0, text="US real rates", height=0, text_color=color.rgb(255, 255, 255, 20), text_halign=text.align_center, text_valign= text.align_center)
//============================================



//============================================
// Recessions are shown with a gray background. Same as my Abz US Recessions indicator
r = request.quandl('FRED/USRECD')
recession = r == 1 ? true : false

color shade = input(defval=color.new(color.gray, 85), title='Recession Shading')
bgcolor(recession ? shade : na, title = "US Recessions")