Harmonic Pattern Table Inputs
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RozaniGhani-RG

//@version=5
indicator('Harmonic Pattern Table Inputs', 'HPTI', overlay = true)

// 0. Initialize
// 1. Inputs
// 2. Variable
// 3. Methods
// 4. Constructs

//#region ———————————————————— 0. Initialize
// @type         rng
// @field min    float value
// @field max    float value
type rng
    float min = na
    float max = na

// @type         ratio
// @field BXA    rng value
// @field CAB    rng value
// @field DBC    rng value
// @field DXA    float value
// @field SL     float value
type ratio
    rng   BXA = na
    rng   CAB = na
    rng   DBC = na
    float DXA = na
    float  SL = na
//#endregion

//#region ———————————————————— 1. Inputs
TT        = 'Small font size recommended for mobile app or multiple layout'
GT0       = 'TABLE SETTINGS'
i_s_font  = input.string('normal', 'Font size', group = GT0, options = ['tiny', 'small',  'normal', 'large', 'huge'], tooltip = TT)
i_s_Y     = input.string('bottom',  'Position', group = GT0, inline = 'Table2', options = ['top',  'middle', 'bottom'])
i_s_X     = input.string( 'right',          '', group = GT0, inline = 'Table2', options = ['left', 'center', 'right'])

GT1       = 'SHOW / HIDE TITLE'
i_b_title = input.bool(      true,     'TITLE', group = GT1)
i_b_NO    = input.bool(      true,        'NO', group = GT1)
i_b_BXA   = input.bool(      true,   'POINT B', group = GT1)
i_b_CAB   = input.bool(      true,   'POINT C', group = GT1)
i_b_DBC   = input.bool(      true,  'LAYERING', group = GT1)
i_b_DXA   = input.bool(      true,       'PRZ', group = GT1)
i_b_SL    = input.bool(      true, 'STOP LOSS', group = GT1)

GP        = 'SHOW / HIDE PATTERN'
i_b_aBat  = input.bool(      true,   'ALT BAT', group = GP)
i_b_bat   = input.bool(      true,       'BAT', group = GP)
i_b_crab  = input.bool(      true,      'CRAB', group = GP)
i_b_gart  = input.bool(      true,   'GARTLEY', group = GP)  
i_b_bfly  = input.bool(      true, 'BUTTERFLY', group = GP)
i_b_dCrab = input.bool(      true, 'DEEP CRAB', group = GP)
i_b_shark = input.bool(      true,     'SHARK', group = GP)

G0        = 'C = AB'
cRet      =  rng.new(input.float(0.382, 'Retracement', group = G0, inline = 'R'), input.float(0.886, '', group = G0, inline = 'R'))
cExt      =  rng.new(input.float(1.130,   'Extension', group = G0, inline = 'E'), input.float(1.618, '', group = G0, inline = 'E', tooltip = 'Shark only'))

P0     = 'ALT BAT'
aBat   = ratio.new(
         rng.new(input.float(0.371, 'B = XA', group = P0, inline = 'B'), input.float(0.382, '', group = P0, inline = 'B')),
         cRet,
         rng.new(input.float(2.000, 'D = BC', group = P0, inline = 'D'), input.float(3.618, '', group = P0, inline = 'D')),
         input.float(1.130, 'D = XA', group = P0),
         input.float(1.270,     'SL', group = P0))

P1     = 'BAT'
bat    = ratio.new(
         rng.new(input.float(0.382, 'B = XA', group = P1, inline = 'B'), input.float(0.500, '', group = P1, inline = 'B')),
         cRet,
         rng.new(input.float(1.618, 'D = BC', group = P1, inline = 'D'), input.float(2.618, '', group = P1, inline = 'D')),
         input.float(0.886, 'D = XA', group = P1),
         input.float(1.130,     'SL', group = P1))

P2     = 'CRAB'
crab   = ratio.new(
         rng.new(input.float(0.382, 'B = XA', group = P2, inline = 'B'), input.float(0.618, '', group = P2, inline = 'B')),
         cRet,
         rng.new(input.float(2.618, 'D = BC', group = P2, inline = 'D'), input.float(3.618, '', group = P2, inline = 'D')),
         input.float(1.618, 'D = XA', group = P2),
         input.float(2.000,     'SL', group = P2))

P3     = 'GARTLEY'
gart   = ratio.new(
         rng.new(input.float(0.599, 'B = XA', group = P3, inline = 'B'), input.float(0.637, '', group = P3, inline = 'B')),
         cRet,
         rng.new(input.float(1.130, 'D = BC', group = P3, inline = 'D'), input.float(1.618, '', group = P3, inline = 'D')),
         input.float(0.786, 'D = XA', group = P3),
         input.float(1.000,     'SL', group = P3))

P4     = 'BUTTERFLY'
bfly   = ratio.new(
         rng.new(input.float(0.762, 'B = XA', group = P4, inline = 'B'), input.float(0.810, '', group = P4, inline = 'B')),
         cRet,
         rng.new(input.float(1.618, 'D = BC', group = P4, inline = 'D'), input.float(2.240, '', group = P4, inline = 'D')),
         input.float(1.270, 'D = XA', group = P4),
         input.float(1.414,     'SL', group = P4))

