interest rate gap for forex
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kmassociates

//@version=5
indicator("interest rate gap")
duration = input.int(2, title="yield duration", minval = 1, inline="yield duration")
duration_unit = input.string("Year", title="", options=["Year", "Month"], inline="yield duration")
_duration_unit = duration_unit == "Year"? "": "M"
suffix = duration >= 10? str.tostring(duration) + _duration_unit + "Y": "0" + str.tostring(duration) + _duration_unit + "Y"

ticker01 = str.startswith(syminfo.ticker, 'USD')? 'US':
              str.startswith(syminfo.ticker, 'EUR')? 'EU':
              str.startswith(syminfo.ticker, 'JPY')? 'JP':
              str.startswith(syminfo.ticker, 'AUD')? 'AU':
              str.startswith(syminfo.ticker, 'CAD')? 'CA':
              str.startswith(syminfo.ticker, 'NZD')? 'NZ':
              str.startswith(syminfo.ticker, 'GBP')? 'GB':
              str.startswith(syminfo.ticker, 'CHF')? 'CH':
              str.startswith(syminfo.ticker, 'CNH')? 'CN':
              str.startswith(syminfo.ticker, 'DKK')? 'DK':
              str.startswith(syminfo.ticker, 'HKD')? 'HK':
              str.startswith(syminfo.ticker, 'HUF')? 'HU':
              str.startswith(syminfo.ticker, 'MXN')? 'MX':
              str.startswith(syminfo.ticker, 'NOK')? 'NO':
              str.startswith(syminfo.ticker, 'PLN')? 'PL':
              str.startswith(syminfo.ticker, 'SEK')? 'SE':
              str.startswith(syminfo.ticker, 'SGD')? 'SG':
              str.startswith(syminfo.ticker, 'TRY')? 'TR':
              str.startswith(syminfo.ticker, 'ZAR')? 'ZA':
              str.startswith(syminfo.ticker, 'AED')? 'AE':
              str.startswith(syminfo.ticker, 'AFN')? 'AF':
              str.startswith(syminfo.ticker, 'ALL')? 'AL':
              str.startswith(syminfo.ticker, 'AMD')? 'AM':
              str.startswith(syminfo.ticker, 'AOA')? 'AO':
              str.startswith(syminfo.ticker, 'ARS')? 'AR':
              str.startswith(syminfo.ticker, 'AWG')? 'AW':
              str.startswith(syminfo.ticker, 'AZN')? 'AZ':
              str.startswith(syminfo.ticker, 'BAM')? 'BA':
              str.startswith(syminfo.ticker, 'BBD')? 'BB':
              str.startswith(syminfo.ticker, 'BDT')? 'BD':
              str.startswith(syminfo.ticker, 'BGN')? 'BG':
              str.startswith(syminfo.ticker, 'BHD')? 'BH':
              str.startswith(syminfo.ticker, 'BIF')? 'BI':
              str.startswith(syminfo.ticker, 'BMD')? 'BM':
              str.startswith(syminfo.ticker, 'BND')? 'BN':
              str.startswith(syminfo.ticker, 'BOB')? 'BO':
              str.startswith(syminfo.ticker, 'BRL')? 'BR':
              str.startswith(syminfo.ticker, 'BSD')? 'BS':
              str.startswith(syminfo.ticker, 'BTN')? 'BT':
              str.startswith(syminfo.ticker, 'BWP')? 'BW':
              str.startswith(syminfo.ticker, 'BYN')? 'BY':
              str.startswith(syminfo.ticker, 'BZD')? 'BZ':
              str.startswith(syminfo.ticker, 'CDF')? 'CD':
              str.startswith(syminfo.ticker, 'CHE')? 'CH':
              str.startswith(syminfo.ticker, 'CHW')? 'CH':
              str.startswith(syminfo.ticker, 'CLF')? 'CL':
              str.startswith(syminfo.ticker, 'CLP')? 'CL':
              str.startswith(syminfo.ticker, 'CNY')? 'CN':
              str.startswith(syminfo.ticker, 'COP')? 'CO':
              str.startswith(syminfo.ticker, 'COU')? 'CO':
              str.startswith(syminfo.ticker, 'CRC')? 'CR':
              str.startswith(syminfo.ticker, 'CUC')? 'CU':
              str.startswith(syminfo.ticker, 'CUP')? 'CU':
              str.startswith(syminfo.ticker, 'CVE')? 'CV':
              str.startswith(syminfo.ticker, 'CZK')? 'CZ':
              str.startswith(syminfo.ticker, 'DJF')? 'DJ':
              str.startswith(syminfo.ticker, 'DOP')? 'DO':
              str.startswith(syminfo.ticker, 'DZD')? 'DZ':
              str.startswith(syminfo.ticker, 'EGP')? 'EG':
              str.startswith(syminfo.ticker, 'ERN')? 'ER':
              str.startswith(syminfo.ticker, 'ETB')? 'ET':
              str.startswith(syminfo.ticker, 'FJD')? 'FJ':
              str.startswith(syminfo.ticker, 'FKP')? 'FK':
              str.startswith(syminfo.ticker, 'GEL')? 'GE':
              str.startswith(syminfo.ticker, 'GHS')? 'GH':
              str.startswith(syminfo.ticker, 'GIP')? 'GI':
              str.startswith(syminfo.ticker, 'GMD')? 'GM':
              str.startswith(syminfo.ticker, 'GNF')? 'GN':
              str.startswith(syminfo.ticker, 'GTQ')? 'GT':
              str.startswith(syminfo.ticker, 'GYD')? 'GY':
              str.startswith(syminfo.ticker, 'HNL')? 'HN':
              str.startswith(syminfo.ticker, 'HRK')? 'HR':
              str.startswith(syminfo.ticker, 'HTG')? 'HT':
              str.startswith(syminfo.ticker, 'IDR')? 'ID':
              str.startswith(syminfo.ticker, 'ILS')? 'IL':
              str.startswith(syminfo.ticker, 'INR')? 'IN':
              str.startswith(syminfo.ticker, 'IQD')? 'IQ':
              str.startswith(syminfo.ticker, 'IRR')? 'IR':
              str.startswith(syminfo.ticker, 'ISK')? 'IS':
              str.startswith(syminfo.ticker, 'JMD')? 'JM':
              str.startswith(syminfo.ticker, 'JOD')? 'JO':
              str.startswith(syminfo.ticker, 'KES')? 'KE':
              str.startswith(syminfo.ticker, 'KGS')? 'KG':
              str.startswith(syminfo.ticker, 'KHR')? 'KH':
              str.startswith(syminfo.ticker, 'KMF')? 'KM':
              str.startswith(syminfo.ticker, 'KPW')? 'KP':
              str.startswith(syminfo.ticker, 'KRW')? 'KR':
              str.startswith(syminfo.ticker, 'KWD')? 'KW':
              str.startswith(syminfo.ticker, 'KYD')? 'KY':
              str.startswith(syminfo.ticker, 'KZT')? 'KZ':
              str.startswith(syminfo.ticker, 'LAK')? 'LA':
              str.startswith(syminfo.ticker, 'LBP')? 'LB':
              str.startswith(syminfo.ticker, 'LKR')? 'LK':
              str.startswith(syminfo.ticker, 'LRD')? 'LR':
              str.startswith(syminfo.ticker, 'LSL')? 'LS':
              str.startswith(syminfo.ticker, 'LYD')? 'LY':
              str.startswith(syminfo.ticker, 'MAD')? 'MA':
              str.startswith(syminfo.ticker, 'MDL')? 'MD':
              str.startswith(syminfo.ticker, 'MGA')? 'MG':
              str.startswith(syminfo.ticker, 'MKD')? 'MK':
              str.startswith(syminfo.ticker, 'MMK')? 'MM':
              str.startswith(syminfo.ticker, 'MNT')? 'MN':
              str.startswith(syminfo.ticker, 'MOP')? 'MO':
              str.startswith(syminfo.ticker, 'MRU')? 'MR':
              str.startswith(syminfo.ticker, 'MUR')? 'MU':
              str.startswith(syminfo.ticker, 'MVR')? 'MV':
              str.startswith(syminfo.ticker, 'MWK')? 'MW':
              str.startswith(syminfo.ticker, 'MXV')? 'MX':
              str.startswith(syminfo.ticker, 'MYR')? 'MY':
              str.startswith(syminfo.ticker, 'MZN')? 'MZ':
              str.startswith(syminfo.ticker, 'NAD')? 'NA':
              str.startswith(syminfo.ticker, 'NGN')? 'NG':
              str.startswith(syminfo.ticker, 'NIO')? 'NI':
              str.startswith(syminfo.ticker, 'NPR')? 'NP':
              str.startswith(syminfo.ticker, 'OMR')? 'OM':
              str.startswith(syminfo.ticker, 'PAB')? 'PA':
              str.startswith(syminfo.ticker, 'PEN')? 'PE':
              str.startswith(syminfo.ticker, 'PGK')? 'PG':
              str.startswith(syminfo.ticker, 'PHP')? 'PH':
              str.startswith(syminfo.ticker, 'PKR')? 'PK':
              str.startswith(syminfo.ticker, 'PYG')? 'PY':
              str.startswith(syminfo.ticker, 'QAR')? 'QA':
              str.startswith(syminfo.ticker, 'RON')? 'RO':
              str.startswith(syminfo.ticker, 'RSD')? 'RS':
              str.startswith(syminfo.ticker, 'RUB')? 'RU':
              str.startswith(syminfo.ticker, 'RWF')? 'RW':
              str.startswith(syminfo.ticker, 'SAR')? 'SA':
              str.startswith(syminfo.ticker, 'SBD')? 'SB':
              str.startswith(syminfo.ticker, 'SCR')? 'SC':
              str.startswith(syminfo.ticker, 'SDG')? 'SD':
              str.startswith(syminfo.ticker, 'SHP')? 'SH':
              str.startswith(syminfo.ticker, 'SLL')? 'SL':
              str.startswith(syminfo.ticker, 'SOS')? 'SO':
              str.startswith(syminfo.ticker, 'SRD')? 'SR':
              str.startswith(syminfo.ticker, 'SSP')? 'SS':
              str.startswith(syminfo.ticker, 'STN')? 'ST':
              str.startswith(syminfo.ticker, 'SVC')? 'SV':
              str.startswith(syminfo.ticker, 'SYP')? 'SY':
              str.startswith(syminfo.ticker, 'SZL')? 'SZ':
              str.startswith(syminfo.ticker, 'THB')? 'TH':
              str.startswith(syminfo.ticker, 'TJS')? 'TJ':
              str.startswith(syminfo.ticker, 'TMT')? 'TM':
              str.startswith(syminfo.ticker, 'TND')? 'TN':
              str.startswith(syminfo.ticker, 'TOP')? 'TO':
              str.startswith(syminfo.ticker, 'TTD')? 'TT':
              str.startswith(syminfo.ticker, 'TZS')? 'TZ':
              str.startswith(syminfo.ticker, 'UAH')? 'UA':
              str.startswith(syminfo.ticker, 'UGX')? 'UG':
              str.startswith(syminfo.ticker, 'UYI')? 'UY':
              str.startswith(syminfo.ticker, 'UYU')? 'UY':
              str.startswith(syminfo.ticker, 'UYW')? 'UY':
              str.startswith(syminfo.ticker, 'UZS')? 'UZ':
              str.startswith(syminfo.ticker, 'VND')? 'VN':
              str.startswith(syminfo.ticker, 'VUV')? 'VU':
              str.startswith(syminfo.ticker, 'WST')? 'WS':
              str.startswith(syminfo.ticker, 'YER')? 'YE':
              str.startswith(syminfo.ticker, 'ZMW')? 'ZM':
              str.startswith(syminfo.ticker, 'ZWL')? 'ZW':""
