Global Yield Spread
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Celestial-Eye

//@version=5
indicator("Global Yield Spread", overlay=true, scale = scale.left)

// Define the bond yields for each country
china_3my = request.security("CN01Y", "D", close)
china_2y = request.security("CN02Y", "D", close)
china_10y = request.security("CN10Y", "D", close)
china_30y = request.security("CN30Y", "D", close)

japan_3my = request.security("JP03MY", "D", close)
japan_2y = request.security("JP02Y", "D", close)
japan_10y = request.security("JP10Y", "D", close)
japan_30y = request.security("JP30Y", "D", close)


germany_3my = request.security("DE03MY", "D", close)
germany_2y = request.security("DE02Y", "D", close)
germany_10y = request.security("DE10Y", "D", close)
germany_30y = request.security("DE30Y", "D", close)

italy_3my = request.security("IT03MY", "D", close)
italy_2y = request.security("IT02Y", "D", close)
italy_10y = request.security("IT10Y", "D", close)
italy_30y = request.security("IT30Y", "D", close)

canada_3my = request.security("CA03MY", "D", close)
canada_2y = request.security("CA02Y", "D", close)
canada_10y = request.security("CA10Y", "D", close)
canada_30y = request.security("CA30Y", "D", close)

russia_3my = request.security("RU03MY", "D", close)
russia_2y = request.security("RU02Y", "D", close)
russia_10y = request.security("RU10Y", "D", close)
russia_30y = request.security("RU20Y", "D", close)

france_3my = request.security("FR03MY", "D", close)
france_2y = request.security("FR02Y", "D", close)
france_10y = request.security("FR10Y", "D", close)
france_30y = request.security("FR30Y", "D", close)

uk_3my = request.security("GB03MY", "D", close)
uk_2y = request.security("GB02Y", "D", close)
uk_10y = request.security("GB10Y", "D", close)
uk_30y = request.security("GB30Y", "D", close)

india_3my = request.security("IN03MY", "D", close)
india_2y = request.security("IN02Y", "D", close)
india_10y = request.security("IN10Y", "D", close)
india_30y = request.security("IN30Y", "D", close)

us_3my = request.security("US03MY", "D", close)
us_2y = request.security("US02Y", "D", close)
us_10y = request.security("US10Y", "D", close)
us_30y = request.security("US30Y", "D", close)


// Calculate yield spread
china_yield_spread10to3m = china_10y - china_3my
japan_yield_spread10to3m = japan_10y - japan_3my
germany_yield_spread10to3m = germany_10y - germany_3my
italy_yield_spread10to3m = italy_10y - italy_3my
canada_yield_spread10to3m = canada_10y - canada_3my
russia_yield_spread10to3m = russia_10y - russia_3my
france_yield_spread10to3m = france_10y - france_3my
uk_yield_spread10to3m = uk_10y - uk_3my
india_yield_spread10to3m = india_10y - india_3my
us_yield_spread10to3m = us_10y - us_3my

china_yield_spread10to02 = china_10y - china_2y
japan_yield_spread10to02 = japan_10y - japan_2y
germany_yield_spread10to02 = germany_10y - germany_2y
italy_yield_spread10to02 = italy_10y - italy_2y
canada_yield_spread10to02 = canada_10y - canada_2y
russia_yield_spread10to02 = russia_10y - russia_2y
france_yield_spread10to02 = france_10y - france_2y
uk_yield_spread10to02 = uk_10y - uk_2y
india_yield_spread10to02 = india_10y - india_2y
us_yield_spread10to02 = us_10y - us_2y

china_yield_spread30to10 = china_30y - china_10y
japan_yield_spread30to10 = japan_30y - japan_10y
germany_yield_spread30to10 = germany_30y - germany_10y
italy_yield_spread30to10 = italy_30y - italy_10y
canada_yield_spread30to10 = canada_30y - canada_10y
russia_yield_spread30to10 = russia_30y - russia_10y
france_yield_spread30to10 = france_30y - france_10y
uk_yield_spread30to10 = uk_30y - uk_10y
india_yield_spread30to10 = india_30y - india_10y
us_yield_spread30to10 = us_30y - us_10y