P5     = 'DEEP CRAB'
dCrab  = ratio.new(
         rng.new(input.float(0.886, 'B = XA', group = P5, inline = 'B'), input.float(0.930, '', group = P5, inline = 'B')),
         cRet,
         rng.new(input.float(2.000, 'D = BC', group = P5, inline = 'D'), input.float(3.618, '', group = P5, inline = 'D')),
         input.float(1.618, 'D = XA', group = P5),
         input.float(2.000,     'SL', group = P5))

P6     = 'SHARK'
shark  = ratio.new(
         rng.new(input.float(0.382, 'B = XA', group = P6, inline = 'B'), input.float(0.618, '', group = P6, inline = 'B')),
         cExt,
         rng.new(input.float(1.618, 'D = BC', group = P6, inline = 'D'), input.float(2.240, '', group = P6, inline = 'D')),
         input.float(0.886, 'D = XA', group = P6),
         input.float(1.270,     'SL', group = P6))
//#endregion

//#region ———————————————————— 2. Variable
var tbl = table.new(i_s_Y + '_' + i_s_X, 7, 9, color.new(color.blue, 100), chart.fg_color, 1, chart.fg_color, 1)
//#endregion

//#region ———————————————————— 3. Methods
// @function createTitle
// @param       table_id   table id
// @param       fontSize   string value
// @returns   table.cell
method createTitle(table table_id = na, string fontSize = na) =>
    if i_b_title
        if i_b_NO
            table_id.cell(0, 0,                   'NO', text_color = chart.fg_color, text_size = fontSize)
        table_id.cell(    1, 0,    'HARMONIC\nPATTERN', text_color = chart.fg_color, text_size = fontSize)
        if i_b_BXA
            table_id.cell(2, 0,               'B = XA', text_color = chart.fg_color, text_size = fontSize)
        if i_b_CAB
            table_id.cell(3, 0,    'C = AB\n(POINT C)', text_color = chart.fg_color, text_size = fontSize)
        if i_b_DBC
            table_id.cell(4, 0,   'D = BC\n(LAYERING)', text_color = chart.fg_color, text_size = fontSize)
        if i_b_DXA
            table_id.cell(5, 0,        'D = XA\n(PRZ)', text_color = chart.fg_color, text_size = fontSize)
        if i_b_SL
            table_id.cell(6, 0, 'SL = XA\n(STOP LOSS)', text_color = chart.fg_color, text_size = fontSize)

// @function createRow
// @param     table_id   table id
// @param          row   int value
// @param       enable   bool value
// @param       string   str value
// @param           id   ratio value
// @param     fontSize   string value
// @returns table.cell
method createRow(table table_id = na, int row = na, bool enable = na, string str = na, ratio id = na, string fontSize = na) =>
    if enable 
        if i_b_NO
            table_id.cell(0, row, str.tostring(row),                                                             text_size = fontSize, text_color = id.DXA     >= 1. ? color.blue : color.red)
        table_id.cell(    1, row, str,                                                                           text_size = fontSize, text_color = id.DXA     >= 1. ? color.blue : color.red)
        if i_b_BXA
            table_id.cell(2, row, str.tostring(id.BXA.min, '0.000') + ' ; ' + str.tostring(id.BXA.max, '0.000'), text_size = fontSize, text_color = id.BXA.min >= 1. ? color.blue : color.red)
        if i_b_CAB
            table_id.cell(3, row, str.tostring(id.CAB.min, '0.000') + ' ; ' + str.tostring(id.CAB.max, '0.000'), text_size = fontSize, text_color = id.CAB.min >= 1. ? color.blue : color.red)
        if i_b_DBC
            table_id.cell(4, row, str.tostring(id.DBC.min, '0.000') + ' ; ' + str.tostring(id.DBC.max, '0.000'), text_size = fontSize, text_color = id.DBC.min >= 1. ? color.blue : color.red)
        if i_b_DXA
            table_id.cell(5, row, str.tostring(id.DXA,     '0.000'),                                             text_size = fontSize, text_color = id.DXA     >= 1. ? color.blue : color.red)
        if i_b_SL
            table_id.cell(6, row, str.tostring(id.SL,      '0.000'),                                             text_size = fontSize, text_color = id.SL      >= 1. ? color.blue : color.red)
//#endregion

//#region ———————————————————— 4. Constructs
if barstate.islast
    tbl.createTitle(i_s_font)
    tbl.createRow(1, i_b_aBat,   'ALT BAT',  aBat, i_s_font)
    tbl.createRow(2, i_b_bat,        'BAT',   bat, i_s_font)
    tbl.createRow(3, i_b_crab,      'CRAB',  crab, i_s_font)
    tbl.createRow(4, i_b_gart,   'GARTLEY',  gart, i_s_font)
    tbl.createRow(5, i_b_bfly, 'BUTTERFLY',  bfly, i_s_font)
    tbl.createRow(6, i_b_dCrab,'DEEP CRAB', dCrab, i_s_font)
    tbl.createRow(7, i_b_shark,    'SHARK', shark, i_s_font)
//#endregion