ticker01 += ticker01 == ""? "": suffix
ticker02 = str.endswith(syminfo.ticker, 'USD')? 'US':
              str.endswith(syminfo.ticker, 'EUR')? 'EU':
              str.endswith(syminfo.ticker, 'JPY')? 'JP':
              str.endswith(syminfo.ticker, 'AUD')? 'AU':
              str.endswith(syminfo.ticker, 'CAD')? 'CA':
              str.endswith(syminfo.ticker, 'NZD')? 'NZ':
              str.endswith(syminfo.ticker, 'GBP')? 'GB':
              str.endswith(syminfo.ticker, 'CHF')? 'CH':
              str.endswith(syminfo.ticker, 'CNH')? 'CN':
              str.endswith(syminfo.ticker, 'DKK')? 'DK':
              str.endswith(syminfo.ticker, 'HKD')? 'HK':
              str.endswith(syminfo.ticker, 'HUF')? 'HU':
              str.endswith(syminfo.ticker, 'MXN')? 'MX':
              str.endswith(syminfo.ticker, 'NOK')? 'NO':
              str.endswith(syminfo.ticker, 'PLN')? 'PL':
              str.endswith(syminfo.ticker, 'SEK')? 'SE':
              str.endswith(syminfo.ticker, 'SGD')? 'SG':
              str.endswith(syminfo.ticker, 'TRY')? 'TR':
              str.endswith(syminfo.ticker, 'ZAR')? 'ZA':
              str.endswith(syminfo.ticker, 'AED')? 'AE':
              str.endswith(syminfo.ticker, 'AFN')? 'AF':
              str.endswith(syminfo.ticker, 'ALL')? 'AL':
              str.endswith(syminfo.ticker, 'AMD')? 'AM':
              str.endswith(syminfo.ticker, 'AOA')? 'AO':
              str.endswith(syminfo.ticker, 'ARS')? 'AR':
              str.endswith(syminfo.ticker, 'AWG')? 'AW':
              str.endswith(syminfo.ticker, 'AZN')? 'AZ':
              str.endswith(syminfo.ticker, 'BAM')? 'BA':
              str.endswith(syminfo.ticker, 'BBD')? 'BB':
              str.endswith(syminfo.ticker, 'BDT')? 'BD':
              str.endswith(syminfo.ticker, 'BGN')? 'BG':
              str.endswith(syminfo.ticker, 'BHD')? 'BH':
              str.endswith(syminfo.ticker, 'BIF')? 'BI':
              str.endswith(syminfo.ticker, 'BMD')? 'BM':
              str.endswith(syminfo.ticker, 'BND')? 'BN':
              str.endswith(syminfo.ticker, 'BOB')? 'BO':
              str.endswith(syminfo.ticker, 'BRL')? 'BR':
              str.endswith(syminfo.ticker, 'BSD')? 'BS':
              str.endswith(syminfo.ticker, 'BTN')? 'BT':
              str.endswith(syminfo.ticker, 'BWP')? 'BW':
              str.endswith(syminfo.ticker, 'BYN')? 'BY':
              str.endswith(syminfo.ticker, 'BZD')? 'BZ':
              str.endswith(syminfo.ticker, 'CDF')? 'CD':
              str.endswith(syminfo.ticker, 'CHE')? 'CH':
              str.endswith(syminfo.ticker, 'CHW')? 'CH':
              str.endswith(syminfo.ticker, 'CLF')? 'CL':
              str.endswith(syminfo.ticker, 'CLP')? 'CL':
              str.endswith(syminfo.ticker, 'CNY')? 'CN':
              str.endswith(syminfo.ticker, 'COP')? 'CO':
              str.endswith(syminfo.ticker, 'COU')? 'CO':
              str.endswith(syminfo.ticker, 'CRC')? 'CR':
              str.endswith(syminfo.ticker, 'CUC')? 'CU':
              str.endswith(syminfo.ticker, 'CUP')? 'CU':
              str.endswith(syminfo.ticker, 'CVE')? 'CV':
              str.endswith(syminfo.ticker, 'CZK')? 'CZ':
              str.endswith(syminfo.ticker, 'DJF')? 'DJ':
              str.endswith(syminfo.ticker, 'DOP')? 'DO':
              str.endswith(syminfo.ticker, 'DZD')? 'DZ':
              str.endswith(syminfo.ticker, 'EGP')? 'EG':
              str.endswith(syminfo.ticker, 'ERN')? 'ER':
              str.endswith(syminfo.ticker, 'ETB')? 'ET':
              str.endswith(syminfo.ticker, 'FJD')? 'FJ':
              str.endswith(syminfo.ticker, 'FKP')? 'FK':
              str.endswith(syminfo.ticker, 'GEL')? 'GE':
              str.endswith(syminfo.ticker, 'GHS')? 'GH':
              str.endswith(syminfo.ticker, 'GIP')? 'GI':
              str.endswith(syminfo.ticker, 'GMD')? 'GM':
              str.endswith(syminfo.ticker, 'GNF')? 'GN':
              str.endswith(syminfo.ticker, 'GTQ')? 'GT':
              str.endswith(syminfo.ticker, 'GYD')? 'GY':
              str.endswith(syminfo.ticker, 'HNL')? 'HN':
              str.endswith(syminfo.ticker, 'HRK')? 'HR':
              str.endswith(syminfo.ticker, 'HTG')? 'HT':
              str.endswith(syminfo.ticker, 'IDR')? 'ID':
              str.endswith(syminfo.ticker, 'ILS')? 'IL':
              str.endswith(syminfo.ticker, 'INR')? 'IN':
              str.endswith(syminfo.ticker, 'IQD')? 'IQ':
              str.endswith(syminfo.ticker, 'IRR')? 'IR':
              str.endswith(syminfo.ticker, 'ISK')? 'IS':
              str.endswith(syminfo.ticker, 'JMD')? 'JM':
              str.endswith(syminfo.ticker, 'JOD')? 'JO':
              str.endswith(syminfo.ticker, 'KES')? 'KE':
              str.endswith(syminfo.ticker, 'KGS')? 'KG':
              str.endswith(syminfo.ticker, 'KHR')? 'KH':
              str.endswith(syminfo.ticker, 'KMF')? 'KM':
              str.endswith(syminfo.ticker, 'KPW')? 'KP':
              str.endswith(syminfo.ticker, 'KRW')? 'KR':
              str.endswith(syminfo.ticker, 'KWD')? 'KW':
              str.endswith(syminfo.ticker, 'KYD')? 'KY':
              str.endswith(syminfo.ticker, 'KZT')? 'KZ':
              str.endswith(syminfo.ticker, 'LAK')? 'LA':
              str.endswith(syminfo.ticker, 'LBP')? 'LB':
              str.endswith(syminfo.ticker, 'LKR')? 'LK':
              str.endswith(syminfo.ticker, 'LRD')? 'LR':
              str.endswith(syminfo.ticker, 'LSL')? 'LS':
              str.endswith(syminfo.ticker, 'LYD')? 'LY':
              str.endswith(syminfo.ticker, 'MAD')? 'MA':
              str.endswith(syminfo.ticker, 'MDL')? 'MD':
              str.endswith(syminfo.ticker, 'MGA')? 'MG':
              str.endswith(syminfo.ticker, 'MKD')? 'MK':
              str.endswith(syminfo.ticker, 'MMK')? 'MM':
              str.endswith(syminfo.ticker, 'MNT')? 'MN':
              str.endswith(syminfo.ticker, 'MOP')? 'MO':
              str.endswith(syminfo.ticker, 'MRU')? 'MR':
              str.endswith(syminfo.ticker, 'MUR')? 'MU':
              str.endswith(syminfo.ticker, 'MVR')? 'MV':
              str.endswith(syminfo.ticker, 'MWK')? 'MW':
              str.endswith(syminfo.ticker, 'MXV')? 'MX':
              str.endswith(syminfo.ticker, 'MYR')? 'MY':
              str.endswith(syminfo.ticker, 'MZN')? 'MZ':
              str.endswith(syminfo.ticker, 'NAD')? 'NA':
              str.endswith(syminfo.ticker, 'NGN')? 'NG':
              str.endswith(syminfo.ticker, 'NIO')? 'NI':
              str.endswith(syminfo.ticker, 'NPR')? 'NP':
              str.endswith(syminfo.ticker, 'OMR')? 'OM':
              str.endswith(syminfo.ticker, 'PAB')? 'PA':
              str.endswith(syminfo.ticker, 'PEN')? 'PE':
              str.endswith(syminfo.ticker, 'PGK')? 'PG':
              str.endswith(syminfo.ticker, 'PHP')? 'PH':
              str.endswith(syminfo.ticker, 'PKR')? 'PK':
              str.endswith(syminfo.ticker, 'PYG')? 'PY':
              str.endswith(syminfo.ticker, 'QAR')? 'QA':
              str.endswith(syminfo.ticker, 'RON')? 'RO':
              str.endswith(syminfo.ticker, 'RSD')? 'RS':
              str.endswith(syminfo.ticker, 'RUB')? 'RU':
              str.endswith(syminfo.ticker, 'RWF')? 'RW':
              str.endswith(syminfo.ticker, 'SAR')? 'SA':
              str.endswith(syminfo.ticker, 'SBD')? 'SB':
              str.endswith(syminfo.ticker, 'SCR')? 'SC':
              str.endswith(syminfo.ticker, 'SDG')? 'SD':
              str.endswith(syminfo.ticker, 'SHP')? 'SH':
              str.endswith(syminfo.ticker, 'SLL')? 'SL':
              str.endswith(syminfo.ticker, 'SOS')? 'SO':
              str.endswith(syminfo.ticker, 'SRD')? 'SR':
              str.endswith(syminfo.ticker, 'SSP')? 'SS':
              str.endswith(syminfo.ticker, 'STN')? 'ST':
              str.endswith(syminfo.ticker, 'SVC')? 'SV':
              str.endswith(syminfo.ticker, 'SYP')? 'SY':
              str.endswith(syminfo.ticker, 'SZL')? 'SZ':
              str.endswith(syminfo.ticker, 'THB')? 'TH':
              str.endswith(syminfo.ticker, 'TJS')? 'TJ':
              str.endswith(syminfo.ticker, 'TMT')? 'TM':
              str.endswith(syminfo.ticker, 'TND')? 'TN':
              str.endswith(syminfo.ticker, 'TOP')? 'TO':
              str.endswith(syminfo.ticker, 'TTD')? 'TT':
              str.endswith(syminfo.ticker, 'TZS')? 'TZ':
              str.endswith(syminfo.ticker, 'UAH')? 'UA':
              str.endswith(syminfo.ticker, 'UGX')? 'UG':
              str.endswith(syminfo.ticker, 'UYI')? 'UY':
              str.endswith(syminfo.ticker, 'UYU')? 'UY':
              str.endswith(syminfo.ticker, 'UYW')? 'UY':
              str.endswith(syminfo.ticker, 'UZS')? 'UZ':
              str.endswith(syminfo.ticker, 'VND')? 'VN':
              str.endswith(syminfo.ticker, 'VUV')? 'VU':
              str.endswith(syminfo.ticker, 'WST')? 'WS':
              str.endswith(syminfo.ticker, 'YER')? 'YE':
              str.endswith(syminfo.ticker, 'ZMW')? 'ZM':
              str.endswith(syminfo.ticker, 'ZWL')? 'ZW':""
