HarmonicDB
// 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

// @description TODO: Database of Harmonic Pattern Specifications
library('HarmonicDB', overlay = true)

// 0. animal_db
// 1. example

// ————————————————————————————————————————————————————————————————————————————— 0. animal_db {
// @function TODO: export animal_db
// @param x TODO: float value is set to default if not necessary
// @returns TODO: [animal_name, spec_B_XA_min, spec_B_XA_max, spec_C_AB_min, spec_C_AB_max, spec_D_BC_min, spec_D_BC_max, spec_D_XA_nom, spec_E_SL_nom]
export animal_db(float prz_crab = 1.618, float prz_gart = 0.786, float prz_shark = 0.886) =>
    p0 = math.round(math.rphi, 3)
    p1 = math.round(math.phi,  3)
    p2 = p0 + 2
    p3 = p0 + 3
    [bc1_gart, bc2_gart, sl_gart] = switch prz_gart
    // Output =>  bc1_gart, bc2_gart, sl_gart
        .786 => [    1.13 ,       p1,   1.  ]
        .886 => [       p1,       p2,   1.13]
    float sl_shark = switch prz_shark
        0.886 => 1.13
        1.130 => 1.27
    // Credits to Scott M Carney, author of Harmonic Trading : Volume Three
    // Page                         101,    98,       104,        92,         113,         107, 119 - 220
    // Index                          0      1          2          3            4            5          6
    animal_name   = array.from('ALT BAT', 'BAT',    'CRAB', 'GARTLEY', 'BUTTERFLY', 'DEEP CRAB',   'SHARK')
    spec_B_XA_min = array.from(     .371,  .382,      .382,      .599,        .762,        .886,      .382) // B = XA ==|=> Trade Identification
    spec_B_XA_max = array.from(     .382,  .5  ,        p0,      .637,        .81 ,        .93 ,        p0) //          |
    spec_C_AB_min = array.from(     .382,  .382,      .382,      .382,        .382,        .382,     1.13 ) // C = AB   |
    spec_C_AB_max = array.from(     .886,  .886,      .886,      .886,        .886,        .886,        p1) //        ==|
    spec_D_BC_min = array.from(    2.   ,    p1,        p2,  bc1_gart,          p1,       2.   ,        p1) // D = BC ==|=> Trade Execution - PRZ (Potential Reversal Zone)
    spec_D_BC_max = array.from(       p3,    p2,        p3,  bc2_gart,       2.24 ,          p3,     2.24 ) //          |
    spec_D_XA_nom = array.from(    1.13 ,  .886,  prz_crab,  prz_gart,       1.27 ,    prz_crab, prz_shark) // D = XA ==|
    spec_E_SL_nom = array.from(    1.27 , 1.13 ,     2.   ,   sl_gart,       1.414,       2.   ,  sl_shark) // Stop Loss => Trade Management (Page 174)
    [animal_name, spec_B_XA_min, spec_B_XA_max, spec_C_AB_min, spec_C_AB_max, spec_D_BC_min, spec_D_BC_max, spec_D_XA_nom, spec_E_SL_nom]

// }
// ————————————————————————————————————————————————————————————————————————————— 1. example {
T0        = 'Small font size recommended for mobile app or multiple layout'
i_s_font  = input.string('large',             'Font size', options = ['tiny', 'small',  'normal', 'large', 'huge'], tooltip = T0)
[animal_name, spec_B_XA_min, spec_B_XA_max, spec_C_AB_min, spec_C_AB_max, spec_D_BC_min, spec_D_BC_max, spec_D_XA_nom, spec_E_SL_nom] = animal_db()
var testTable = table.new(position.middle_center, 10, 10, bgcolor = color.white, border_color = color.black, border_width = 1)
if barstate.islast
    table.cell(testTable, 0, 0,  'ANIMAL', text_size = i_s_font), table.merge_cells(testTable, 0, 0, 0, 1)
    table.cell(testTable, 1, 0,  'B = XA', text_size = i_s_font), table.merge_cells(testTable, 1, 0, 2, 0)
    table.cell(testTable, 1, 1,     'MIN', text_size = i_s_font)
    table.cell(testTable, 2, 1,  '   MAX', text_size = i_s_font)
    table.cell(testTable, 3, 0,  'C = AB', text_size = i_s_font), table.merge_cells(testTable, 3, 0, 4, 0)
    table.cell(testTable, 3, 1,     'MIN', text_size = i_s_font)
    table.cell(testTable, 4, 1,     'MAX', text_size = i_s_font)
    table.cell(testTable, 5, 0,  'D = BC', text_size = i_s_font), table.merge_cells(testTable, 5, 0, 6, 0)
    table.cell(testTable, 5, 1,     'MIN', text_size = i_s_font)
    table.cell(testTable, 6, 1,     'MAX', text_size = i_s_font)
    table.cell(testTable, 7, 0,  'D = XA', text_size = i_s_font), table.merge_cells(testTable, 7, 0, 7, 1)
    table.cell(testTable, 8, 0, 'SL = XA', text_size = i_s_font), table.merge_cells(testTable, 8, 0, 8, 1)

    for x = 0 to 6
        table.cell(testTable, 0, x + 2,              array.get(animal_name,   x),           bgcolor = array.get(spec_D_XA_nom, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
        table.cell(testTable, 1, x + 2, str.tostring(array.get(spec_B_XA_min, x), '0.000'), bgcolor = array.get(spec_B_XA_min, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
        table.cell(testTable, 2, x + 2, str.tostring(array.get(spec_B_XA_max, x), '0.000'), bgcolor = array.get(spec_B_XA_max, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
        table.cell(testTable, 3, x + 2, str.tostring(array.get(spec_C_AB_min, x), '0.000'), bgcolor = array.get(spec_C_AB_min, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
        table.cell(testTable, 4, x + 2, str.tostring(array.get(spec_C_AB_max, x), '0.000'), bgcolor = array.get(spec_C_AB_max, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
        table.cell(testTable, 5, x + 2, str.tostring(array.get(spec_D_BC_min, x), '0.000'), bgcolor = array.get(spec_D_BC_min, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
        table.cell(testTable, 6, x + 2, str.tostring(array.get(spec_D_BC_max, x), '0.000'), bgcolor = array.get(spec_D_BC_max, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
        table.cell(testTable, 7, x + 2, str.tostring(array.get(spec_D_XA_nom, x), '0.000'), bgcolor = array.get(spec_D_XA_nom, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
        table.cell(testTable, 8, x + 2, str.tostring(array.get(spec_E_SL_nom, x), '0.000'), bgcolor = array.get(spec_E_SL_nom, x) <= 1 ? color.red : color.blue, text_size = i_s_font)
// }