International [interest, exchange] rates 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Bill_Howell


//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// 19Sep2022 initial, 20Sep2022 fixed colored labels, added min,max from my previous scripts
// see https://www.tradingview.com/script/xOLBArA6-International-interest-exchange-rates/

//	All manually-provided constants for del[Min, Max]_<sym>10Y will become inaccurate with time!!
// ONLY time.periods [3M, 6M, 1Y, 5Y] work.  
// time.periods [1D, 5D, 1M, ALL] do NOT work - I haven't manually provided values from charts.
// there are some errors in my manually [read, record] of the 5Y time-periods
// additionally, this Pine Script was thrown together in a day, no rigorous testing

// exchange rates are shown by darker colors, interest rates lighter similar colors
// China - green, Euro - purple, Great Britain - blue,  Japan - orange   (you can see this in the4 code below!) 
// stable rates :  [JP10Y, CN10Y]
//		https://www.tradingview.com/x/OXFvIi7I/
// policy-manipulated rates :  [TNX, EU10Y, GB10Y]  
//		https://www.tradingview.com/x/V4LOppCz/


//**********************************
study(title="International 10Y rates minus US", shorttitle=" ", overlay=true, resolution="")


//+-----------------------------------------------------------------+
//+-----------------------------------------------------------------+
// Code in this section was taken from my earlier PineScripts 
//		(eg "SP500 [Puetz time, Fibonacci price]Fractals [trend, relStdDev] 1926-2020")




//+-----+
// https://www.tradingview.com/pine-script-docs/en/v4/appendix/HOWTOs.html#count-bars-in-a-dataset
// Find the highest and lowest values for the entire dataset
// 20Mar2021 Howell - adapt this for viewable data

// 20Sep2022 WARNING!!  
// PineScript fails at providing functions that work over the full length of the visible stock period.  
//		One cannot accurately determine the start time bar
//		functions like [big, small]est below give WRONG results!!  - best to hand-craft answers


biggest(series,size) =>
	max =  0.0
	for j = 0 to size
		if series[j] > max 
			max := series[j]
	max

smalest(series,size) =>
	min =  100000.  
	for j = size to 0
		if series[j] < min 
			min := series[j]
	min

//+-----+
// Setup - timeframe.period, n_bars, [min, max] of tracked main price series in chart
// User must turn OFF : SP500 axis menu -> Labels -> Indicators and financials name labels (no checkmark)


// timeIdx = index to ary_t_length, the active chart timeSpan 
// t_lengthYear =  duration of graph timescale for each timeperiod
// visual  = "1D"			 "5D"			 "1M"			"3M"		"6M"	 "1Y"		"5Y"	"All"  	 	
// actual  =  1    		 1      		 30			120		D		  W		30? or 20?
//qnial> 	 (1/365.25)  (5/365.25)  (1/12)		(3/12)	(1/2)  (1.0)  (5.0)	(20) 
//  			 .00273785	 0.0136893   0.0833333	0.25 		0.5 	 1. 		5.		 20.

var int	 timeIdx =  0		// index to UWS constants

// 20Sep2022 NOTE :  hand-derived values below!!!  (Pine Script no good for these kinds of things)
// Formulae don't work in PineScript for full time-period 
// if			(timeframe.period == "1") 
// 	timeIdx :=  0
// else if	(timeframe.period == "5") 
// 	timeIdx :=  1
// else if	(timeframe.period == "30") 
// 	timeIdx :=  2
// else if	(timeframe.period == "60") 
// 	timeIdx :=  3
// else if	(timeframe.period == "120") 
// 	timeIdx :=  4
// else if	(timeframe.period == "D") 
// 	timeIdx :=  5
// else if	(timeframe.period == "W") 
// 	timeIdx :=  6
// else if	(timeframe.period == "M") 
// 	timeIdx :=  7


delMin_CN10Y  =  0. 
delMin_EU10Y  =  0. 
delMin_GB10Y  =  0. 
delMin_JP10Y  =  0. 

delMax_CN10Y  =  1.
delMax_EU10Y  =  1.
delMax_GB10Y  =  1.
delMax_JP10Y  =  1.

// hand-crafted off chart, typed in!!  To read the values : 
//		set params like this for a given time.period 
//		[save, run] script
//		enter [min, max] values shown on graph for the given time.period of : 
//			d_CN10Y = (v_TNX - v_CN10Y - delMin_CN10Y)/(delMax_CN10Y - delMin_CN10Y)
//					  = (v_TNX - v_CN10Y)
//					  = del_CN10Y
// YES - this is STUPID work that PineScript should do well...
//	delMin_CN10Y  :=  0.   
//	delMin_EU10Y  :=  0.  
//	delMin_GB10Y  :=  0.  
//	delMin_JP10Y  :=  0.  
//	delMax_CN10Y  :=  1.    
//	delMax_EU10Y  :=  1.  
//	delMax_GB10Y  :=  1.  


if			(timeframe.period == "1")		// 1 day   
	delMin_CN10Y  :=   0.   
	delMin_EU10Y  :=   0.  
	delMin_GB10Y  :=   0.  
	delMin_JP10Y  :=   0.  
	delMax_CN10Y  :=   1.    
	delMax_EU10Y  :=   1.  
	delMax_GB10Y  :=   1.  
	delMax_JP10Y  :=   1.  
