Earnings Yield & Dividend Yield (vs SP500, treasury, IG)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NatsukiKano

//@version=5

// # What's this script?
// Plot the following yields.
//  1. Treasury constant maturity rate. default is 10Y(FRED:DGS10).
//  2. Investment grade corporate bond yields by Moody's.
//     Grades from highest to lowest are represented by color bands.
//      - Moody's Seasoned Aaa Corporate bond yields.(FRED:AAA)
//      - Moody's Seasoned Baa Corporate bond yields.(FRED:DBAA)
//  3. Investment grade bond yields by BofA.
//     Grades from highest to lowest are represented by color bands.
//      - BofA AAA US Corporate Index Effective Yield.(FRED:BAMLC0A1CAAAEY)
//      - BofA BBB US Corporate Index Effective Yield.(FRED:BAMLC0A4CBBBEY)
//  3. Earnings Yield of S&P500.
//     Calculated using S&P 500 Earnings by Month provided by Nasdaq date link.
//      (https://data.nasdaq.com/data/MULTPL/SP500_EARNINGS_MONTH-sp-500-earnings-by-month)
//  4. Dividend Yield of S&P500.
//     Calculated using S&P 500 Dividend by Month provided by Nasdaq date link.
//       (https://data.nasdaq.com/data/MULTPL/SP500_DIV_MONTH-sp-500-dividend-by-month)
//  5. Earnings Yield of the displayed symbol.
//  6. Dividend Yield of the displayed symbol.
// 2023/07/10 Changed from getting IG(BofA) data from Nasdaq date link to getting it from FRED ticker.

indicator(title="Earnings Yield & Dividend Yield (vs SP500, treasury, IG)",shorttitle="Yields", precision=3, overlay=false)

// ====================
// PRE
// ====================

/// <import_statements>
// --------------------
SymbolTreasury  = input.symbol( "FRED:DGS10"    ,"Treasury Constant Maturity Rate")
Group1 ="Line Color"
ColorTreasury   = input(color.new(color.gray,10)      , "Treasury"  , group = Group1)
ColorMoodys     = input(color.rgb( 255, 165,  35, 10) , "Investment grade Corporate bond by Moody's"    , group = Group1)
ColorBofA       = input(color.rgb(  98, 186, 108, 10) , "Investment grade Corporate bond by BofA"       , group = Group1)
TransCorpBond   = input( 75, "Transparency of color bands for investment grade corporate bond", group = Group1)
ColorSP500EY    = input(color.rgb( 254,  82,  92, 10) , "S&P500 Earnings yield"   , group = Group1)
ColorSP500DY    = input(color.rgb( 172,  77, 187, 10) , "S&P500 Dividends yield"  , group = Group1)
ColorSymbolEY   = input(color.rgb(   0, 126, 244, 10) , "Symbol Earnings yeild"   , group = Group1)
ColorSymbolDY   = input(color.rgb(   0, 198, 218, 10) , "Symbol Dividends yeild"  , group = Group1)

// <request>
// --------------------

// ##1. Treasury Constant Maturity Rate
Treasury    = request.security( SymbolTreasury          , timeframe.period, close)

// ##2. Investment Grade Corporate Bond Yield Rates by Moody's
Moodys_Aaa  = request.security( "FRED:AAA"              , timeframe.period, close)
Moodys_Baa  = request.security( "FRED:DBAA"             , timeframe.period, close)

// ##3. Investment Grade Corporate Bond Yield Rates by BofA
BofA_AAA    = request.security( "FRED:BAMLC0A1CAAAEY"   , timeframe.period, close)
BofA_BBB    = request.security( "FRED:BAMLC0A4CBBBEY"   , timeframe.period, close)

// ##4. S&P 500 Earnings Yield.
SP500                   = request.security("SP500", timeframe.period, close)
EPS_SP500               = request.quandl("MULTPL/SP500_EARNINGS_MONTH", barmerge.gaps_off )
EarningsYield_SP500     = EPS_SP500 / SP500 * 100

// ##5. S&P 500 Dividend Yield.
DPS_SP500               = request.quandl("MULTPL/SP500_DIV_MONTH", barmerge.gaps_off )
DividendYield_SP500     = DPS_SP500 / SP500 * 100

// ##6. Symbol Earnings Yield
EPS_TTM                 = request.financial( syminfo.tickerid , "EARNINGS_PER_SHARE" , "TTM" , barmerge.gaps_off , false , syminfo.currency)
EPS_Y                   = request.financial( syminfo.tickerid , "EARNINGS_PER_SHARE" , "FY"  , barmerge.gaps_off , false , syminfo.currency)
EPS_Symbol              = EPS_TTM ? EPS_TTM : EPS_Y     //Combine long term financial data and short term financial data
EarningsYield_Symbol    = EPS_Symbol/close*100

// ##7. Symbol Dividends Yield
DPS_Symbol              = request.financial( syminfo.tickerid , "DPS_COMMON_STOCK_PRIM_ISSUE" , "FY" , barmerge.gaps_off , false , syminfo.currency)
DividendYield_Symbol    = DPS_Symbol/close*100

// ====================
// POST
// ====================
lineTreasury    = plot( Treasury    , "Treasury"        , ColorTreasury , linewidth = 2)
lineMoodysAaa   = plot( Moodys_Aaa  ,"Moodys_Aaa",ColorMoodys, linewidth = 2)
lineMoodysBaa   = plot( Moodys_Baa  ,"Moodys_Baa",ColorMoodys, linewidth = 1)
lineBofA_AAA    = plot( BofA_AAA,"BofA AAA",ColorBofA, linewidth = 2)
lineBofA_BBB    = plot( BofA_BBB,"BofA BBB",ColorBofA, linewidth = 1)
fill( lineMoodysAaa , lineMoodysBaa , color.new( ColorMoodys    , TransCorpBond ) )
fill( lineBofA_AAA  , lineBofA_BBB  , color.new( ColorBofA      , TransCorpBond ) )
lineEarningsYield_SP500     = plot( EarningsYield_SP500     , "S&P500Earnings"  , ColorSP500EY  , linewidth = 2)
lineEarningsYield_Symbol    = plot( EarningsYield_Symbol    , "SymbolEarnings"  , ColorSymbolEY , linewidth = 2)
lineDividendsYield_SP500    = plot( DividendYield_SP500     , "S&P500Dividend"  , ColorSP500DY  , linewidth = 2)
lineDividendsYield_Symbol   = plot( DividendYield_Symbol    , "SymbolDividend"  , ColorSymbolDY , linewidth = 2)