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/
// © kevindcarr

//@version=5
indicator(title='Mannarino Market Risk Indicator', shorttitle='MMRI', overlay=false, scale=scale.right, format=format.price)

var table risk = table.new(position=position.top_right, columns=1, rows=1)
var table data = table.new(position=position.bottom_right, columns=3, rows=3, frame_width=4, border_width=4)

lowRisk = input(title='Low Risk', defval=100)
moderateRisk = input(title='Moderate Risk', defval=200)
highRisk = input(title='High Risk', defval=300)
maLength = input(title="SMA Length", defval=50)
hideSma = input.bool(title="Hide SMA", defval=false)
hideData = input.bool(title="Hide Data", defval=false)

highlight(mmri) =>
    col = color.green
    if mmri > lowRisk and mmri < moderateRisk
        col := color.yellow
        col
    if mmri >= moderateRisk and mmri < highRisk
        col := color.orange
        col
    if mmri >= highRisk
        col := color.red
        col
    col

riskLevel(mmri) =>
    text_1 = 'Low Risk'
    if mmri > lowRisk and mmri < moderateRisk
        text_1 := 'Moderate Risk'
        text_1
    if mmri >= moderateRisk and mmri < highRisk
        text_1 := 'High Risk'
        text_1
    if mmri >= highRisk
        text_1 := 'Extreme Risk'
        text_1
    text_1

dollarSymbol = input.symbol(title='Dollar Symbol', defval='dxy')
yieldSymbol = input.symbol(title='Yield Symbol', defval='tnx')
oilSymbol = input.symbol(title="Oil Symbol", defval='usoil')
mmriConstant = input(title='Divisor', defval=1.61)

dxy = request.security(symbol=dollarSymbol, timeframe='D', expression=close)
yield = request.security(symbol=yieldSymbol, timeframe='D',expression=close)
mmri = dxy * yield / mmriConstant
oil = request.security(symbol=oilSymbol, timeframe='D', expression=close)

table.set_bgcolor(risk, color.new(highlight(mmri), 25))
table.cell(risk, 0, 0, str.tostring(riskLevel(mmri)))
plot(series=mmri, title="MMRI", trackprice=false, color=highlight(mmri))
plot(series=hideSma ? na : ta.sma(mmri, maLength), title="SMA", color=color.new(color.blue, 50))

highlight2(security) =>
    col = color.gray
    if ta.roc(security, 1) > 0
        col := color.green
        col
    if ta.roc(security, 1) < 0
        col := color.red
        col
    col

if not hideData
    table.cell(data, 0, 0, str.tostring("10 Yr"), text_color=color.gray)
    table.cell(data, 1, 0, str.format("{0,number,#.##}", yield), text_color=color.gray) 
    table.cell(data, 2, 0, str.format("{0,number,#.##} {1}", ta.roc(yield, 1), "%"), text_color=color.new(highlight2(yield), 25))
    table.cell(data, 0, 1, str.tostring("Dollar"), text_color=color.gray)
    table.cell(data, 1, 1, str.format("{0,number,#.##}", dxy), text_color=color.gray)
    table.cell(data, 2, 1, str.format("{0,number,#.##} {1}", ta.roc(dxy, 1), "%"), text_color=color.new(highlight2(dxy), 25))
    table.cell(data, 0, 2, str.tostring("Oil"), text_color=color.gray)
    table.cell(data, 1, 2, str.format("{0,number,#.##}", oil), text_color=color.gray)
    table.cell(data, 2, 2, str.format("{0,number,#.##} {1}", ta.roc(oil, 1), "%"), text_color=color.new(highlight2(oil), 25))
