S&P 500 Quandl Data & Ratios
//This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
//Author © TradeAutomation


indicator("S&P 500 Quandl Data & Ratios")

// Pulls in S&P500 data using Quandl / Nasdaq Data Link //
//S&P 500 Dividend Yield by Month. 12 month dividend per share/price.
DIVYield = request.quandl("MULTPL/SP500_DIV_YIELD_MONTH", barmerge.gaps_on, 0) 
//S&P 500 PE Ratio by Month. Price to earnings ratio, based on trailing twelve month as reported earnings.
PERatio = request.quandl("MULTPL/SP500_PE_RATIO_MONTH", barmerge.gaps_off, 0) 
//Cyclically Adjusted PE Ratio (CAPE Ratio), aka Shiller PE Ratio by Month. Price earnings ratio based on average inflation-adjusted earnings.
CAPERatio = request.quandl("MULTPL/SHILLER_PE_RATIO_MONTH", barmerge.gaps_off, 0) 
//S&P 500 Earnings Yield by month. Earnings Yield = trailing 12 month earnings divided by index price (or inverse PE). Data after latest S&P report is estimated.
EYield = request.quandl("MULTPL/SP500_EARNINGS_YIELD_MONTH", barmerge.gaps_on, 0) 
//S&P 500 price to book value ratio by quarter.
PriceBook = request.quandl("MULTPL/SP500_PBV_RATIO_QUARTER", barmerge.gaps_on, 0) 
//S&P 500 Price to Sales Ratio by quarter (P/S or Price to Revenue).
PriceSales = request.quandl("MULTPL/SP500_PSR_QUARTER", barmerge.gaps_on, 0) 
//S&P 500 level, Inflation Adjusted, by Month.
RealSP500 = request.quandl("MULTPL/SP500_INFLADJ_MONTH", barmerge.gaps_on, 0) 
//S&P 500 sales by quarter. Trailing twelve month S&P 500 Sales Per Share (S&P 500 Revenue Per Share) non-inflation adjusted 
Sales = request.quandl("MULTPL/SP500_SALES_QUARTER", barmerge.gaps_on, 0) 
//S&P 500 Earnings Per Share. 12-month real earnings per share inflation adjusted
EPS = request.quandl("MULTPL/SP500_EARNINGS_MONTH", barmerge.gaps_on, 0) 

// Indicator Inputs //
DIVYieldInput = input.bool(false, "Dividend Yield", tooltip="Aqua - Plots S&P 500 Dividend Yield by Month. 12 month dividend per share/price.", group="Which metrics would you like to plot?")
PERatioInput = input.bool(true, "Price Earnings Ratio", tooltip="Purple - Plots S&P 500 PE Ratio by Month. Price to earnings ratio, based on trailing twelve month as reported earnings.", group="Which metrics would you like to plot?")
CAPERatioInput = input.bool(true, "CAPE/Shiller PE Ratio", tooltip="Green - Plots Cyclically Adjusted PE Ratio (CAPE Ratio), aka Shiller PE Ratio by Month. Price earnings ratio based on average inflation-adjusted earnings.", group="Which metrics would you like to plot?")
EYieldInput = input.bool(false, "Earnings Yield", tooltip="Red - Plots S&P 500 Earnings Yield by month. Earnings Yield = trailing 12 month earnings divided by index price (or inverse PE). Data after latest S&P report is estimated.", group="Which metrics would you like to plot?")
PriceBookInput = input.bool(false, "Price Book Ratio", tooltip="Gray - Plots S&P 500 price to book value ratio by quarter.", group="Which metrics would you like to plot?")
PriceSalesInput = input.bool(false, "Price Sales Ratio", tooltip="Blue - Plots S&P 500 Price to Sales Ratio by quarter (P/S or Price to Revenue).", group="Which metrics would you like to plot?")
RealSP500Input = input.bool(false, "Inflation Adjusted SP500", tooltip="Olive - Plots the S&P 500 level, Inflation Adjusted, by Month.", group="Which metrics would you like to plot?")
SalesInput = input.bool(false, "Revenue Per Share", tooltip="Fuchsia - Plots Trailing twelve month S&P 500 Sales Per Share (S&P 500 Revenue Per Share) non-inflation adjusted.", group="Which metrics would you like to plot?")
EPSInput = input.bool(false, "Earnings Per Share", tooltip="Orange - Plots S&P 500 Earnings Per Share (EPS). 12-month real earnings per share inflation adjusted", group="Which metrics would you like to plot?")
LineWidth = input.int(1, "Line Width", group="Plot Settings")

// Plotting //
plot(DIVYieldInput==true ? DIVYield : na, title='DIVYield', linewidth=LineWidth, color=color.aqua)
plot(PERatioInput==true ? PERatio : na, title='PERatio', linewidth=LineWidth, color=color.purple)
plot(CAPERatioInput==true ? CAPERatio : na, title='CAPERatio', linewidth=LineWidth, color=color.green)
plot(EYieldInput==true ? EYield : na, title='EYield', linewidth=LineWidth, color=color.red)
plot(PriceBookInput==true ? PriceBook : na, title='PriceBook', linewidth=LineWidth, color=color.gray)
plot(PriceSalesInput==true ? PriceSales : na, title='PriceSales', linewidth=LineWidth, color=color.blue)
plot(RealSP500Input==true ? RealSP500 : na, title='RealSP500', linewidth=LineWidth, color=color.olive)
plot(SalesInput==true ? Sales : na, title='Sales', linewidth=LineWidth, color=color.fuchsia)
plot(EPSInput==true ? EPS : na, title='EPS', linewidth=LineWidth, color=color.orange)
