pta_plot
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © protradingart

//@version=5

// @description pta_plot: This library will help you to plot different value. I will keep updating with your requirement
library("pta_plot", overlay = true)

// @function Display array element as a table.
// @param array_id Id of your array.
// @param border_color Color for border (`color.black` is used if no argument is supplied).
// @param position: Position of Table
// @returns Display array element in bottom of the pane.
export print_array_float(float[] array_id, int position = 22, color border_color=color.black) =>
    tablePosition = switch position
        00 => position.top_left
        01 => position.top_center
        02 => position.top_right
        10 => position.middle_left
        11 => position.middle_center
        12 => position.middle_right
        20 => position.bottom_left
        21 => position.bottom_center
        22 => position.bottom_right
    size = array.size(array_id)
    if size > 0
        var testTable = table.new(position = tablePosition, columns = size + 2, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=border_color)
        if barstate.islast
            table.cell(table_id = testTable, column = 0, row = 0, text = "Size")
            table.cell(table_id = testTable, column = 1, row = 0, text = "Index")
            
            table.cell(table_id = testTable, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = testTable, column = 1, row = 1, text = "Value")
            
            for i = 0 to size - 1
                table.cell(table_id = testTable, column = i + 2, row = 0, text = str.tostring(i))
                table.cell(table_id = testTable, column = i + 2, row = 1, text = str.tostring(array.get(array_id, i)))
    else
        var testTable = table.new(position = tablePosition, columns = 2, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=border_color)
        if barstate.islast
            table.cell(table_id = testTable, column = 0, row = 0, text = "Size")
            table.cell(table_id = testTable, column = 1, row = 0, text = "Message")
            
            table.cell(table_id = testTable, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = testTable, column = 1, row = 1, text = "No element in your array", text_color=color.red)


// @function Display array element as a table.
// @param array_id Id of your array.
// @param border_color Color for border (`color.black` is used if no argument is supplied).
// @param position Position of Table
// @returns Display array element in bottom of the pane.
export print_array_int(int[] array_id, int position = 22, color border_color=color.black) =>
    tablePosition = switch position
        00 => position.top_left
        01 => position.top_center
        02 => position.top_right
        10 => position.middle_left
        11 => position.middle_center
        12 => position.middle_right
        20 => position.bottom_left
        21 => position.bottom_center
        22 => position.bottom_right
    size = array.size(array_id)
    if size > 0
        var testTable = table.new(position = tablePosition, columns = size + 2, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=border_color)
        if barstate.islast
            table.cell(table_id = testTable, column = 0, row = 0, text = "Size")
            table.cell(table_id = testTable, column = 1, row = 0, text = "Index")
            
            table.cell(table_id = testTable, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = testTable, column = 1, row = 1, text = "Value")
            
            for i = 0 to size - 1
                table.cell(table_id = testTable, column = i + 2, row = 0, text = str.tostring(i))
                table.cell(table_id = testTable, column = i + 2, row = 1, text = str.tostring(array.get(array_id, i)))
    else
        var testTable = table.new(position = tablePosition, columns = 2, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=border_color)
        if barstate.islast
            table.cell(table_id = testTable, column = 0, row = 0, text = "Size")
            table.cell(table_id = testTable, column = 1, row = 0, text = "Message")
            
            table.cell(table_id = testTable, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = testTable, column = 1, row = 1, text = "No element in your array", text_color=color.red)
            
            
// @function Display array element as a table.
// @param array_id Id of your array.
// @param border_color Color for border (`color.black` is used if no argument is supplied).
// @param position Position of Table
// @returns Display array element in bottom of the pane.
export print_array_string(string[] array_id, int position = 22, color border_color=color.black) =>
    tablePosition = switch position
        00 => position.top_left
        01 => position.top_center
        02 => position.top_right
        10 => position.middle_left
        11 => position.middle_center
        12 => position.middle_right
        20 => position.bottom_left
        21 => position.bottom_center
        22 => position.bottom_right
    size = array.size(array_id)
    if size > 0
        var testTable = table.new(position = tablePosition, columns = size + 2, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=border_color)
        if barstate.islast
            table.cell(table_id = testTable, column = 0, row = 0, text = "Size")
            table.cell(table_id = testTable, column = 1, row = 0, text = "Index")
            
            table.cell(table_id = testTable, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = testTable, column = 1, row = 1, text = "Value")
            
            for i = 0 to size - 1
                table.cell(table_id = testTable, column = i + 2, row = 0, text = str.tostring(i))
                table.cell(table_id = testTable, column = i + 2, row = 1, text = str.tostring(array.get(array_id, i)))
    else
        var testTable = table.new(position = tablePosition, columns = 2, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=border_color)
        if barstate.islast
            table.cell(table_id = testTable, column = 0, row = 0, text = "Size")
            table.cell(table_id = testTable, column = 1, row = 0, text = "Message")
            
            table.cell(table_id = testTable, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = testTable, column = 1, row = 1, text = "No element in your array", text_color=color.red)
            
