Annualizer: New Indicator + CPI Analysis
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Skipper86

//Date created: 9/25/2022
//Purpose of indicator: To analyze month-over-month (MoM) and year-over-year (YoY) changes in cumulative indexes such as CPI and M2 
//Calculates:
//1. year-over-year % change
//2. annualized % change based on MoM % change (extrapolates MoM % change for comparison to YoY by multiplying by 12)

//notes: 
//1. This indicator will not work on percent change ticker symbols such as YoY CPI percentage ("USCCPI"), 
//   it must be used on cumulative ticker symbols such as "CPIAUCSL" or "M2SL"
//2. This indicator will work on any timeframe but it will always show data based on monthly chart data.

//@version=5
indicator(title="Annualizer", shorttitle="Annualized", format=format.percent)
//designates script as an indicator script, assigns titles, and sets y-axis format to percent

TickerSymbol = input.symbol("CPIAUCSL","Symbol")
//Creates user input in settings menu for changing ticker symbol, defaults to CPIAUCSL
//CPIAUCSL = Consumer Price Index for All Urban Consumers: All Items in U.S. City Average

//TickerSymbol = syminfo.tickerid
//In order to link the ticker symbol to the chart's symbol, use the alternate TickerSymbol formula above (ref.)

//note: the 2 formulas below are referenced by the formula below them which runs monthly chart data (monthly closes) through
//them regardless of the current timeframe of the chart.  This allows the indicator to be used on any timeframe while
//still properly referencing monthly data for annualization
//This limits the script to only capturing monthly data, even if weekly data or data reported more often is available.  
//This limitation should be suitable for macroeconomic data such as cpi and M2 money supply which are usually analyzed
//on a monthly basis.  In order to work with lower timeframes, the "M" in the 3rd formula below needs to be changed to
//reflect a lower timeframe such as "W", "D", or "60" (1hr).  Multipliers and lookbacks will also need to be adjusted.

pctchangeYoY = 100*(close-close[12])/close[12]
//pct change YoY = 100*(current value - value from 12 months ago)/value from 12 months ago

pctchangeAnn = 100*(close - close[1])/close[1]*12
//MoM pct change annualized = MoM pct change multipled by number of months in a year (12)
//MoM pct change = 100*(current value - previous month's value)/previous month's value

[YoYx, Annx] = request.security(TickerSymbol, "M", [pctchangeYoY, pctchangeAnn], lookahead=barmerge.lookahead_on)
//calculates YoY and Annualized MoM pct changes by running monthly data of TickerSymbol through formulas above

plot(YoYx, color=#FF0000, title="YoY % Change")
plot(Annx, color=TickerSymbol == "FRED:M2SL" ? #006600:#2962FF, title="Annualized % Change")
//plots calculated pct changes below main chart
//if Ticker symbol is "FRED:M2SL" which is M2 money supply, plots annualized pct change in green, otherwise, plots it in blue

hline(0, color=#787B8650)
//plots horizontal line at zero pct for reference

//End of Script