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

//@version=5

// @description TODO: add library description here
library(title ="TR_Base_Lib",overlay =true)

//////                       //
//// global function group ////    **********グローバル関数群**********
//                       //////

export SetHighLowArray(
  float[] _a_PHiLo, int[]   _a_IHiLo,
  int[]   _a_FHiLo, float[] _a_DHiLo,
  float   _Price,   int     _Index,
  int     _Flag,    float   _Difference,
  float[] _a_Price, int[]   _a_Index) =>

    if _Price >0
        // 配列の先頭から最も古いレベルを削除します
        array.shift(_a_PHiLo)
        array.shift(_a_IHiLo)
        array.shift(_a_FHiLo)
        array.shift(_a_DHiLo)

        // 配列の最後に新しいレベルを追加します
        array.push(_a_PHiLo, _Price)
        array.push(_a_IHiLo, _Index)
        array.push(_a_FHiLo, _Flag)
        array.push(_a_DHiLo, _Difference)

        // High値のみの配列、Low値のみの配列の更新
        array.shift(_a_Price)
        array.shift(_a_Index)
        array.push (_a_Price, _Price)
        array.push (_a_Index, _Index)

    [_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo, _a_Price, _a_Index]

export SetHighLowArray(
  float[] _a_PHiLo, int[]   _a_IHiLo,
  float   _Price,   int     _Index) =>

    // 配列の先頭から最も古いレベルを削除します
    array.shift(_a_PHiLo)
    array.shift(_a_IHiLo)

    // 配列の最後に新しいレベルを追加します
    array.push(_a_PHiLo, _Price)
    array.push(_a_IHiLo, _Index)

    [_a_PHiLo, _a_IHiLo]

export SetHighLowArray(
  float[] _a_PHiLo, int[] _a_IHiLo, int[] _a_Flg,
  float   _Price,   int   _Index,   int   _Flg) =>

    // 配列の先頭から最も古いレベルを削除します
    array.shift(_a_PHiLo)
    array.shift(_a_IHiLo)
    array.shift(_a_Flg)

    // 配列の最後に新しいレベルを追加します
    array.push(_a_PHiLo, _Price)
    array.push(_a_IHiLo, _Index)
    array.push(_a_Flg,   _Flg)

    [_a_PHiLo, _a_IHiLo, _a_Flg]

export ChangeHighLowArray(
  float[] _a_PHiLo, int[]   _a_IHiLo,
  float[] _a_DHiLo, float   _Price,
  int     _Index,   float   _Difference,
  float[] _a_Price, int[]   _a_Index) =>

    int i_1 =array.size(_a_PHiLo) -1
    array.set(_a_PHiLo, i_1, _Price)
    array.set(_a_IHiLo, i_1, _Index)
    array.set(_a_DHiLo, i_1, _Difference)

    int i_2 =array.size(_a_Price) -1
    array.set(_a_Price, i_2, _Price)
    array.set(_a_Index, i_2, _Index)

    [_a_PHiLo, _a_IHiLo, _a_DHiLo, _a_Price, _a_Index]

export ChangeHighLowArray(
  float[] _a_PHiLo, int[]   _a_IHiLo,
  float   _Price,   int     _Index) =>

    int i_1 =array.size(_a_PHiLo) -1
    array.set(_a_PHiLo, i_1, _Price)

    int i_2 =array.size(_a_IHiLo) -1
    array.set(_a_IHiLo, i_2, _Index)

    [_a_PHiLo, _a_IHiLo]

// @function TODO: Function to display labels
// @param _Text TODO: text (series string) Label text.
// @param _X TODO: x (series int) Bar index.
// @param _Y TODO: y (series int/float) Price of the label position.
// @param _Style TODO: style (series string) Label style.
// @param _Size TODO: size (series string) Label size.
// @param _Yloc TODO: yloc (series string) Possible values are yloc.price, yloc.abovebar, yloc.belowbar.
// @param _Color TODO: color (series color) Label border and arrow color
// @returns TODO: No return values
export ShowLabel(
  string _Text,
  int    _X,     float  _Y,
  string _Style, string _Size,
  string _Yloc,  color  _Color) =>

    var label lbl1 = na
    lbl1 := label.new(x =_X, y =_Y, text =_Text, yloc =_Yloc, style =_Style, size =_Size, textcolor =_Color, textalign =text.align_center)