else if	(timeframe.period == "5")		// 5 days  
	delMin_CN10Y  :=   0.   
	delMin_EU10Y  :=   0.  
	delMin_GB10Y  :=   0.  
	delMin_JP10Y  :=   0.  
	delMax_CN10Y  :=   1.    
	delMax_EU10Y  :=   1.  
	delMax_GB10Y  :=   1.  
	delMax_JP10Y  :=   1.  
else if	(timeframe.period == "30")		// 1 month  
	delMin_CN10Y  :=   0.   
	delMin_EU10Y  :=   0.  
	delMin_GB10Y  :=   0.  
	delMin_JP10Y  :=   0.  
	delMax_CN10Y  :=   1.    
	delMax_EU10Y  :=   1.  
	delMax_GB10Y  :=   1.  
	delMax_JP10Y  :=   1.  
else if	(timeframe.period == "60")		// 3 months  
	delMin_CN10Y  :=  -0.158 
	delMin_EU10Y  :=   1.452 
	delMin_GB10Y  :=   0.110
	delMin_JP10Y  :=   2.412
	delMax_CN10Y  :=   0.920   
	delMax_EU10Y  :=   1.951  
	delMax_GB10Y  :=   0.986
	delMax_JP10Y  :=   3.336
else if	(timeframe.period == "120")	// 6 months 
	delMin_CN10Y  :=  -0.664 
	delMin_EU10Y  :=   1.455
	delMin_GB10Y  :=   0.109  
	delMin_JP10Y  :=   1.946 
	delMax_CN10Y  :=   0.910
	delMax_EU10Y  :=   2.084
	delMax_GB10Y  :=   1.247 
	delMax_JP10Y  :=   3.338  
else if	(timeframe.period == "D")		// 1 year 
	delMin_CN10Y  :=  -1.559   
	delMin_EU10Y  :=   1.483  
	delMin_GB10Y  :=   0.127  
	delMin_JP10Y  :=   1.261  
	delMax_CN10Y  :=   0.815    
	delMax_EU10Y  :=   2.029  
	delMax_GB10Y  :=   1.189  
	delMax_JP10Y  :=   3.252  
else if	(timeframe.period == "W")		// 5 years
	delMin_CN10Y  :=  -0.397   
	delMin_EU10Y  :=  -0.854  
	delMin_GB10Y  :=   0.084  
	delMin_JP10Y  :=  -0.371  
	delMax_CN10Y  :=   1.041    
	delMax_EU10Y  :=   2.374  
	delMax_GB10Y  :=   1.624  
	delMax_JP10Y  :=   1.034  
else if	(timeframe.period == "M")		// ALL years 
	delMin_CN10Y  :=  0.   
	delMin_EU10Y  :=  0.  
	delMin_GB10Y  :=  0.  
	delMin_JP10Y  :=  0.  
	delMax_CN10Y  :=  1.    
	delMax_EU10Y  :=  1.  
	delMax_GB10Y  :=  1.  
	delMax_JP10Y  :=  1.  



//+-----------------------------------------------------------------+
//+-----------------------------------------------------------------+
// Code adapted from my previous PineScript


v_TNX   = security("TVC:TNX",   timeframe.period, close)
v_CN10Y = security("TVC:CN10Y", timeframe.period, close)
v_EU10Y = security("TVC:EU10Y", timeframe.period, close)
v_GB10Y = security("TVC:GB10Y", timeframe.period, close)
v_JP10Y = security("TVC:JP10Y", timeframe.period, close)


del_CN10Y = v_TNX - v_CN10Y
del_EU10Y = v_TNX - v_EU10Y
del_GB10Y = v_TNX - v_GB10Y
del_JP10Y = v_TNX - v_JP10Y


// 20Sep2022 Cannot use - give wrong results : 
// delMin_CN10Y  =  smalest(del_CN10Y,n_bars) 
// delMin_EU10Y  =  smalest(del_EU10Y,n_bars)  
// delMin_GB10Y  =  smalest(del_GB10Y,n_bars)  
// delMin_JP10Y  =  smalest(del_JP10Y,n_bars)  
// 
// delMax_CN10Y  =  biggest(del_CN10Y,n_bars) 
// delMax_EU10Y  =  biggest(del_EU10Y,n_bars) 
// delMax_GB10Y  =  biggest(del_GB10Y,n_bars) 
// delMax_JP10Y  =  biggest(del_JP10Y,n_bars) 


d_CN10Y = (v_TNX - v_CN10Y - delMin_CN10Y)/(delMax_CN10Y - delMin_CN10Y)
d_EU10Y = (v_TNX - v_EU10Y - delMin_EU10Y)/(delMax_EU10Y - delMin_EU10Y)
d_GB10Y = (v_TNX - v_GB10Y - delMin_GB10Y)/(delMax_GB10Y - delMin_GB10Y)
d_JP10Y = (v_TNX - v_JP10Y - delMin_JP10Y)/(delMax_JP10Y - delMin_JP10Y)


// standard named colors : 
// https://www.tradingview.com/pine-script-reference/v5/#var_color{dot}black
// "see also" list of named colors
// 	color.[black, silver, gray, white, red, purple, fuchsia, green, lime, olive, yellow, navy, blue, teal, aqua, orange]

plot(d_CN10Y, color=color.lime,		linewidth=1, title="d_CN10Y")		// go with green
plot(d_EU10Y, color=color.fuchsia,	linewidth=1, title="d_EU10Y")		// purple
plot(d_GB10Y, color=color.aqua,		linewidth=1, title="d_GB10Y")		// blue
plot(d_JP10Y, color=color.yellow,	linewidth=2, title="d_JP10Y")		// orange



// endcode
