YoY or MoM Returns
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rodopacapital

//@version=5
indicator("YoY or MoM Returns", overlay=false)

// Option inputs
var string returnsType = input.string("YoY", "Returns Type", options=["YoY", "MoM"])


// Calculate the returns
var float prevClose = na
var float prevYearClose = na
var float prevMonthClose = na

prevClose := request.security(syminfo.ticker, "D", close[1])
prevYearClose := request.security(syminfo.ticker, "252D", close[1]) // 252 trading days in a year
prevMonthClose := request.security(syminfo.ticker, "21D", close[1]) // 21 trading days in a month


var float returns = na
if returnsType == "YoY"
    returns := (close / prevYearClose - 1) * 100
else
    returns := (close / prevMonthClose - 1) * 100

//Change color under Zero level 
col= returns > 0 ? color.rgb(82, 146, 97) : color.rgb(255, 63, 63)        

// Plot the returns
plot(returns, title="Returns", color = col, style=plot.style_line, linewidth=2)
hline(0,"Zero",color = color.gray)