// @function Display array element as a table.
// @param array_id Id of your array.
// @param border_color Color for border (`color.black` is used if no argument is supplied).
// @param position Position of Table
// @returns Display array element in bottom of the pane.
export print_array_bool(bool[] array_id, int position = 22, color border_color=color.black) =>
    tablePosition = switch position
        00 => position.top_left
        01 => position.top_center
        02 => position.top_right
        10 => position.middle_left
        11 => position.middle_center
        12 => position.middle_right
        20 => position.bottom_left
        21 => position.bottom_center
        22 => position.bottom_right
    size = array.size(array_id)
    if size > 0
        var testTable = table.new(position = tablePosition, columns = size + 2, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=border_color)
        if barstate.islast
            table.cell(table_id = testTable, column = 0, row = 0, text = "Size")
            table.cell(table_id = testTable, column = 1, row = 0, text = "Index")
            
            table.cell(table_id = testTable, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = testTable, column = 1, row = 1, text = "Value")
            
            for i = 0 to size - 1
                table.cell(table_id = testTable, column = i + 2, row = 0, text = str.tostring(i))
                table.cell(table_id = testTable, column = i + 2, row = 1, text = str.tostring(array.get(array_id, i)))
    else
        var testTable = table.new(position = tablePosition, columns = 2, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=border_color)
        if barstate.islast
            table.cell(table_id = testTable, column = 0, row = 0, text = "Size")
            table.cell(table_id = testTable, column = 1, row = 0, text = "Message")
            
            table.cell(table_id = testTable, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = testTable, column = 1, row = 1, text = "No element in your array", text_color=color.red)           
            
        



///////////////////////// Calculate CPR ///////////
// @function Return CPR.
// @param High: High Price for any timeframe.
// @param Low: Low Price for any timeframe.
// @param Close: Close Price for any timeframe.
// @returns CPR Value as Tuple [pp, tc, bc, r1, r2, r3, r4, s1, s2, s3, s4].
export calcCPR(float High, float Low, float Close) =>
    pp = (High + Low + Close) / 3
    tc = (High + Low) / 2
    bc = pp + (pp - tc)

    r1 = 2 * pp - Low
    r2 = pp + (High - Low)
    r3 = High + 2 * (pp - Low)
    r4 = High + 3 * (pp - Low)
    s1 = 2 * pp - High
    s2 = pp - (High - Low)
    s3 = Low - 2 * (High - pp)
    s4 = Low - 3 * (High - pp)
    
    [pp, tc, bc, r1, r2, r3, r4, s1, s2, s3, s4]




///////////////////////// Plot Line and Label Function ///////////
// @description Plots a horizontal line and label at a specified price level on the chart.
// @param Price: The Y value for the line and label.
// @param Text: The text to display alongside the label.
// @param barCount: Number of candle in a session.
// @param Color: The color for the line and label text.
// @param Width: The width of the line (default is 1).
// @param LineStyle: The style of the line (default is solid).
// @param Historical: If set to true, the line and label will appear on historical chart data (default is false).
// @returns Nothing. Displays a horizontal line and label on the chart.
export dll(float Price, string Text, int barCount, color Color, int Width=1, string LineStyle=line.style_solid, bool Historical=false) =>
    // Create a label with specified text at the given price level
    LABEL = label.new(x=bar_index + barCount, y=Price, text=Text + " : " + str.tostring(Price, format.mintick),
                      color=color.new(Color, 100), style=label.style_label_left, size=size.normal, textcolor=Color)
    
    // Draw a horizontal line at the specified price level
    LINE = line.new(x1=bar_index, y1=Price, x2=bar_index + barCount, y2=Price, color=Color, width=Width, style=LineStyle)
    
    // Remove the line and label from historical data if Historical is false
    if not Historical
        label.delete(LABEL[1])
        line.delete(LINE[1])



