Gann Square 9 Table Concept Alternate UI
// 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
//
// Related build
// https://www.tradingview.com/script/WMeyD4Sy-Gann-Square-4-Cross-Cardinal-Table-Concept/
// https://www.tradingview.com/script/mEzLbIKm-Gann-Square-9-Cross-Cardinal-Table-Concept/
// https://www.tradingview.com/script/yH604xPv-Gann-Square-4-Table-Concept-Alternate-UI/

//@version=4
study("Gann Square 9 Table Concept Alternate UI", shorttitle = "GS9TCAU_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("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, _column0, _row0, _column1, _row1, _column2, _row2, _column3, _row3, _color) =>
    if _input
        table.cell_set_bgcolor(testTable, _column0, _row0, _color)
        table.cell_set_bgcolor(testTable, _column1, _row1, _color)
        table.cell_set_bgcolor(testTable, _column2, _row2, _color)
        table.cell_set_bgcolor(testTable, _column3, _row3, _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")

// input cross                                                                           0     90     180    270     color
//                                                                                       c  r   c  r   c  r  c  r
cross_0 = input(false, "  2,   4,   6,   8", group = G0, inline = "0"), fun_set(cross_0, 8, 9, 10, 8, 11, 9, 10, 11, c0)
cross_1 = input(false, " 11,  15,  19,  23", group = G0, inline = "1"), fun_set(cross_1, 7, 9, 10, 7, 12, 9, 10, 12, c1)
cross_2 = input(false, " 28,  34,  40,  46", group = G0, inline = "2"), fun_set(cross_2, 6, 9, 10, 6, 13, 9, 10, 13, c2)
cross_3 = input(false, " 53,  61,  69,  77", group = G0, inline = "3"), fun_set(cross_3, 5, 9, 10, 5, 14, 9, 10, 14, c3)
cross_4 = input(false, " 86,  96, 106, 116", group = G0, inline = "4"), fun_set(cross_4, 4, 9, 10, 4, 15, 9, 10, 15, c4)
cross_5 = input(false, "127, 139, 151, 163", group = G0, inline = "5"), fun_set(cross_5, 3, 9, 10, 3, 16, 9, 10, 16, c5)
cross_6 = input(false, "176, 190, 204, 218", group = G0, inline = "6"), fun_set(cross_6, 2, 9, 10, 2, 17, 9, 10, 17, c6)
cross_7 = input(false, "233, 249, 265, 281", group = G0, inline = "7"), fun_set(cross_7, 1, 9, 10, 1, 18, 9, 10, 18, c7)
cross_8 = input(false, "298, 316, 334, 352", group = G0, inline = "8"), fun_set(cross_8, 0, 9, 10, 0, 19, 9, 10, 19, c8)

// input cardinal                                                                              45    135     225    315    color
//                                                                                             c  r   c  r   c   r  c  r
cardinal_0 = input(false, "3  , 5  , 7  , 9  ", group = G1, inline =  "9"), fun_set(cardinal_0, 8, 8, 11, 8, 11, 11, 8, 11, c9)
cardinal_1 = input(false, "13 , 17 , 21 , 25 ", group = G1, inline = "10"), fun_set(cardinal_1, 7, 7, 12, 7, 12, 12, 7, 12, c10)
cardinal_2 = input(false, "31 , 37 , 43 , 49 ", group = G1, inline = "11"), fun_set(cardinal_2, 6, 6, 13, 6, 13, 13, 6, 13, c11)
cardinal_3 = input(false, "57 , 65 , 73 , 81 ", group = G1, inline = "12"), fun_set(cardinal_3, 5, 5, 14, 5, 14, 14, 5, 14, c12)
cardinal_4 = input(false, "91 , 101, 111, 121", group = G1, inline = "13"), fun_set(cardinal_4, 4, 4, 15, 4, 15, 15, 4, 15, c13)
cardinal_5 = input(false, "133, 145, 157, 169", group = G1, inline = "14"), fun_set(cardinal_5, 3, 3, 16, 3, 16, 16, 3, 16, c14)
cardinal_6 = input(false, "183, 197, 211, 225", group = G1, inline = "15"), fun_set(cardinal_6, 2, 2, 17, 2, 17, 17, 2, 17, c15)
cardinal_7 = input(false, "241, 257, 273, 289", group = G1, inline = "16"), fun_set(cardinal_7, 1, 1, 18, 1, 18, 18, 1, 18, c16)
cardinal_8 = input(false, "307, 325, 343, 361", group = G1, inline = "17"), fun_set(cardinal_8, 0, 0, 19, 0, 19, 19, 0, 19, c17)

