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

//@version=5

// @description Helper functions to work with timeframes: to get the next higher TF, to make the string pretty for use in labels, and so on.

library("LibraryTimeframeHelper")


// 🟦🟦🟦
// @function f_getHigherTF(): Converts the input timeframe into the next one up in the list of commonly used timeframes. NOTE: You can NOT use a TF from this function as input to a request.security() call if called from this library because it gets converted to a series (since there's nothing special about this function, I expect this probably goes for any library). However, you CAN copy the code and use it directly in your script, in which case the output is only a simple variable and thus suitable for the timeframe of a request.security() call.
// @param string _TF: The timeframe to convert.
// @returns: A string in standard timeframe format.

export f_getHigherTF(string _TF) =>
    _HTF = switch _TF
        "1" => "2"  // 1 minute
        "2" => "3"
        "3" => "5"
        "5" => "10"
        "10" => "15"
        "15" => "30"
        "30" => "60"
        "60" => "120"  // 2H
        "120" => "180"  // 3H
        "180" => "240"  // 4H
        "240" => "480"  // 8H
        "480" => "720"  // 12H
        "720" => "D"   // D
        "D" => "2D"
        "2D" => "3D"
        "3D" => "W"
        "W" => "2W"
        "2W" => "M"
        => na  // If the TF isn't listed, or the output would be too high, return na, so that it's empty, instead of copying the chart TF

// EXAMPLE
string TF1 = f_getHigherTF(timeframe.period)  // One higher than the chart timeframe
string TF2 = f_getHigherTF(TF1)  // One higher again

var string labelText = na
labelText := "Chart TF: " + str.tostring(timeframe.period)
  + "\n TF1: " + TF1 + ", TF2: " + str.tostring(TF2)


// 🟦🟦🟦
// @function f_prettifyTF(): Converts the input timeframe from standard timeframe format to the format shown by TradingView on a chart. The output is not suitable for use as an input timeframe of a request.security() call.
// @param string _TF: The timeframe to convert.
// @returns: A string in prettified timeframe format.

export f_prettifyTF(string _TF) =>
    int _secs = timeframe.in_seconds(_TF)
    string _prettyTF = _secs > 59 and _secs < 3600 ? str.tostring(value=(_secs/60), format="##m") : _secs > 3599 and _secs < 86400 ? str.tostring(value=(_secs/3600), format="##h") : _TF == "D" or _TF == "W" or _TF == "M" ? "1" + _TF : _TF

// EXAMPLE
in_TF = input.timeframe(title="Timeframe to prettify", defval="")
string prettyTF = f_prettifyTF(in_TF)

labelText := labelText + "\nInput TF: " + in_TF
  + "\nPrettified TF: " + prettyTF


// PRINT THE EXAMPLE LABEL
exampleLabel=label.new(x=bar_index+4, y=0, text=labelText, xloc=xloc.bar_index, yloc=yloc.price, color=chart.fg_color, style=label.style_label_left, textcolor=chart.bg_color, size=size.normal, textalign=text.align_center)
label.delete(exampleLabel[1])

