Ratio For Harmonic Points
// 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('Ratio For Harmonic Points', shorttitle = 'RFHP')

// 0. Inputs
// 1. Arrays and variables
// 2. Switch
// 3. Custom functions
// 4. Delete drawings
// 5. Construct

// ————————————————————————————————————————————————————————————————————————————— 0. Inputs {
G1               = 'Animal'
point            = input.string(           'B = XA', 'Point', options = ['B = XA', 'C = AB', 'D = BC', 'D = XA', 'Stop Loss'])
show             = input.bool(true,     'Show D = XA labels')
b_abat           = input.bool(true,     '  Alt Bat 1.130 XA', group = G1, inline ='0')
b_bat            = input.bool(true,     '      Bat 0.886 XA', group = G1, inline ='0')
b_crab           = input.bool(true,       '   Crab 1.618 XA', group = G1, inline ='1')
b_gart           = input.bool(true,    '   Gartley 0.786 XA', group = G1, inline ='1')
b_bfly           = input.bool(true,     'Butterfly 1.270 XA', group = G1, inline ='2')
b_dcrab          = input.bool(true,     'Deep Crab 1.618 XA', group = G1, inline ='2')
// }
// ————————————————————————————————————————————————————————————————————————————— 1. Arrays and variables {
p0 = math.rphi
p1 = math.phi
p2 = math.rphi + 2
p3 = math.rphi + 3
arr_bool         = array.new_bool(6)
box[] box_bxa    = array.new_box(6),   box[] box_cab = array.new_box(6), box[] box_dbc    = array.new_box(6)
line_dxa         = array.new_line(6),  line_sl       = array.new_line(6)
label_dxa        = array.new_label(6), label_sl      = array.new_label(6)
array_label      = array.new_label(6)
label_bxa_min    = array.new_label(6), label_bxa_max = array.new_label(6)
label_cab_min    = array.new_label(6), label_cab_max = array.new_label(6)
label_dbc_min    = array.new_label(6), label_dbc_max = array.new_label(6)
animal           = array.from('ALT BAT', 'BAT', 'CRAB', 'GARTLEY', 'BUTTERFLY', 'DEEP CRAB')
bar_label        = array.from(   95,    85,    75,    65,    55,    45)
box_left         = array.from(  100,    90,    80,    70,    60,    50)
box_right        = array.from(   90,    80,    70,    60,    50,    40)
bxa_min          = array.from( .371,  .382,  .382,  .599,  .762,  .886)
bxa_max          = array.from( .382,  .5  ,  .618,  .637,  .81 ,  .913)
dbc_min          = array.from(2.   ,    p1,    p2, 1.130,    p1, 2.   )
dbc_max          = array.from(   p3,    p2,    p3,    p1, 2.24 ,    p3)
dxa              = array.from(1.13 ,  .886,    p1,  .786, 1.27 ,    p1)
sl               = array.from(1.27 , 1.130, 2.   , 1.   , 1.414, 2.   )

var ratioTable   = table.new(position.top_center, 1, 2, border_width = 1)
var int   _left  = na, var int   _right  = na
var float _top   = na, var float _bottom = na
var int   _x     = na
var float _y     = na, var float _y1     = na, var float _y2 = na
var string _text = na

array.set(arr_bool, 0, b_abat)
array.set(arr_bool, 1, b_bat)
array.set(arr_bool, 2, b_crab)
array.set(arr_bool, 3, b_gart)
array.set(arr_bool, 4, b_bfly)
array.set(arr_bool, 5, b_dcrab)
// }
// ————————————————————————————————————————————————————————————————————————————— 2. Switch {
col = switch point
    'B = XA'    => color.red
    'C = AB'    => color.red
    'D = BC'    => color.blue
    'D = XA'    => color.blue
    'Stop Loss' => color.blue
// }
// ————————————————————————————————————————————————————————————————————————————— 3. Custom functions {
f_delete_box(_id) =>
    for x = 0 to array.size(_id) - 1
        box.delete(array.get(_id, x))
f_delete_label(_id) =>
    for x = 0 to array.size(_id) - 1
        label.delete(array.get(_id, x))