// @function TODO: Function to display labels
// @param _Text TODO: text (series string) Label text.
// @param _Tooltip TODO: text (series string) Tooltip text.
// @param _X TODO: x (series int) Bar index.
// @param _Y TODO: y (series int/float) Price of the label position.
// @param _Style TODO: style (series string) Label style.
// @param _Size TODO: size (series string) Label size.
// @param _Yloc TODO: yloc (series string) Possible values are yloc.price, yloc.abovebar, yloc.belowbar.
// @param _Color TODO: color (series color) Label border and arrow color
// @param _TextColor TODO: color (series color) Label text color
// @returns TODO: No return values
export ShowLabel(
  string _Text,  string _Tooltip,
  int    _X,     float  _Y,
  string _Style, string _Size,
  string _Yloc,  
  color  _Color, color  _TextColor) =>

    var label lbl1 = na
    lbl1 := label.new(x =_X, y =_Y, text =_Text, yloc =_Yloc, style =_Style, size =_Size,color =_Color, textcolor =_TextColor, tooltip =_Tooltip, textalign =text.align_center)

// @function TODO: Function to take out 12 colors in order
// @param _Index TODO: color number.
// @returns TODO: color code
export GetColor(
  int _Index) =>

    int _count =_Index % 12
    color ret =switch _count
        0  =>#ffbf7f
        1  =>#bf7fff
        2  =>#7fffbf
        3  =>#ff7f7f
        4  =>#7f7fff
        5  =>#7fff7f
        6  =>#ff7fbf
        7  =>#7fbfff
        8  =>#bfff7f
        9  =>#ff7fff
        10 =>#7fffff
        11 =>#ffff7f

// @function TODO: Table display position function
// @param _Pos TODO: position.
// @returns TODO: Table position
export Tbl_position(
  string _Pos) =>

    string _result =switch _Pos
        "top_left" => position.top_left
        "top_center" => position.top_center
        "top_right" => position.top_right
        "middle_left" => position.middle_left
        "middle_center" => position.middle_center
        "middle_right" => position.middle_right
        "bottom_left" => position.bottom_left
        "bottom_center" => position.bottom_center
        "bottom_right" => position.bottom_right
    _result

// @function             TODO: テーブル表示位置 日本語表示位置を定数に変換
// @param _Pos           TODO: 日本語表示位置
// @returns              TODO: _result:表示位置の定数を返す
export Tbl_position_JP(
  string _Pos) =>

    string _result =switch _Pos
        "左上" => position.top_left
        "真上" => position.top_center
        "右上" => position.top_right
        "左中" => position.middle_left
        "真中" => position.middle_center
        "右中" => position.middle_right
        "左下" => position.bottom_left
        "真下" => position.bottom_center
        "右下" => position.bottom_right
    _result

// @function             TODO: 足変換、TimeFrameを分に変換
// @param _Tf            TODO: TimeFrame文字
// @returns              TODO: _result:TimeFrameを分に変換した値、_chartTf:チャートのTimeFrameを分に変換した値
export TfInMinutes(
  simple string _Tf ="") =>

    float _chartTf =
      timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
    float _result =0.

    if _Tf == ''
        _result :=_chartTf
    else if _Tf == "1D"
        _result :=60. * 24
    else if _Tf == "1W"
        _result :=60. * 24 * 7
    else if _Tf == "1M"
        _result :=60. * 24 * 365 / 12
    else
        _result :=str.tonumber(_Tf)

    [_result, _chartTf]

// @function             TODO: TimeFrameを日本語足名に変換して返す関数　引数がブランクの時はチャートの日本語足名を返す
// @param _tf            TODO: TimeFrame文字
// @returns              TODO: _result:日本語足名
export TfName_JP(
  simple string _Tf = "") =>

    string _result =""
    if _Tf == ""
        int _count =timeframe.multiplier
        string _ashi =(
          timeframe.isseconds ? "秒" :
          timeframe.isminutes ? (_count % 60 != 0) ? "分" : "時間" :
          timeframe.isdaily   ? "日" :
          timeframe.isweekly  ? "週" :
          timeframe.ismonthly ? "月" : "")
        int _cal = _ashi == "時間" ? _count / 60 : _count
        _result :=str.tostring(_cal) + _ashi
    else
        string _ashi =
           _Tf == "5"   ?   "5分" :
           _Tf == "15"  ?  "15分" :
           _Tf == "60"  ? "1時間" :
           _Tf == "240" ? "4時間" :
           _Tf == "1D"  ?   "1日" :
           _Tf == "1"   ?   "1分" :
           _Tf == "30"  ?  "30分" :
           _Tf == "45"  ?  "45分" :
           _Tf == "120" ? "2時間" :
           _Tf == "180" ? "3時間" :
           _Tf == "1W"  ?   "1週" :
           _Tf == "1M"  ?   "1月" : ""
        _result :=_ashi

// @function      TODO: Delete Line
// @param         TODO: No parameter
// @returns       TODO: No return value
export DeleteLine() =>

    for currentElement in line.all
        line.delete(currentElement)

// @function      TODO: Delete Label
// @param         TODO: No parameter
// @returns       TODO: No return value
export DeleteLabel() =>

    for currentElement in label.all
        label.delete(currentElement)
