Gann Square 4 Cross Cardinal Table Concept
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RozaniGhani-RG
// Inspired by The Tunnel Thru The Air Or Looking Back From 1940, written by WD Gann
// Page 195 - 198


//@version=4
study("Gann Square 4 Cross Cardinal Table Concept", shorttitle = "GS4CCTC_SC")


// 0. Arrays
// 1. Variables
// 2. Custom functions
// 3. if statament
// 4. Inputs


// 0. Arrays
//                            0   1   2   3   4    5    6    7    8    9
arr_cardinal_45  = array.from(1,  6, 19, 40, 69, 106, 151, 204, 265, 334)
arr_cardinal_135 = array.from(2,  9, 24, 47, 78, 117, 164, 219, 282, 353)
arr_cardinal_225 = array.from(3, 12, 29, 54, 87, 128, 177, 234, 299, 372)
arr_cardinal_315 = array.from(4, 15, 34, 61, 96, 139, 190, 249, 316, 391)
// sort array
array.sort(arr_cardinal_45, order.descending)


// 1. Variables
G1 = "Cross Cardinal"
var testTable = table.new(position = position.middle_center, columns = 21, rows = 21, bgcolor = color.black, border_width = 1)
input_font = input("Small", "Font Size",options = ["Tiny", "Small", "Normal", "Large", "Huge"])
font   = input_font == "Tiny"  ? size.tiny  :
         input_font == "Small" ? size.small :
         input_font == "Large" ? size.large :
         input_font == "Huge"  ? size.huge  :
         size.normal


// 2. Custom functions
cell_default(_column, _row, _id, _index)=>
    table.cell(testTable, _column, _row, tostring(array.get(_id, _index)), bgcolor = color.gray, text_color = color.black, text_size = font)
fun_set(_input, _column, _row, _color) =>
    if _input
        table.cell_set_bgcolor(testTable, _column, _row, _color)


// 3. if statament
if barstate.islast
    for _id = 0 to 9
        cell_default(     _id,      _id, arr_cardinal_45,  _id)
        cell_default(11 + _id,  9 - _id, arr_cardinal_135, _id)
        cell_default(11 + _id, 11 + _id, arr_cardinal_225, _id)
        cell_default(9  - _id, 11 + _id, arr_cardinal_315, _id)