showCN = input.bool(false, "Show CN Yield Spread")
showJP = input.bool(false, "Show JP Yield Spread")
showGE = input.bool(false, "Show GE Yield Spread")
showIT = input.bool(false, "Show IT Yield Spread")
showCA = input.bool(false, "Show CA Yield Spread")
showRU = input.bool(false, "Show RU Yield Spread")
showFR = input.bool(false, "Show FR Yield Spread")
showGB = input.bool(false, "Show GB Yield Spread")
showIN = input.bool(false, "Show IN Yield Spread")
showUS = input.bool(false, "Show US Yield Spread")



// Plot yield spread
plot( showCN?china_yield_spread10to3m:na, color=color.blue, linewidth=2, title="China 10Y - 3M Yield Spread")
plot( showJP?japan_yield_spread10to3m:na, color= color.red, linewidth=2, title="Japan 10Y - 3M Yield Spread")
plot( showGE?germany_yield_spread10to3m:na, color= color.green, linewidth=2, title="Germany 10Y - 3M Yield Spread")
plot( showIT?italy_yield_spread10to3m:na, color=  color.orange, linewidth=2, title="Italy 10Y - 3M Yield Spread")
plot( showCA?canada_yield_spread10to3m:na, color=  color.purple, linewidth=2, title="Canada 10Y - 3M Yield Spread")
plot( showRU?russia_yield_spread10to3m:na, color=  color.yellow, linewidth=2, title="Russia 10Y - 3M Yield Spread")
plot( showFR?france_yield_spread10to3m:na, color=  color.teal, linewidth=2, title="France 10Y - 3M Yield Spread")
plot( showGB?uk_yield_spread10to3m:na, color=  color.gray, linewidth=2, title="UK 10Y - 3M Yield Spread")
plot( showIN?india_yield_spread10to3m:na, color=  color.maroon, linewidth=2, title="India 10Y - 3M Yield Spread")
plot( showUS?us_yield_spread10to3m:na, color=  color.black, linewidth=2, title="US 10Y - 3M Yield Spread")

plot( showCN?china_yield_spread10to02:na, color=color.blue, linewidth=2, title="China 10Y - 2Y Yield Spread")
plot( showJP?japan_yield_spread10to02:na, color= color.red, linewidth=2, title="Japan 10Y - 2Y Yield Spread")
plot( showGE?germany_yield_spread10to02:na, color= color.green, linewidth=2, title="Germany 10Y - 2Y Yield Spread")
plot( showIT?italy_yield_spread10to02:na, color=  color.orange, linewidth=2, title="Italy 10Y - 2Y Yield Spread")
plot( showCA?canada_yield_spread10to02:na, color=  color.purple, linewidth=2, title="Canada 10Y - 2Y Yield Spread")
plot( showRU?russia_yield_spread10to02:na, color=  color.yellow, linewidth=2, title="Russia 10Y - 2Y Yield Spread")
plot( showFR?france_yield_spread10to02:na, color=  color.teal, linewidth=2, title="France 10Y - 2Y Yield Spread")
plot( showGB?uk_yield_spread10to02:na, color=  color.gray, linewidth=2, title="UK 10Y - 2Y Yield Spread")
plot( showIN?india_yield_spread10to02:na, color=  color.maroon, linewidth=2, title="India 10Y - 2Y Yield Spread")
plot( showUS?us_yield_spread10to02:na, color=  color.black, linewidth=2, title="US 10Y - 2Y Yield Spread")

