Global Leaders M2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kikfraben
// Updated last on 27. Oct, 2023
//@version=5

indicator("Global Leaders M2", shorttitle = "GL_M2", overlay = false, precision = 2)

// Get Chart Currency
cur = input.string("USD", title = "Currency",  options = ["USD", "EUR"])

// Get M2 Data of Global Leaders (Top 10)
CN = request.security("ECONOMICS:CNM2", "D", close, currency = cur) // CHINA
US = request.security("ECONOMICS:USM2", "D", close, currency = cur) // USA
JP = request.security("ECONOMICS:JPM2", "D", close, currency = cur) // JAPAN
DE = request.security("ECONOMICS:DEM2", "D", close, currency = cur) // GERMANY
UK = request.security("ECONOMICS:GBM2", "D", close, currency = cur) // UNITED KINGDOM
FR = request.security("ECONOMICS:FRM2", "D", close, currency = cur) // FRANCE
IT = request.security("ECONOMICS:ITM2", "D", close, currency = cur) // ITALY
CA = request.security("ECONOMICS:CAM2", "D", close, currency = cur) // CANADA
RU = request.security("ECONOMICS:RUM2", "D", close, currency = cur) // RUSSIA
IN = request.security("ECONOMICS:INM2", "D", close, currency = cur) // INDIA

// Get Global Leaders M2
GL_M2 = CN + US + JP + DE + UK + FR + IT + CA + RU + IN

// Define Length for Rate of Change
roc_len = input(1, "Rate of Change Length", group = "Rate of Change")

// Get Rate of Change
CN_roc = ta.roc(CN, roc_len)
US_roc = ta.roc(US, roc_len)
JP_roc = ta.roc(JP, roc_len)
DE_roc = ta.roc(DE, roc_len)
UK_roc = ta.roc(UK, roc_len)
FR_roc = ta.roc(FR, roc_len)
IT_roc = ta.roc(IT, roc_len)
CA_roc = ta.roc(CA, roc_len)
RU_roc = ta.roc(RU, roc_len)
IN_roc = ta.roc(IN, roc_len)
GL_roc = ta.roc(GL_M2, roc_len)

// User Inputs
plot_gl = input(true, "Plot Global Leaders M2", group = "Global Leaders Data")
plot_table = input(true, "Plot Overview Table", group = "Global Leaders Data")
plot_cn = input(false, "Plot China M2", group = "Country Specific Data")
plot_us = input(false, "Plot United States M2", group = "Country Specific Data")
plot_jp = input(false, "Plot Japan M2", group = "Country Specific Data")
plot_de = input(false, "Plot Germany M2", group = "Country Specific Data")
plot_uk = input(false, "Plot United Kingdom M2",group = "Country Specific Data")
plot_fr = input(false, "Plot France M2", group = "Country Specific Data")
plot_it = input(false, "Plot Italy M2", group = "Country Specific Data")
plot_ca = input(false, "Plot Canada M2", group = "Country Specific Data")
plot_ru = input(false, "Plot Russia M2", group = "Country Specific Data")
plot_in = input(false, "Plot India M2", group = "Country Specific Data")