// 4. Inputs
c0  = input(color.red,     "", group = G1, inline =  "0")
c1  = input(color.orange,  "", group = G1, inline =  "1")
c2  = input(color.yellow,  "", group = G1, inline =  "2")
c3  = input(color.green,   "", group = G1, inline =  "3")
c4  = input(color.teal,    "", group = G1, inline =  "4")
c5  = input(color.aqua,    "", group = G1, inline =  "5")
c6  = input(color.blue,    "", group = G1, inline =  "6")
c7  = input(color.purple,  "", group = G1, inline =  "7")
c8  = input(color.fuchsia, "", group = G1, inline =  "8")
c9  = input(#FFC0CB,       "", group = G1, inline =  "9")

// arr_cardinal_45
cardinal_45_0 = input(false, "1  ", group = G1, inline = "0"), fun_set(cardinal_45_0, 9, 9, c0)
cardinal_45_1 = input(false, "6  ", group = G1, inline = "1"), fun_set(cardinal_45_1, 8, 8, c1)
cardinal_45_2 = input(false, "19 ", group = G1, inline = "2"), fun_set(cardinal_45_2, 7, 7, c2)
cardinal_45_3 = input(false, "40 ", group = G1, inline = "3"), fun_set(cardinal_45_3, 6, 6, c3)
cardinal_45_4 = input(false, "69 ", group = G1, inline = "4"), fun_set(cardinal_45_4, 5, 5, c4)
cardinal_45_5 = input(false, "106", group = G1, inline = "5"), fun_set(cardinal_45_5, 4, 4, c5)
cardinal_45_6 = input(false, "151", group = G1, inline = "6"), fun_set(cardinal_45_6, 3, 3, c6)
cardinal_45_7 = input(false, "204", group = G1, inline = "7"), fun_set(cardinal_45_7, 2, 2, c7)
cardinal_45_8 = input(false, "265", group = G1, inline = "8"), fun_set(cardinal_45_8, 1, 1, c8)
cardinal_45_9 = input(false, "334", group = G1, inline = "9"), fun_set(cardinal_45_9, 0, 0, c9)

// arr_cardinal_135
cardinal_135_0 = input(false, "2  ", group = G1, inline = "0"), fun_set(cardinal_135_0, 11, 9, c0)
cardinal_135_1 = input(false, "9  ", group = G1, inline = "1"), fun_set(cardinal_135_1, 12, 8, c1)
cardinal_135_2 = input(false, "24 ", group = G1, inline = "2"), fun_set(cardinal_135_2, 13, 7, c2)
cardinal_135_3 = input(false, "47 ", group = G1, inline = "3"), fun_set(cardinal_135_3, 14, 6, c3)
cardinal_135_4 = input(false, "78 ", group = G1, inline = "4"), fun_set(cardinal_135_4, 15, 5, c4)
cardinal_135_5 = input(false, "117", group = G1, inline = "5"), fun_set(cardinal_135_5, 16, 4, c5)
cardinal_135_6 = input(false, "164", group = G1, inline = "6"), fun_set(cardinal_135_6, 17, 3, c6)
cardinal_135_7 = input(false, "219", group = G1, inline = "7"), fun_set(cardinal_135_7, 18, 2, c7)
cardinal_135_8 = input(false, "282", group = G1, inline = "8"), fun_set(cardinal_135_8, 19, 1, c8)
cardinal_135_9 = input(false, "353", group = G1, inline = "9"), fun_set(cardinal_135_9, 20, 0, c9)

// arr_cardinal_225
cardinal_225_0 = input(false, "3  ", group = G1, inline = "0"), fun_set(cardinal_225_0, 11, 11, c0)
cardinal_225_1 = input(false, "12 ", group = G1, inline = "1"), fun_set(cardinal_225_1, 12, 12, c1)
cardinal_225_2 = input(false, "29 ", group = G1, inline = "2"), fun_set(cardinal_225_2, 13, 13, c2)
cardinal_225_3 = input(false, "54 ", group = G1, inline = "3"), fun_set(cardinal_225_3, 14, 14, c3)
cardinal_225_4 = input(false, "87 ", group = G1, inline = "4"), fun_set(cardinal_225_4, 15, 15, c4)
cardinal_225_5 = input(false, "128", group = G1, inline = "5"), fun_set(cardinal_225_5, 16, 16, c5)
cardinal_225_6 = input(false, "177", group = G1, inline = "6"), fun_set(cardinal_225_6, 17, 17, c6)
cardinal_225_7 = input(false, "234", group = G1, inline = "7"), fun_set(cardinal_225_7, 18, 18, c7)
cardinal_225_8 = input(false, "299", group = G1, inline = "8"), fun_set(cardinal_225_8, 19, 19, c8)
cardinal_225_9 = input(false, "372", group = G1, inline = "9"), fun_set(cardinal_225_9, 20, 20, c9)

// arr_cardinal_315
cardinal_315_0 = input(false, "4  ", group = G1, inline = "0"), fun_set(cardinal_315_0, 9, 11, c0)
cardinal_315_1 = input(false, "15 ", group = G1, inline = "1"), fun_set(cardinal_315_1, 8, 12, c1)
cardinal_315_2 = input(false, "34 ", group = G1, inline = "2"), fun_set(cardinal_315_2, 7, 13, c2)
cardinal_315_3 = input(false, "61 ", group = G1, inline = "3"), fun_set(cardinal_315_3, 6, 14, c3)
cardinal_315_4 = input(false, "96 ", group = G1, inline = "4"), fun_set(cardinal_315_4, 5, 15, c4)
cardinal_315_5 = input(false, "139", group = G1, inline = "5"), fun_set(cardinal_315_5, 4, 16, c5)
cardinal_315_6 = input(false, "190", group = G1, inline = "6"), fun_set(cardinal_315_6, 3, 17, c6)
cardinal_315_7 = input(false, "249", group = G1, inline = "7"), fun_set(cardinal_315_7, 2, 18, c7)
cardinal_315_8 = input(false, "316", group = G1, inline = "8"), fun_set(cardinal_315_8, 1, 19, c8)
cardinal_315_9 = input(false, "391", group = G1, inline = "9"), fun_set(cardinal_315_9, 0, 20, c9)