Modified Mannarino Market Risk Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SunilHalai
// Formula being used to calculate : //TVC*DXY*TVC:US10Y*ECONOMICS:USGD/(1.61*FRED:GDP*1000)

//@version=5
indicator("Modified Mannarino Market Risk Indicator Percentile",  format = format.percent, max_bars_back=3000)

src = input(close, title="Source")
lookback_period = input.int(2000, title="Lookback Period for Percentile", maxval=3000, minval=200)


dxy = request.security("TVC:DXY","D", src)
us10y = request.security("TVC:TNX", "D", src)
debt = request.security("ECONOMICS:USGD", "D", src)
gdp = request.security("FRED:GDPPOT", "D", src)
debt_to_gdp = request.security("FRED:GFDEGDQ188S", "D", src)


mmmri = (dxy * us10y * debt)/ (1.61 * gdp * 1000000000)

h = ta.highest(mmmri, lookback_period)
l = ta.lowest(mmmri, lookback_period)


//Use this with the mmmri to plot a percentile of risk.

percentile_mmmri = ((mmmri - l)/(h - l)) * 100

if(percentile_mmmri >= 100)
    percentile_mmmri := 100


risk_color = (percentile_mmmri < 25) ?  color.green :  (percentile_mmmri < 50) ? color.yellow : (percentile_mmmri < 75) ? color.orange : color.red

plot(percentile_mmmri, color = risk_color)