plot( showCN?china_yield_spread30to10:na, color=color.blue, linewidth=2, title="China 30Y - 10Y Yield Spread")
plot( showJP?japan_yield_spread30to10:na, color= color.red, linewidth=2, title="Japan 30Y - 10Y Yield Spread")
plot( showGE?germany_yield_spread30to10:na, color= color.green, linewidth=2, title="Germany 30Y - 10Y Yield Spread")
plot( showIT?italy_yield_spread30to10:na, color=  color.orange, linewidth=2, title="Italy 30Y - 10Y Yield Spread")
plot( showCA?canada_yield_spread30to10:na, color=  color.purple, linewidth=2, title="Canada 30Y - 10Y Yield Spread")
plot( showRU?russia_yield_spread30to10:na, color=  color.yellow, linewidth=2, title="Russia 30Y - 10Y Yield Spread")
plot( showFR?france_yield_spread30to10:na, color=  color.teal, linewidth=2, title="France 30Y - 10Y Yield Spread")
plot( showGB?uk_yield_spread30to10:na, color=  color.gray, linewidth=2, title="UK 30Y - 10Y Yield Spread")
plot( showIN?india_yield_spread30to10:na, color=  color.maroon, linewidth=2, title="India 30Y - 10Y Yield Spread")
plot( showUS?us_yield_spread30to10:na, color=  color.black, linewidth=2, title="US 30Y - 10Y Yield Spread")



// Calculate global yield spread
global_yield_spread10to3m = (china_yield_spread10to3m + germany_yield_spread10to3m + canada_yield_spread10to3m + france_yield_spread10to3m + uk_yield_spread10to3m + india_yield_spread10to3m) / 6
global_yield_spread10to02 = (china_yield_spread10to02 + japan_yield_spread10to02+ germany_yield_spread10to02 + italy_yield_spread10to02 + canada_yield_spread10to02 + france_yield_spread10to02 + uk_yield_spread10to02 + india_yield_spread10to02) / 8
global_yield_spread30to10 = (china_yield_spread30to10 + japan_yield_spread30to10 + germany_yield_spread30to10 + italy_yield_spread30to10 + canada_yield_spread30to10 + france_yield_spread30to10 + uk_yield_spread30to10 + india_yield_spread30to10 + us_yield_spread30to10) / 9
avgGlobalYield = math.avg(global_yield_spread10to3m,global_yield_spread10to02,global_yield_spread30to10)

show10to3m = input.bool(false, "Show Global 10Y - 3M Yield Spread")
show10to02 = input.bool(false, "Show Global 10Y - 2Y Yield Spread")
show30to10 = input.bool(false, "Show Global 30Y - 10Y Yield Spread")
showavg = input.bool(false, "Show Average Global Yield Spread")
GlobalYieldFill = input.bool(true, "Fill Yield Spread")

// Plot yield spread
plot(show10to3m?global_yield_spread10to3m:na, color=color.purple, linewidth=3, title="Global 10Y - 3M Yield Spread")
plot(show10to02?global_yield_spread10to02:na, color=color.purple, linewidth=3, title="Global 10Y - 2Y Yield Spread")
plot(show30to10?global_yield_spread30to10:na, color=color.purple, linewidth=3, title="Global 30Y - 10Y Yield Spread")
plot(showavg?avgGlobalYield:na, color=color.purple, linewidth=3, title="Average Global Yield Spread")


fill(plot(show10to3m?global_yield_spread10to3m:na,"", color= color.new(color.black,100)),plot(show10to3m?0:na,"", color= color.new(color.black,100)), color = GlobalYieldFill? global_yield_spread10to3m < 0? color.new(color.red,40): color.new(color.green,60):na)
fill(plot(show10to02?global_yield_spread10to02:na,"", color= color.new(color.black,100)),plot(show10to02?0:na,"", color= color.new(color.black,100)), color = GlobalYieldFill? global_yield_spread10to02 < 0? color.new(color.red,40): color.new(color.green,60):na)
fill(plot(show30to10?global_yield_spread30to10:na,"", color= color.new(color.black,100)),plot(show30to10?0:na,"", color= color.new(color.black,100)), color = GlobalYieldFill? global_yield_spread30to10 < 0? color.new(color.red,40): color.new(color.green,60):na)
fill(plot(showavg?avgGlobalYield:na,"", color= color.new(color.black,100)),plot(showavg?0:na,"", color= color.new(color.black,100)), color = GlobalYieldFill? avgGlobalYield < 0? color.new(color.red,40): color.new(color.green,60):na)

hline(0)