ticker02 += ticker02 == ""? "": suffix

sym01 = request.security(ticker01, timeframe.period, close)
sym02 = request.security(ticker02, timeframe.period, close)
irg = sym01 - sym02

plot(syminfo.type == "forex"? irg: na, title="interest rate gap")

bool_ma = input.bool(true, title="SMA", inline="MA")
len_ma = input.int(20, title="length", inline="MA")
plot(syminfo.type == "forex" and bool_ma? ta.sma(irg, len_ma): na, title="SMA", color=color.olive)

bool_zero = input.bool(true, title="Zero line", inline="zero")
hline(bool_zero? 0.0: na, title="zero line", color=color.gray, linestyle=hline.style_dotted)


show_notes = input.bool(true, title="show_notes")
var tbl = table.new(position = position.top_right, columns = 1, rows = 2, bgcolor = color.new(color.gray, 80), border_width = 1)
if barstate.islast and show_notes
    if syminfo.type == "forex"
        table.cell(tbl, column = 0, row = 0, text = ticker01 + " - " + ticker02, text_color = color.white, text_size=size.small)
        tbl_duration_unit = duration_unit == "Year"? "year": "month"
        table.cell(tbl, column = 0, row = 1, text = syminfo.description + "\n" + str.tostring(duration) + " " + tbl_duration_unit + " gov bonds yield gap", text_color = color.white, text_size=size.small)
    if syminfo.type != "forex"
        table.cell(tbl, column = 0, row = 0, text = "symbol is not forex", text_color = color.white, text_size=size.small)
        table.set_position(tbl, position.middle_center)