// M2 Plots
plot(plot_gl ? GL_M2 : na, color = #3fa8c9)
plot(plot_cn ? CN : na, color = color.red)
plot(plot_us ? US : na, color = color.blue)
plot(plot_jp ? JP : na, color = color.white)
plot(plot_de ? DE : na, color = color.yellow)
plot(plot_uk ? UK : na, color = color.orange)
plot(plot_fr ? FR : na, color = color.lime)
plot(plot_it ? IT : na, color = color.green)
plot(plot_ca ? CA : na, color = color.purple)
plot(plot_ru ? RU : na, color = color.silver)
plot(plot_in ? IN : na, color = color.navy)

// Format Data
cn_val = str.tostring(CN / 1000000000000, "#.0") + "T"
us_val = str.tostring(US / 1000000000000, "#.0") + "T"
jp_val = str.tostring(JP / 1000000000000, "#.0") + "T"
de_val = str.tostring(DE / 1000000000000, "#.0") + "T"
uk_val = str.tostring(UK / 1000000000000, "#.0") + "T"
fr_val = str.tostring(FR / 1000000000000, "#.0") + "T"
it_val = str.tostring(IT / 1000000000000, "#.0") + "T"
ca_val = str.tostring(CA / 1000000000000, "#.0") + "T"
ru_val = str.tostring(RU / 1000000000000, "#.0") + "T"
in_val = str.tostring(IN / 1000000000000, "#.0") + "T"
gl_val = str.tostring(GL_M2 / 1000000000000, "#.0") + "T"

// Color Conditions
up_col = #3fa8c9
down_col = #c93f3f
bg_col = color.new(color.white, 95)
txt_col = color.white
txt_col1 = color.new(color.white, 25)
txt_col2 = color.new(color.green, 35)
cn_col = CN_roc > 0 ? up_col : down_col
us_col = US_roc > 0 ? up_col : down_col
jp_col = JP_roc > 0 ? up_col : down_col
de_col = DE_roc > 0 ? up_col : down_col
uk_col = UK_roc > 0 ? up_col : down_col
fr_col = FR_roc > 0 ? up_col : down_col
it_col = IT_roc > 0 ? up_col : down_col
ca_col = CA_roc > 0 ? up_col : down_col
ru_col = RU_roc > 0 ? up_col : down_col
in_col = IN_roc > 0 ? up_col : down_col
gl_col = GL_roc > 0 ? up_col : down_col

// Get Overview Table
if plot_table == true
    table = table.new(position = position.top_right, columns = 13, rows = 13, frame_color = color.new(color.white, 70),
     border_color = color.new(color.white, 70), frame_width = 2, border_width = 2)

    table.merge_cells(table, 0, 0, 2, 0)
// Layout
    table.cell(table, 0, 0, text = "Global Leaders M2", bgcolor = bg_col, text_color = color.yellow)
    table.cell(table, 0, 1, "Country", bgcolor = bg_col, text_color = txt_col)
    table.cell(table, 1, 1, "Current Value", bgcolor = bg_col, text_color = txt_col, tooltip = "Displays the current value in the predefined currency in cur in trillions")
    table.cell(table, 2, 1, "RoC", bgcolor = bg_col, text_color = txt_col, tooltip = "Displays the Rate of Change of the predefined time horizon in roc_len")
    table.cell(table, 0, 2, "China", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 3, "United States", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 4, "Japan", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 5, "Germany", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 6, "United Kingdom", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 7, "France", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 8, "Italy", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 9, "Canada", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 10, "Russia", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 11, "India", text_color = txt_col1, bgcolor = bg_col)
    table.cell(table, 0, 12, "Total", text_color = color.yellow, bgcolor = bg_col)
// Current Value
    table.cell(table, 1, 2, text = cn_val, text_color = cn_col, bgcolor = bg_col)
    table.cell(table, 1, 3, text = us_val, text_color = us_col, bgcolor = bg_col)
    table.cell(table, 1, 4, text = jp_val, text_color = jp_col, bgcolor = bg_col)
    table.cell(table, 1, 5, text = de_val, text_color = de_col, bgcolor = bg_col)
    table.cell(table, 1, 6, text = uk_val, text_color = uk_col, bgcolor = bg_col)
    table.cell(table, 1, 7, text = fr_val, text_color = fr_col, bgcolor = bg_col)
    table.cell(table, 1, 8, text = it_val, text_color = it_col, bgcolor = bg_col)
    table.cell(table, 1, 9, text = ca_val, text_color = ca_col, bgcolor = bg_col)
    table.cell(table, 1, 10, text = ru_val, text_color = ru_col, bgcolor = bg_col)
    table.cell(table, 1, 11, text = in_val, text_color = in_col, bgcolor = bg_col)
    table.cell(table, 1, 12, text = gl_val, text_color = gl_col, bgcolor = bg_col)
// Rate of Change
    table.cell(table, 2, 2, text = str.tostring(math.round(CN_roc, 2)), text_color = cn_col, bgcolor = bg_col)
    table.cell(table, 2, 3, text = str.tostring(math.round(US_roc, 2)), text_color = us_col, bgcolor = bg_col)
    table.cell(table, 2, 4, text = str.tostring(math.round(JP_roc, 2)), text_color = jp_col, bgcolor = bg_col)
    table.cell(table, 2, 5, text = str.tostring(math.round(DE_roc, 2)), text_color = de_col, bgcolor = bg_col)
    table.cell(table, 2, 6, text = str.tostring(math.round(UK_roc, 2)), text_color = uk_col, bgcolor = bg_col)
    table.cell(table, 2, 7, text = str.tostring(math.round(FR_roc, 2)), text_color = fr_col, bgcolor = bg_col)
    table.cell(table, 2, 8, text = str.tostring(math.round(IT_roc, 2)), text_color = it_col, bgcolor = bg_col)
    table.cell(table, 2, 9, text = str.tostring(math.round(CA_roc, 2)), text_color = ca_col, bgcolor = bg_col)
    table.cell(table, 2, 10, text = str.tostring(math.round(RU_roc, 2)), text_color = ru_col, bgcolor = bg_col)
    table.cell(table, 2, 11, text = str.tostring(math.round(IN_roc, 2)), text_color = in_col, bgcolor = bg_col)
    table.cell(table, 2, 12, text = str.tostring(math.round(GL_roc, 2)), text_color = gl_col, bgcolor = bg_col)