// }
// ————————————————————————————————————————————————————————————————————————————— 4. Delete drawings {
f_delete_box(box_bxa)
f_delete_label(array_label)
// }
// ————————————————————————————————————————————————————————————————————————————— 5. Construct {
if barstate.islast
    table.cell(ratioTable, 0, 0, 'Ratio For Harmonic Points', bgcolor = color.new(color.blue, 100), text_color = color.blue)
    table.cell(ratioTable, 0, 1, point,                       bgcolor = color.new(color.blue, 100), text_color = color.blue)
    for x = 0 to 5

        if array.get(arr_bool, x)
            _x := bar_index[array.get(bar_label, x)]
            _text := show ? array.get(animal, x) + str.tostring(array.get(dxa, x), '\n 0.000 XA') : array.get(animal, x)
            array.set(array_label, x, label.new(_x, 0, _text, color = col, style = label.style_label_center, textcolor = color.white))

            if point == 'B = XA'
                _left   := bar_index[array.get(box_left, x)]
                _top    := array.get(bxa_max, x)
                _right  := bar_index[array.get(box_right, x)]
                _bottom := array.get(bxa_min, x)
                _x      := bar_index[array.get(bar_label, x)]
                _y1     := array.get(bxa_min, x)
                _y2     := array.get(bxa_max, x)
                array.set(box_bxa, x, box.new(_left, _top, _right, _bottom, border_color = color.black, bgcolor = col))  
                array.set(label_bxa_min, x, label.new(_x, _y1, str.tostring(_y1, '0.000'), color = color.new(color.blue, 100), style = label.style_label_up,   textcolor = col))
                array.set(label_bxa_max, x, label.new(_x, _y2, str.tostring(_y2, '0.000'), color = color.new(color.blue, 100), style = label.style_label_down, textcolor = col))
    
            else if point == 'C = AB'
                _left   := bar_index[array.get(box_left, x)]
                _right  := bar_index[array.get(box_right, x)]
                _x      := bar_index[array.get(bar_label, x)]
                _y1     := array.get(bxa_min, x)
                _y2     := array.get(bxa_max, x)
                array.set(box_bxa, x, box.new(_left, 0.886, _right, 0.382, border_color = color.black, bgcolor = col))     
                array.set(label_cab_min, x, label.new(_x, 0.382, '0.382', color = color.new(color.blue, 100), style = label.style_label_up,   textcolor = col))
                array.set(label_cab_max, x, label.new(_x, 0.886, '0.886', color = color.new(color.blue, 100), style = label.style_label_down, textcolor = col))    

            else if point == 'D = BC'
                _left   := bar_index[array.get(box_left, x)]
                _top    := array.get(dbc_max, x)
                _right  := bar_index[array.get(box_right, x)]
                _bottom := array.get(dbc_min, x)
                _x      := bar_index[array.get(bar_label, x)]
                _y1     := array.get(dbc_min, x)
                _y2     := array.get(dbc_max, x)
                array.set(box_bxa, x, box.new(_left, _top, _right, _bottom, border_color = color.black, bgcolor = col))  
                array.set(label_dbc_min, x, label.new(_x, _y1, str.tostring(_y1, '0.000'), color = color.new(color.blue, 100), style = label.style_label_up,   textcolor = col))
                array.set(label_dbc_max, x, label.new(_x, _y2, str.tostring(_y2, '0.000'), color = color.new(color.blue, 100), style = label.style_label_down, textcolor = col))

            else if point == 'D = XA'
                _left   := bar_index[array.get(box_left, x)]
                _right  := bar_index[array.get(box_right, x)]
                _x      := bar_index[array.get(bar_label, x)]
                _y      := array.get(dxa, x)
                array.set(line_dxa,  x, line.new(_left, _y, _right, _y, color = col))
                array.set(label_dxa, x, label.new(_x, _y, str.tostring(_y, '0.000'), color = color.new(color.blue, 100), style = label.style_label_down, textcolor = col))
                label.set_color(  array.get(array_label, 1), color.red)
                label.set_color(  array.get(array_label, 3), color.red)
                label.set_textcolor(array.get(label_dxa, 1), color.red)
                label.set_textcolor(array.get(label_dxa, 3), color.red)
                line.set_color(      array.get(line_dxa, 1), color.red)
                line.set_color(      array.get(line_dxa, 3), color.red)

            if point == 'Stop Loss'
                _left   := bar_index[array.get(box_left, x)]
                _right  := bar_index[array.get(box_right, x)]
                _x      := bar_index[array.get(bar_label, x)]
                _y      := array.get(sl, x)
                array.set(line_sl,  x, line.new(_left, _y, _right, _y, color = col))
                array.set(label_sl, x, label.new(_x, _y, str.tostring(_y, '0.000'), color = color.new(color.blue, 100), style = label.style_label_up, textcolor = col))

hline(point == 'B = XA'    ? 1.2 : na,    'B = XA', color.new(color.blue, 100))
hline(point == 'C = AB'    ? 1.2 : na,    'C = AB', color.new(color.blue, 100))
hline(point == 'D = BC'    ? 4.0 : na,    'D = BC', color.new(color.blue, 100))
hline(point == 'D = XA'    ? 2.5 : na,    'D = XA', color.new(color.blue, 100))
hline(point == 'Stop Loss' ? 2.5 : na, 'Stop Loss', color.new(color.blue, 100))
// }