Gann Square 9 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


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


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


// 0. Arrays
//                            0   1   2   3    4    5    6    7    8
arr_cross_0      = array.from(2, 11, 28, 53,  86, 127, 176, 233, 298)
arr_cross_90     = array.from(4, 15, 34, 61,  96, 139, 190, 249, 316)
arr_cross_180    = array.from(6, 19, 40, 69, 106, 151, 204, 265, 334)
arr_cross_270    = array.from(8, 23, 46, 77, 116, 163, 218, 281, 352)
arr_cardinal_45  = array.from(3, 13, 31, 57,  91, 133, 183, 241, 307)
arr_cardinal_135 = array.from(5, 17, 37, 65, 101, 145, 197, 257, 325)
arr_cardinal_225 = array.from(7, 21, 43, 73, 111, 157, 211, 273, 343)
arr_cardinal_315 = array.from(9, 25, 49, 81, 121, 169, 225, 289, 361)
// sort array
array.sort(arr_cross_0,     order.descending)
array.sort(arr_cardinal_45, order.descending)
array.sort(arr_cross_90,    order.descending)


// 1. Variables
G0 = "Cross", G1 = "Cross Cardinal"
var testTable = table.new(position = position.middle_center, columns = 21, rows = 21, bgcolor = color.black, border_width = 1)
input_font = input("Normal", "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 8
        cell_default(     _id,        9, arr_cross_0,      _id)
        cell_default(     _id,      _id, arr_cardinal_45,  _id)
        cell_default(      10,      _id, arr_cross_90,     _id)
        cell_default(11 + _id,  8 - _id, arr_cardinal_135, _id)
        cell_default(11 + _id,        9, arr_cross_180,    _id)
        cell_default(11 + _id, 11 + _id, arr_cardinal_225, _id)
        cell_default(      10, 11 + _id, arr_cross_270,    _id)
        cell_default(8  - _id, 11 + _id, arr_cardinal_315, _id)


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

c9  = input(color.red,     "", group = G1, inline =  "9")
c10 = input(color.orange,  "", group = G1, inline = "10")
c11 = input(color.yellow,  "", group = G1, inline = "11")
c12 = input(color.green,   "", group = G1, inline = "12")
c13 = input(color.teal,    "", group = G1, inline = "13")
c14 = input(color.aqua,    "", group = G1, inline = "14")
c15 = input(color.blue,    "", group = G1, inline = "15")
c16 = input(color.purple,  "", group = G1, inline = "16")
c17 = input(color.fuchsia, "", group = G1, inline = "17")

// arr_cross_0
cross_0_0 = input(false, "2  ", group = G0, inline = "0"), fun_set(cross_0_0, 8, 9, c0)
cross_0_1 = input(false, "11 ", group = G0, inline = "1"), fun_set(cross_0_1, 7, 9, c1)
cross_0_2 = input(false, "28 ", group = G0, inline = "2"), fun_set(cross_0_2, 6, 9, c2)
cross_0_3 = input(false, "53 ", group = G0, inline = "3"), fun_set(cross_0_3, 5, 9, c3)
cross_0_4 = input(false, "86 ", group = G0, inline = "4"), fun_set(cross_0_4, 4, 9, c4)
cross_0_5 = input(false, "127", group = G0, inline = "5"), fun_set(cross_0_5, 3, 9, c5)
cross_0_6 = input(false, "176", group = G0, inline = "6"), fun_set(cross_0_6, 2, 9, c6)
cross_0_7 = input(false, "233", group = G0, inline = "7"), fun_set(cross_0_7, 1, 9, c7)
cross_0_8 = input(false, "298", group = G0, inline = "8"), fun_set(cross_0_8, 0, 9, c8)

// arr_cross_90
cross_90_0 = input(false, "4  ", group = G0, inline = "0"), fun_set(cross_90_0, 10, 8, c0)
cross_90_1 = input(false, "15 ", group = G0, inline = "1"), fun_set(cross_90_1, 10, 7, c1)
cross_90_2 = input(false, "34 ", group = G0, inline = "2"), fun_set(cross_90_2, 10, 6, c2)
cross_90_3 = input(false, "61 ", group = G0, inline = "3"), fun_set(cross_90_3, 10, 5, c3)
cross_90_4 = input(false, "96 ", group = G0, inline = "4"), fun_set(cross_90_4, 10, 4, c4)
cross_90_5 = input(false, "139", group = G0, inline = "5"), fun_set(cross_90_5, 10, 3, c5)
cross_90_6 = input(false, "190", group = G0, inline = "6"), fun_set(cross_90_6, 10, 2, c6)
cross_90_7 = input(false, "249", group = G0, inline = "7"), fun_set(cross_90_7, 10, 1, c7)
cross_90_8 = input(false, "316", group = G0, inline = "8"), fun_set(cross_90_8, 10, 0, c8)

// arr_cross_180
cross_180_0 = input(false, "6  ", group = G0, inline = "0"), fun_set(cross_180_0, 11, 9, c0)
cross_180_1 = input(false, "19 ", group = G0, inline = "1"), fun_set(cross_180_1, 12, 9, c1)
cross_180_2 = input(false, "40 ", group = G0, inline = "2"), fun_set(cross_180_2, 13, 9, c2)
cross_180_3 = input(false, "69 ", group = G0, inline = "3"), fun_set(cross_180_3, 14, 9, c3)
cross_180_4 = input(false, "106", group = G0, inline = "4"), fun_set(cross_180_4, 15, 9, c4)
cross_180_5 = input(false, "151", group = G0, inline = "5"), fun_set(cross_180_5, 16, 9, c5)
cross_180_6 = input(false, "204", group = G0, inline = "6"), fun_set(cross_180_6, 17, 9, c6)
cross_180_7 = input(false, "265", group = G0, inline = "7"), fun_set(cross_180_7, 18, 9, c7)
cross_180_8 = input(false, "334", group = G0, inline = "8"), fun_set(cross_180_8, 19, 9, c8)

// arr_cross_270
cross_270_0 = input(false, "8  ", group = G0, inline = "0"), fun_set(cross_270_0, 10, 11, c0)
cross_270_1 = input(false, "23 ", group = G0, inline = "1"), fun_set(cross_270_1, 10, 12, c1)
cross_270_2 = input(false, "46 ", group = G0, inline = "2"), fun_set(cross_270_2, 10, 13, c2)
cross_270_3 = input(false, "77 ", group = G0, inline = "3"), fun_set(cross_270_3, 10, 14, c3)
cross_270_4 = input(false, "116", group = G0, inline = "4"), fun_set(cross_270_4, 10, 15, c4)
cross_270_5 = input(false, "163", group = G0, inline = "5"), fun_set(cross_270_5, 10, 16, c5)
cross_270_6 = input(false, "218", group = G0, inline = "6"), fun_set(cross_270_6, 10, 17, c6)
cross_270_7 = input(false, "281", group = G0, inline = "7"), fun_set(cross_270_7, 10, 18, c7)
cross_270_8 = input(false, "352", group = G0, inline = "8"), fun_set(cross_270_8, 10, 19, c8)

// arr_cardinal_45
cardinal_45_0 = input(false, "3  ", group = G1, inline =  "9"), fun_set(cardinal_45_0, 8, 8,  c9)
cardinal_45_1 = input(false, "13 ", group = G1, inline = "10"), fun_set(cardinal_45_1, 7, 7, c10)
cardinal_45_2 = input(false, "31 ", group = G1, inline = "11"), fun_set(cardinal_45_2, 6, 6, c11)
cardinal_45_3 = input(false, "57 ", group = G1, inline = "12"), fun_set(cardinal_45_3, 5, 5, c12)
cardinal_45_4 = input(false, "91 ", group = G1, inline = "13"), fun_set(cardinal_45_4, 4, 4, c13)
cardinal_45_5 = input(false, "133", group = G1, inline = "14"), fun_set(cardinal_45_5, 3, 3, c14)
cardinal_45_6 = input(false, "183", group = G1, inline = "15"), fun_set(cardinal_45_6, 2, 2, c15)
cardinal_45_7 = input(false, "241", group = G1, inline = "16"), fun_set(cardinal_45_7, 1, 1, c16)
cardinal_45_8 = input(false, "307", group = G1, inline = "17"), fun_set(cardinal_45_8, 0, 0, c17)

// arr_cardinal_135
cardinal_135_0 = input(false, "5  ", group = G1, inline =  "9"), fun_set(cardinal_135_0, 11, 8,  c9)
cardinal_135_1 = input(false, "17 ", group = G1, inline = "10"), fun_set(cardinal_135_1, 12, 7, c10)
cardinal_135_2 = input(false, "37 ", group = G1, inline = "11"), fun_set(cardinal_135_2, 13, 6, c11)
cardinal_135_3 = input(false, "65 ", group = G1, inline = "12"), fun_set(cardinal_135_3, 14, 5, c12)
cardinal_135_4 = input(false, "101", group = G1, inline = "13"), fun_set(cardinal_135_4, 15, 4, c13)
cardinal_135_5 = input(false, "145", group = G1, inline = "14"), fun_set(cardinal_135_5, 16, 3, c14)
cardinal_135_6 = input(false, "197", group = G1, inline = "15"), fun_set(cardinal_135_6, 17, 2, c15)
cardinal_135_7 = input(false, "257", group = G1, inline = "16"), fun_set(cardinal_135_7, 18, 1, c16)
cardinal_135_8 = input(false, "325", group = G1, inline = "17"), fun_set(cardinal_135_8, 19, 0, c17)

// arr_cardinal_225
cardinal_225_0 = input(false, "7  ", group = G1, inline =  "9"), fun_set(cardinal_225_0, 11, 11,  c9)
cardinal_225_1 = input(false, "21 ", group = G1, inline = "10"), fun_set(cardinal_225_1, 12, 12, c10)
cardinal_225_2 = input(false, "43 ", group = G1, inline = "11"), fun_set(cardinal_225_2, 13, 13, c11)
cardinal_225_3 = input(false, "73 ", group = G1, inline = "12"), fun_set(cardinal_225_3, 14, 14, c12)
cardinal_225_4 = input(false, "111", group = G1, inline = "13"), fun_set(cardinal_225_4, 15, 15, c13)
cardinal_225_5 = input(false, "157", group = G1, inline = "14"), fun_set(cardinal_225_5, 16, 16, c14)
cardinal_225_6 = input(false, "211", group = G1, inline = "15"), fun_set(cardinal_225_6, 17, 17, c15)
cardinal_225_7 = input(false, "273", group = G1, inline = "16"), fun_set(cardinal_225_7, 18, 18, c16)
cardinal_225_8 = input(false, "343", group = G1, inline = "17"), fun_set(cardinal_225_8, 19, 19, c17)

// arr_cardinal_315
cardinal_315_0 = input(false, "9  ", group = G1, inline =  "9"), fun_set(cardinal_315_0, 8, 11,  c9)
cardinal_315_1 = input(false, "25 ", group = G1, inline = "10"), fun_set(cardinal_315_1, 7, 12, c10)
cardinal_315_2 = input(false, "49 ", group = G1, inline = "11"), fun_set(cardinal_315_2, 6, 13, c11)
cardinal_315_3 = input(false, "81 ", group = G1, inline = "12"), fun_set(cardinal_315_3, 5, 14, c12)
cardinal_315_4 = input(false, "121", group = G1, inline = "13"), fun_set(cardinal_315_4, 4, 15, c13)
cardinal_315_5 = input(false, "169", group = G1, inline = "14"), fun_set(cardinal_315_5, 3, 16, c14)
cardinal_315_6 = input(false, "225", group = G1, inline = "15"), fun_set(cardinal_315_6, 2, 17, c15)
cardinal_315_7 = input(false, "289", group = G1, inline = "16"), fun_set(cardinal_315_7, 1, 18, c16)
cardinal_315_8 = input(false, "361", group = G1, inline = "17"), fun_set(cardinal_315_8, 0, 19, c17)