10-2 Year Treasury Yield Spread by zdmre
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zdmre

//@version=5
indicator("US TSY Yield Curves by zdmre", overlay=false)

us02y = request.security("TVC:US02Y", "D", close)
us03y = request.security("TVC:US03Y", "D", close)
us05y = request.security("TVC:US05Y", "D", close)
us10y = request.security("TVC:US10Y", "D", close)
us30y = request.security("TVC:US30Y", "D", close)
diff = us10y - us02y
diff30_5 = us30y - us05y
diff10_3 = us10y - us03y
diff10_5 = us10y - us05y
diff30_2 = us30y - us02y

bearish = ta.cross(diff, 0) == 1 and diff < 0
bullish = ta.cross(diff, 2.6) == 1 and diff > 2.6

plot(bearish ? diff : na ,title='Recession Risk', style=plot.style_circles, color=color.new(color.red, 0), linewidth=5)
plot(bullish ? diff : na ,title='Recovery Plan ', style=plot.style_circles, color=color.new(color.green, 0), linewidth=5)

if bearish
    label1 = label.new(bar_index, 0 , text='|\n'+ str.format("{0,date, y\nMMM-d}", time), style=label.style_label_up, color=color.new(color.red,100))
    label.set_size(label1, size.small)
    label.set_textcolor(label1, color.red)
if bullish
    label2 = label.new(bar_index, 2.6 , text=str.format("{0,date, y\nMMM-d\n}", time)+'|', style=label.style_label_down, color=color.new(color.green,100))
    label.set_size(label2, size.small)
    label.set_textcolor(label2, color.green)

uband = hline(2.6, "Upper Band", color=#787B86)
mband = hline(1.3, "Mid-Band", color=color.new(color.yellow, 50))
lband = hline(0, "Lower Band", color=#787B86)

var tbis = table.new(position.middle_right, 2, 6, frame_color=color.black, frame_width=0, border_width=1, border_color=color.black)
table.cell(tbis, 0, 1, '10Y-2Y', bgcolor = color.new(#fff407, 0), text_size=size.normal, text_color=color.black)
table.cell(tbis, 0, 2, '10Y-3Y', bgcolor = color.new(#0765ff, 0), text_size=size.normal, text_color=color.white)
table.cell(tbis, 0, 3, '10Y-5Y', bgcolor = color.new(#34ff07, 0), text_size=size.normal, text_color=color.black)
table.cell(tbis, 0, 4, '30Y-2Y', bgcolor = color.new(#07f4ff, 0), text_size=size.normal, text_color=color.black)
table.cell(tbis, 0, 5, '30Y-5Y', bgcolor = color.new(#ff6807, 0), text_size=size.normal, text_color=color.black)

table.cell(tbis, 1, 1, str.tostring(diff[0]), text_color=color.white, text_size=size.normal, bgcolor=(diff[0] <= 0 ? color.red: diff[0] >= 2.6  ? color.lime : color.black))
table.cell(tbis, 1, 2, str.tostring(diff10_3[0]), text_color=color.white, text_size=size.normal, bgcolor= (diff10_3[0] <= 0 ? color.red: diff10_3[0] >= 2.6  ? color.lime : color.black))
table.cell(tbis, 1, 3, str.tostring(diff10_5[0]), text_color=color.white, text_size=size.normal, bgcolor= (diff10_5[0] <= 0 ? color.red: diff10_5[0] >= 2.6  ? color.lime : color.black))
table.cell(tbis, 1, 4, str.tostring(diff30_2[0]), text_color=color.white, text_size=size.normal, bgcolor= (diff30_2[0] <= 0 ? color.red: diff30_2[0] >= 2.6  ? color.lime : color.black))
table.cell(tbis, 1, 5, str.tostring(diff30_5[0]), text_color=color.white, text_size=size.normal, bgcolor= (diff30_5[0] <= 0 ? color.red: diff30_5[0] >= 2.6  ? color.lime : color.black))

fill(lband, uband, color=color.rgb(126, 87, 194, 90), title="Background")
plot(diff, title="10Y-2Y", color=color.new(#fff407, 0))
plot(diff10_3, title="10Y-3Y", color=color.new(#0765ff, 0), display=display.none)
plot(diff10_5, title="10Y-5Y", color=color.new(#34ff07, 0), display=display.none)
plot(diff30_2, title="30Y-2Y", color=color.new(#07f4ff, 0), display=display.none)
plot(diff30_5, title="30Y-5Y", color=color.new(#ff6807, 0), display=display.none)