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

//@version=5

// @description TODO: add library description here
library("TableUtils", true)

const string TABLE_POSITION_TOP_LEFT     = "Top left"
const string TABLE_POSITION_TOP_RIGHT    = "Top right"
const string TABLE_POSITION_BOTTOM_LEFT  = "Bottom left"
const string TABLE_POSITION_BOTTOM_RIGHT = "Bottom right"
const string TABLE_POSITION_MIDDLE_LEFT  = "Middle left"
const string TABLE_POSITION_MIDDLE_RIGHT = "Middle right"

// @function            Return the position for the table by given label. Useful if you want to control the position of the table with a pine script input selector that use a more human friendly labels
// @param tablePosition (string) Label of the table position. Possible values are: "Top left", "Top right", "Bottom left", "Bottom right", "Middle left", "Middle right"
// @returns             the position for the table by given label. Useful if you want to control the position of the table with a pine script input selector that use a more human friendly labels
export getTablePositionByLabel(string tablePosition) =>
    switch tablePosition
        "Top left"     => position.top_left
        //"Top right"    => position.top_right // default
        "Bottom left"  => position.bottom_left
        "Bottom right" => position.bottom_right
        "Middle left"  => position.middle_left
        "Middle right" => position.middle_right
        =>
            position.top_right


// EXAMPLE OF USAGE
trendToText(trend) => trend == 0 ? "Neutral" : (trend == 1 ? "Bullish" : "Bearish")

trendToColorBg(float trend, color bgColor = na) => trend == 1 ? color.green : (trend == -1 ? color.red : bgColor)

trendToColorTxt(trend) => trend == 1 or trend == -1 ? color.white : color.black

drawTestTable(float trend1D, float trend4H, float trend1H, float trend15, string position = position.top_right, color bgColor = color.gray, color textColor = color.white) =>
    var tableId = table.new(position, 3, 10, border_width = 1)

    // table header
    table.cell(tableId, 0, 0, " ")
    table.cell(tableId, 1, 0, "Trend", bgcolor = bgColor, text_color = textColor)

    // timeframes info
    table.cell(tableId, 0, 1, "1D",                 text_color = textColor,                bgcolor = bgColor)
    table.cell(tableId, 1, 1, trendToText(trend1D), text_color = trendToColorTxt(trend1D), bgcolor = trendToColorBg(trend1D, bgColor))
    table.cell(tableId, 0, 2, "4H",                 text_color = textColor,                bgcolor = bgColor)
    table.cell(tableId, 1, 2, trendToText(trend4H), text_color = trendToColorTxt(trend4H), bgcolor = trendToColorBg(trend4H, bgColor))
    table.cell(tableId, 0, 3, "1H",                 text_color = textColor,                bgcolor = bgColor)
    table.cell(tableId, 1, 3, trendToText(trend1H), text_color = trendToColorTxt(trend1H), bgcolor = trendToColorBg(trend1H, bgColor))
    table.cell(tableId, 0, 4, "15m",                text_color = textColor,                bgcolor = bgColor)
    table.cell(tableId, 1, 4, trendToText(trend15), text_color = trendToColorTxt(trend15), bgcolor = trendToColorBg(trend15, bgColor))
    tableId

trendTablePosition = input.string(TABLE_POSITION_TOP_RIGHT, "Table position", options = [TABLE_POSITION_TOP_LEFT, TABLE_POSITION_TOP_RIGHT, TABLE_POSITION_BOTTOM_LEFT, TABLE_POSITION_BOTTOM_RIGHT, TABLE_POSITION_MIDDLE_LEFT, TABLE_POSITION_MIDDLE_RIGHT])

fastMA = ta.ema(close, 8)
slowMA = ta.ema(close, 21)

[fastMa1D, slowMa1D] = request.security(syminfo.tickerid, "1D",  [fastMA, slowMA], barmerge.gaps_off)
[fastMa4H, slowMa4H] = request.security(syminfo.tickerid, "240", [fastMA, slowMA], barmerge.gaps_off)
[fastMa1H, slowMa1H] = request.security(syminfo.tickerid, "60",  [fastMA, slowMA], barmerge.gaps_off)
[fastMa15, slowMa15] = request.security(syminfo.tickerid, "15",  [fastMA, slowMA], barmerge.gaps_off)

trend1D = math.sign(fastMa1D - slowMa1D)
trend4H = math.sign(fastMa4H - slowMa4H)
trend1H = math.sign(fastMa1H - slowMa1H)
trend15 = math.sign(fastMa15 - slowMa15)

if barstate.islast
    drawTestTable(trend1D, trend4H, trend1H, trend15, getTablePositionByLabel(trendTablePosition))
