TR_HighLow_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_HighLow_Lib",overlay =true)


//////                      //
//// local function group ////    **********ローカル関数群**********
//                      //////
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]

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]

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

// @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) Color of the label border and arrow
// @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 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: 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)


// @function                 TODO: Draw a zig-zag line.
// @param _a_PHiLo           TODO: High-Low price array
// @param _a_IHiLo           TODO: High-Low INDEX array
// @param _a_FHiLo           TODO: High-Low flag array sequence 1:High 2:Low
// @param _a_DHiLo           TODO: High-Low Price Differential Array
// @param _Histories         TODO: Array size (High-Low length)
// @param _Provisional_PHiLo TODO: Provisional High-Low Price
// @param _Provisional_IHiLo TODO: Provisional High-Low INDEX
// @param _Color1            TODO: Normal High-Low color
// @param _Width1            TODO: Normal High-Low width
// @param _Color2            TODO: Provisional High-Low color
// @param _Width2            TODO: Provisional High-Low width
// @param _ShowLabel         TODO: Label display flag True: Displayed False: Not displayed
// @param _LabelSize         TODO: Label Size
// @param _ShowHighLowBar    TODO: High-Low bar display flag True:Show False:Hide
// @param _HighLowBarWidth   TODO: High-Low bar width
// @returns                  TODO: No return value
export ZigZag(
  float[] _a_PHiLo,           int[]   _a_IHiLo,
  int[]   _a_FHiLo,           float[] _a_DHiLo,
  int     _Histories,
  float   _Provisional_PHiLo, int     _Provisional_IHiLo,
  color   _Color1,            int     _Width1,
  color   _Color2,            int     _Width2,
  bool    _ShowLabel,         string  _LabelSize,
  bool    _ShowHighLowBar,    int     _HighLowBarWidth) =>

    string info =""
    var line l   =na
    var line l_1 =na
    var line l_2 =na

    int    i_1 =bar_index - _Histories
    int    i_2 =i_1

    float  p_1 =0., p_2 =0.
    int    f_1 =0 , f_2 =0
    float  d_1 =0., d_2 =0.

    bool _ShowTrendLine  =true

    for int i =0 to _Histories - 1

        i_2 :=i_1
        p_2 :=p_1
        f_2 :=f_1
        d_2 :=d_1

        i_1 :=array.get(_a_IHiLo, i)
        p_1 :=array.get(_a_PHiLo, i)
        f_1 :=array.get(_a_FHiLo, i)
        d_1 :=array.get(_a_DHiLo, i)

        if i !=0
            l :=line.new(i_1, p_1, i_2, p_2, color =_Color1, width =_Width1)

        if _ShowHighLowBar
            l_1 :=line.new(i_1, p_1, i_1 + 50, p_1, color =color.new(GetColor(i),     40), width =_HighLowBarWidth, extend=extend.right)
            if p_2 != 0.
                l_2 :=line.new(i_2, p_2, i_2 + 50, p_2, color =color.new(GetColor(i + 1), 40), width =_HighLowBarWidth, extend=extend.right)

        if _ShowLabel
            string yloc1  =yloc.abovebar
            string yloc2  =yloc.belowbar
            string style1 =label.style_none
            string style2 =label.style_none
            color  color1 =color.blue
            color  color2 =color.red

            if f_1 == 2
                yloc1  :=yloc.belowbar
                yloc2  :=yloc.abovebar
                color1 :=color.red
                color2 :=color.blue

            info :=str.tostring(p_1) + "\n(" + str.tostring(d_1) + ")"
            ShowLabel(info, i_1, p_1, style1, _LabelSize, yloc1, color1)
            if p_2 != 0.
                info :=str.tostring(p_2) + "\n(" + str.tostring(d_2) + ")"
                ShowLabel(info, i_2, p_2, style2, _LabelSize, yloc2, color2)

    if not na(_Provisional_PHiLo)
        if _Provisional_IHiLo > i_1
            l :=line.new(i_1, p_1, _Provisional_IHiLo, _Provisional_PHiLo, color =_Color2, width =_Width2)

// @function             TODO: Draw a Trend Line
// @param _a_PHigh       TODO: High price array
// @param _a_PLow        TODO: Low  price array
// @param _a_IHigh       TODO: High INDEX array
// @param _a_ILow        TODO: Low  INDEX array
// @param _Histories     TODO: Array size (High-Low length)
// @param _MultiLine     TODO: Draw a multiple Line.
// @param _StartWidth    TODO: Line width start value  
// @param _EndWidth      TODO: Line width end value
// @param _IncreWidth    TODO: Line width increment value
// @param _StartTrans    TODO: Transparent rate start value
// @param _EndTrans      TODO: Transparent rate finally
// @param _IncreTrans    TODO: Transparent rate increase value
// @param _ColorMode     TODO: 0:Nomal 1:Gradation
// @param _Color1_1      TODO: Gradation Color 1_1
// @param _Color1_2      TODO: Gradation Color 1_2
// @param _Color2_1      TODO: Gradation Color 2_1
// @param _Color2_2      TODO: Gradation Color 2_2
// @returns              TODO: No return value
export TrendLine(
  float[] _a_PHigh,    float[] _a_PLow,
  int[]   _a_IHigh,    int[]   _a_ILow,
  int     _Histories,  bool    _MultiLine,
  float   _StartWidth, float   _EndWidth,
  float   _IncreWidth, int     _StartTrans,
  int     _EndTrans,   int     _IncreTrans,
  int     _ColorMode,  color   _Color1_1,
  color   _Color1_2,   color   _Color2_1,
  color   _Color2_2) =>
    var line l_1 =na
    var line l_2 =na
    var line l_3 =na
    var line l_4 =na
    var line l_5 =na
    var line l_6 =na

    int      i_1 =bar_index - _Histories, i_2 =i_1, i_3 =i_1, i_4 =i_1
    float    p_1 =0., p_2 =0., p_3 =0., p_4 =0.

    int      f_1 =0,  f_2 =0,  f_3 =0,  f_4 =0
    float    d_1 =0., d_2 =0., d_3 =0., d_4 =0.

    float    width =0., trans =0.

    int      histories_1 =array.size(_a_PHigh) -1
    int      histories_2 =array.size(_a_PLow)  -1

    float h_min = 999999999.
    float h_max =-999999999.
    float h_p1  =0., h_p2 =0.

    for int i =0 to histories_1
        h_p2 :=h_p1
        h_p1 :=array.get(_a_PHigh, i)
        if i > 0
            if (h_p2 - h_p1) > h_max
                h_max :=(h_p2 - h_p1)
            if (h_p2 - h_p1) < h_min
                h_min :=(h_p2 - h_p1)

    float l_min = 999999999.
    float l_max =-999999999.
    float l_p1  =0., l_p2 =0.
    
    for int i =0 to histories_2
        l_p2 :=l_p1
        l_p1 :=array.get(_a_PLow, i)
        if i > 0
            if (l_p2 - l_p1) > l_max
                l_max :=(l_p2 - l_p1)
            if (l_p2 - l_p1) < l_min
                l_min :=(l_p2 - l_p1)

    float    top_Hi    =array.max(_a_PHigh)
    float    top_Lo    =array.min(_a_PHigh)
    float    bottom_Hi =array.max(_a_PLow)
    float    bottom_Lo =array.max(_a_PLow)

    color    c1        =na
    color    c2        =na
    color    c1d       =color.new(color.from_gradient(5, 1, 10, _Color1_1, _Color1_2), 60)
    color    c2d       =color.new(color.from_gradient(5, 1, 10, _Color2_1, _Color2_2), 60)

    if _MultiLine
        for int i =0 to histories_1
            p_1 :=array.get(_a_PHigh, i)
            i_1 :=array.get(_a_IHigh, i)

            width :=_StartWidth + (i * _IncreWidth)
            if _IncreWidth > 0
                if width > _EndWidth
                    width :=_EndWidth
            else
                if width < _EndWidth
                    width :=_EndWidth

            trans :=_StartTrans + (i * _IncreTrans)
            if _IncreTrans > 0
                if trans > _EndTrans
                    trans :=_EndTrans
            else
                if trans < _EndTrans
                    trans :=_EndTrans

            for int j =histories_1 to i
                if j < 0
                    break

                p_3 :=array.get(_a_PHigh, j)
                i_3 :=array.get(_a_IHigh, j)
                if _ColorMode == 1
                    if p_1 >= p_3
                        c1 :=color.new(color.from_gradient(p_1 - p_3, h_min, h_max, _Color1_1, _Color1_2), trans)
                    else
                        c1 :=color.new(color.from_gradient(p_1 - p_3, h_min, h_max, _Color2_1, _Color2_2), trans)
                else
                    c1 :=color.new(GetColor(j), trans)

                l_1 :=line.new(i_1, p_1 + width, i_3, p_3 + width, color =c1,  width =1, extend =extend.right)
                l_2 :=line.new(i_1, p_1,         i_3, p_3,         color =c1d, width =1, extend =extend.right, style =line.style_dotted)
                l_3 :=line.new(i_1, p_1 - width, i_3, p_3 - width, color =c1,  width =1, extend =extend.right)
                linefill.new(l_1, l_3, color.new(c1, trans))

        for int i =0 to histories_2
            p_2 :=array.get(_a_PLow, i)
            i_2 :=array.get(_a_ILow, i)

            width :=_StartWidth + (i * _IncreWidth)
            if _IncreWidth > 0
                if width > _EndWidth
                    width :=_EndWidth
            else
                if width < _EndWidth
                    width :=_EndWidth

            trans :=_StartTrans + (i * _IncreTrans)
            if _IncreTrans > 0
                if trans > _EndTrans
                    trans :=_EndTrans
            else
                if trans < _EndTrans
                    trans :=_EndTrans

            for int j =histories_2 to i
                if j < 0
                    break

                p_4 :=array.get(_a_PLow, j)
                i_4 :=array.get(_a_ILow, j)
                if _ColorMode == 1
                    if p_2 >= p_4
                        c2 :=color.new(color.from_gradient(p_2 - p_4, l_min, l_max, _Color1_1, _Color1_2), trans)
                    else
                        c2 :=color.new(color.from_gradient(p_2 - p_4, l_min, l_max, _Color2_1, _Color2_2), trans)
                else
                    c2 :=color.new(GetColor(j), trans)

                l_4 :=line.new(i_2, p_2 + width, i_4, p_4 + width, color =c2,  width =1, extend =extend.right)
                l_5 :=line.new(i_2, p_2,         i_4, p_4,         color =c2d, width =1, extend =extend.right, style =line.style_dotted)
                l_6 :=line.new(i_2, p_2 - width, i_4, p_4 - width, color =c2,  width =1, extend =extend.right)
                linefill.new(l_4, l_6, color.new(c2, trans))

    else
        for int i =0 to histories_1
            p_3 :=p_1
            i_3 :=i_1
            p_1 :=array.get(_a_PHigh, i)
            i_1 :=array.get(_a_IHigh, i)

            width :=_StartWidth + (i * _IncreWidth)
            if _IncreWidth > 0
                if width > _EndWidth
                    width :=_EndWidth
            else
                if width < _EndWidth
                    width :=_EndWidth

            trans :=_StartTrans + (i * _IncreTrans)
            if _IncreTrans > 0
                if trans > _EndTrans
                    trans :=_EndTrans
            else
                if trans < _EndTrans
                    trans :=_EndTrans

            if p_3 !=0
                if _ColorMode == 1
                    if p_1 >= p_3
                        c1 :=color.new(color.from_gradient(p_1 - p_3, h_min, h_max, _Color1_1, _Color1_2), trans)
                    else
                        c1 :=color.new(color.from_gradient(p_1 - p_3, h_min, h_max, _Color2_1, _Color2_2), trans)
                else
                    c1 :=color.new(GetColor(i), trans)

                l_1 :=line.new(i_1, p_1 + width, i_3, p_3 + width, color =c1,  width =1, extend =extend.left)
                l_2 :=line.new(i_1, p_1,         i_3, p_3,         color =c1d, width =1, extend =extend.left, style =line.style_dotted)
                l_3 :=line.new(i_1, p_1 - width, i_3, p_3 - width, color =c1,  width =1, extend =extend.left)
                linefill.new(l_1, l_3, color.new(c1, trans))

        for int i =0 to histories_2
            p_4 :=p_2
            i_4 :=i_2
            p_2 :=array.get(_a_PLow, i)
            i_2 :=array.get(_a_ILow, i)

            width :=_StartWidth + (i * _IncreWidth)
            if _IncreWidth > 0
                if width > _EndWidth
                    width :=_EndWidth
            else
                if width < _EndWidth
                    width :=_EndWidth

            trans :=_StartTrans + (i * _IncreTrans)
            if _IncreTrans > 0
                if trans > _EndTrans
                    trans :=_EndTrans
            else
                if trans < _EndTrans
                    trans :=_EndTrans

            if p_4 !=0
                if _ColorMode == 1
                    if p_2 >= p_4
                        c2 :=color.new(color.from_gradient(p_2 - p_4, l_min, l_max, _Color1_1, _Color1_2), trans)
                    else
                        c2 :=color.new(color.from_gradient(p_2 - p_4, l_min, l_max, _Color2_1, _Color2_2), trans)
                else
                    c2 :=color.new(GetColor(i), trans)

                l_4 :=line.new(i_2, p_2 + width, i_4, p_4 + width, color =c2,  width =1, extend =extend.left)
                l_5 :=line.new(i_2, p_2,         i_4, p_4,         color =c2d, width =1, extend =extend.left, style =line.style_dotted)
                l_6 :=line.new(i_2, p_2 - width, i_4, p_4 - width, color =c2,  width =1, extend =extend.left)
                linefill.new(l_4, l_6, color.new(c2, trans))

// @function                 TODO: Draw a fibonacci line
// @param _a_Fibonacci       TODO: Fibonacci percentage array
// @param _a_PHiLo           TODO: High-Low price array
// @param _a_IHiLo           TODO: High-Low index array
// @param _Provisional_PHiLo TODO: Provisional High-Low price (when _Index is 0)
// @param _Index             TODO: Where to draw the Fibonacci line
// @param _FrontMargin       TODO: Fibonacci line front-margin
// @param _BackMargin        TODO: Fibonacci line back-margin
// @param _Transparent       TODO: Transparent rate
// @param _TextColor         TODO: Fibonacci text color
// @param _TextMargin        TODO: Fibonacci text margin
// @param _LabelSize         TODO: Fibonacci label size
// @param _Title             TODO: Fibonacci title
// @param _TitleMargin       TODO: Fibonacci title margin 
// @param _ChangeColor       TODO: Change the color of the corresponding trend line
// @param _TrendColor        TODO: Color of trend line to be changed
// @param _TrendLineWidth    TODO: Width of trend line to be changed
// @returns                  TODO: No return value
export Fibonacci(
  float[] _a_Fibonacci,
  float[] _a_PHiLo,           int[]   _a_IHiLo,
  float   _Provisional_PHiLo, int     _Index,
  int     _FrontMargin,       int     _BackMargin,
  int     _Transparent,       color   _TextColor,
  int     _TextMargin,        string  _LabelSize,
  string  _Title,             float   _TitleMargin,
  bool    _ChangeColor,       color   _TrendColor,
  int     _TrendLineWidth) =>

    int    i_1 =0 , i_2 =0
    float  p_1 =0., p_2 =0.

    line   l   =na
    line[] ls  =array.new_line()

    int    index_1 =array.size(_a_PHiLo) - _Index
    int    index_2 =index_1 - 1

    float  dir  =0.
    float  diff =0.

    float  labelMargin =0.
    int    fib_size =array.size(_a_Fibonacci) -1

    var label lbl1 = na

    i_1 :=bar_index + _FrontMargin
    i_2 :=bar_index + _BackMargin

    if _Index == 0
        p_1 :=_Provisional_PHiLo
    else
        p_1 :=array.get(_a_PHiLo, index_1)

    p_2 :=array.get(_a_PHiLo, index_2)

    // 上昇・下降判断用
    dir :=p_1 - p_2
    // 差の絶対値
    diff :=math.abs(dir)

    if dir > 0
        labelMargin :=p_1 - diff * array.get(_a_Fibonacci, fib_size) - (_TitleMargin)
    else
        labelMargin :=p_1 - diff * array.get(_a_Fibonacci, 0)        - (_TitleMargin)

    lbl1 := label.new(x =i_1 + _TextMargin, y =labelMargin, text =_Title, yloc = yloc.price, style =label.style_none, textcolor =_TrendColor, size =_LabelSize)

    if _ChangeColor
        int x_1 =array.get(_a_IHiLo, index_1)
        int x_2 =array.get(_a_IHiLo, index_2)
        l :=line.new(x_1, p_1, x_2, p_2, color =_TrendColor, width =_TrendLineWidth)

    for i1 =0 to array.size(_a_Fibonacci) -1
        float _f =0.
        float _Fib =array.get(_a_Fibonacci, i1)
        // 上昇
        if dir > 0
            _f :=p_1 - (diff * _Fib)
        else
            _f :=p_1 + (diff * _Fib)

        l :=line.new(i_1, _f, i_2, _f, color =color.red, width =1)
        array.push(ls, l)
        lbl1 := label.new(x =i_1 + _TextMargin, y =_f, text =str.tostring(_Fib, "0.000") + " : " + str.tostring(_f, "0.000"),
          yloc = yloc.price, style =label.style_none, textcolor =_TextColor, size =_LabelSize)

    for i2 =0 to array.size(ls) -2
        if array.size(ls) >= 2
            line l1 =array.get(ls, i2)
            line l2 =array.get(ls, i2 + 1)
            linefill.new(l1, l2, color.new(GetColor(i2), _Transparent))


// @function                 TODO: Draw a fibonacci line
// @param _a_Fibonacci       TODO: Fibonacci percentage array
// @param _a_PHiLo           TODO: High-Low price array
// @param _a_IHiLo           TODO: High-Low index array
// @param _Provisional_PHiLo TODO: Provisional High-Low price (when _Index is 0)
// @param _Index1            TODO: Where to draw the fibonacci line 1
// @param _FrontMargin1      TODO: Fibonacci line front-margin 1
// @param _BackMargin1       TODO: Fibonacci line back-margin 1
// @param _Transparent1      TODO: Transparent rate 1
// @param _TextColor1        TODO: Fibonacci text color 1
// @param _TextMargin1       TODO: Fibonacci text margin 1
// @param _LabelSize1        TODO: Fibonacci label size 1
// @param _Title1            TODO: Fibonacci title 1
// @param _TitleMargin1      TODO: Fibonacci title margin 1
// @param _ChangeColor1      TODO: Change the color of the corresponding trend line 1
// @param _TrendColor1       TODO: Color of trend line to be changed 1
// @param _TrendLineWidth1   TODO: Width of trend line to be changed 1
// @param _Index2            TODO: Where to draw the Fibonacci line 2
// @param _FrontMargin2      TODO: Fibonacci line front-margin 2
// @param _BackMargin2       TODO: Fibonacci line back-margin 2
// @param _Transparent2      TODO: Transparent rate 2
// @param _TextColor2        TODO: Fibonacci text color 2
// @param _TextMargin2       TODO: Fibonacci text margin 2
// @param _LabelSize2        TODO: Fibonacci label size 2
// @param _Title2            TODO: Fibonacci title 2
// @param _TitleMargin2      TODO: Fibonacci title margin 2
// @param _ChangeColor2      TODO: Change the color of the corresponding trend line 2
// @param _TrendColor2       TODO: Color of trend line to be changed 2
// @param _TrendLineWidth2   TODO: Width of trend line to be changed 2
// @returns                  TODO: No return value
export Fibonacci(
  float[] _a_Fibonacci,       float[] _a_PHiLo,
  int[]   _a_IHiLo,           float   _Provisional_PHiLo,
  int     _Index1,            int     _FrontMargin1,
  int     _BackMargin1,       int     _Transparent1,
  color   _TextColor1,        int     _TextMargin1,
  string  _LabelSize1,        
  string  _Title1,            float   _TitleMargin1,
  bool    _ChangeColor1,      color   _TrendColor1,
  int     _TrendLineWidth1,
  int     _Index2,            int     _FrontMargin2,
  int     _BackMargin2,       int     _Transparent2,
  color   _TextColor2,        int     _TextMargin2,
  string  _LabelSize2,        
  string  _Title2,            float   _TitleMargin2,
  bool    _ChangeColor2,      color   _TrendColor2,
  int     _TrendLineWidth2) =>

    int    i1_1 =0 , i1_2 =0 , i2_1 =0 , i2_2 =0
    float  p1_1 =0., p1_2 =0., p2_1 =0., p2_2 =0.

    line   l1   =na
    line   l2   =na
    line[] ls1  =array.new_line(), ls2  =array.new_line()

    int    index1_1 =array.size(_a_PHiLo) - _Index1
    int    index1_2 =index1_1 - 1

    int    index2_1 =array.size(_a_PHiLo) - _Index2
    int    index2_2 =index2_1 - 1

    float  dir1  =0., dir2  =0.
    float  diff1 =0., diff2 =0.

    float  labelMargin1 =0., labelMargin2 =0.
    int    fib_size =array.size(_a_Fibonacci) -1

    var label lbl1 = na
    var label lbl2 = na

    i1_1 :=bar_index + _FrontMargin1
    i1_2 :=bar_index + _BackMargin1

    i2_1 :=bar_index + _FrontMargin2
    i2_2 :=bar_index + _BackMargin2

    if _Index1 == 0
        p1_1 :=_Provisional_PHiLo
    else
        p1_1 :=array.get(_a_PHiLo, index1_1)

    if _Index2 == 0
        p2_1 :=_Provisional_PHiLo
    else
        p2_1 :=array.get(_a_PHiLo, index2_1)

    p1_2 :=array.get(_a_PHiLo, index1_2)
    p2_2 :=array.get(_a_PHiLo, index2_2)

    // 上昇・下降判断用
    dir1 :=p1_1 - p1_2
    dir2 :=p2_1 - p2_2
    // 差の絶対値
    diff1 :=math.abs(dir1)
    diff2 :=math.abs(dir2)

    if dir1 > 0
        labelMargin1 :=p1_1 - diff1 * array.get(_a_Fibonacci, fib_size) - (_TitleMargin1)
    else
        labelMargin1 :=p1_1 - diff1 * array.get(_a_Fibonacci, 0)        - (_TitleMargin1)

    if dir2 > 0
        labelMargin2 :=p2_1 - diff2 * array.get(_a_Fibonacci, fib_size) - (_TitleMargin2)
    else
        labelMargin2 :=p2_1 - diff2 * array.get(_a_Fibonacci, 0)        - (_TitleMargin2)

    lbl1 := label.new(x =i1_1 + _TextMargin1, y =labelMargin1, text =_Title1, yloc = yloc.price, style =label.style_none, textcolor =_TrendColor1, size =_LabelSize1)
    lbl2 := label.new(x =i2_1 + _TextMargin2, y =labelMargin2, text =_Title2, yloc = yloc.price, style =label.style_none, textcolor =_TrendColor2, size =_LabelSize2)

    if _ChangeColor1
        int x1_1 =array.get(_a_IHiLo, index1_1)
        int x1_2 =array.get(_a_IHiLo, index1_2)
        l1 :=line.new(x1_1, p1_1, x1_2, p1_2, color =_TrendColor1, width =_TrendLineWidth1)
    if _ChangeColor2
        int x2_1 =array.get(_a_IHiLo, index2_1)
        int x2_2 =array.get(_a_IHiLo, index2_2)
        l2 :=line.new(x2_1, p2_1, x2_2, p2_2, color =_TrendColor2, width =_TrendLineWidth2)

    for i =0 to fib_size
        float _f1 =0., _f2 =0.
        float _Fib =array.get(_a_Fibonacci, i)
        // 上昇
        if dir1 > 0
            _f1 :=p1_1 - (diff1 * _Fib)
        else
            _f1 :=p1_1 + (diff1 * _Fib)

        if dir2 > 0
            _f2 :=p2_1 - (diff2 * _Fib)
        else
            _f2 :=p2_1 + (diff2 * _Fib)

        l1 :=line.new(i1_1, _f1, i1_2, _f1, color =color.red, width =1)
        array.push(ls1, l1)
        l2 :=line.new(i2_1, _f2, i2_2, _f2, color =color.red, width =1)
        array.push(ls2, l2)

        lbl1 := label.new(x =i1_1 + _TextMargin1, y =_f1, text =str.tostring(_Fib, "0.000") + " : " + str.tostring(_f1, "0.000"),
          yloc = yloc.price, style =label.style_none, textcolor =_TextColor1, size =_LabelSize1)
        lbl2 := label.new(x =i2_1 + _TextMargin2, y =_f2, text =str.tostring(_Fib, "0.000") + " : " + str.tostring(_f2, "0.000"),
          yloc = yloc.price, style =label.style_none, textcolor =_TextColor2, size =_LabelSize2)

    for i =0 to array.size(ls1) -2
        if array.size(ls1) >= 2
            line l1_1 =array.get(ls1, i)
            line l1_2 =array.get(ls1, i + 1)
            linefill.new(l1_1, l1_2, color.new(GetColor(i), _Transparent1))
        if array.size(ls2) >= 2
            line l2_1 =array.get(ls2, i)
            line l2_2 =array.get(ls2, i + 1)
            linefill.new(l2_1, l2_2, color.new(GetColor(i), _Transparent2))

// @function           TODO: Judges High-Low
// @param _High        TODO: High Price Source
// @param _Low         TODO: Low Price Source
// @param _Length      TODO: High-Low Confirmation Length
// @param _Extension   TODO: Length of extension when the difference did not open
// @param _Difference  TODO: Difference size
// @returns            TODO: _HiLo=High-Low flag 0:Neither high nor low、1:High、2:Low、3:High-Low
//                           _PHi=high price、_PLo=low price、_IHi=High Price Index、_ILo=Low Price Index、
//                           _Cnt=count、_ECnt=Extension count、
//                           _DiffHi=Difference from Start(High)、_DiffLo=Difference from Start(Low)、
//                           _StartHi=Start value(High)、_StartLo=Start value(Low)
export High_Low_Judgment(
  float _High,
  float _Low,
  int   _Length,
  int   _Extension,
  float _Difference) =>

    int hiLo =0                                                                                             // High-Lowフラグ

    var float pHi =-9999999999.                                                                             // Price Hi
    var float pLo = 9999999999.                                                                             // Price Lo

    var int   iHi =0                                                                                        // Index Hi
    var int   iLo =0                                                                                        // Index Lo

    var int   cnt  =-1                                                                                      // カウント変数
    var int   eCnt =0                                                                                       // Extカウント変数

    var float diffHi  =0.                                                                                   // Highの差
    var float diffLo  =0.                                                                                   // Lowの差

    var float startHi =0.                                                                                   // Start時のHigh
    var float startLo =0.                                                                                   // Start時のLow

    cnt +=1                                                                                                 // カウントインクリ

    if cnt == 0                                                                                             // 初回の時
        pHi :=_High                                                                                         // _PHiにhighを保管
        pLo :=_Low                                                                                          // pLoにlowを保管
        startHi :=pHi                                                                                       // _StartHiに初回highを保管
        startLo :=pLo                                                                                       // _StartLoに初回lowを保管

        iHi :=bar_index                                                                                     // _IHiにインデックスを保存
        iLo :=bar_index                                                                                     // _ILoにインデックスを保存
    else                                                                                                    // 2回目以降の時
        if pHi < _High                                                                                      // _PHiよりhighが大きい
            pHi :=_High                                                                                     // _PHiをアップデート
            iHi :=bar_index                                                                                 // iHiにインデックスを保存
    
        if pLo > _Low                                                                                       // pLoよりlowが小さい
            pLo :=_Low                                                                                      // pLoをアップデート
            iLo :=bar_index                                                                                 // _ILoにインデックスを保存

        if cnt >= _Length                                                                                   // _Cntが_Length以上
            diffHi :=pHi - startHi                                                                          // _StartHi(初回)と_PHiとの差
            diffLo :=startLo - pLo                                                                          // _StartLo(初回)とpLoとの差
            if diffHi > diffLo                                                                              // Highの方が差が大きい
                if diffHi >= _Difference                                                                    // _DiffHiが_Difference以上
                    hiLo :=1                                                                                // High-Lowフラグに1をセット
                    cnt  :=-1                                                                               // カウント変数リセット
                    eCnt :=0                                                                                // Extカウント変数リセット
            else if diffLo > diffHi                                                                         // Lowの方が差が大きい
                if diffLo >= _Difference                                                                    // _DiffLoが_Difference以上
                    hiLo :=2                                                                                // High-Lowフラグに2をセット
                    cnt  :=-1                                                                               // カウント変数リセット
                    eCnt :=0                                                                                // Extカウント変数リセット
            else                                                                                            // High-Lowの差が同じ
                if diffHi >= _Difference                                                                    // _DiffLoが_Difference以上
                    hiLo :=3                                                                                // High-Lowフラグに3をセット
                    cnt  :=-1                                                                               // カウント変数リセット
                    eCnt :=0                                                                                // Extカウント変数リセット

            if eCnt > _Extension                                                                            // _ECntが_Extension以上
                cnt  :=-1                                                                                   // カウント変数リセット
                eCnt :=0                                                                                    // Extカウント変数リセット
                if hiLo == 0
                    if diffHi > diffLo                                                                      // Highの方が大きい
                        hiLo :=1                                                                            // High-Lowフラグに1をセット
                    else if diffLo > diffHi                                                                 // Lowの方が大きい
                        hiLo :=2                                                                            // High-Lowフラグに2をセット
                    else                                                                                    // High-Low同じ大きさ
                        hiLo :=3                                                                            // High-Lowフラグに3をセット

            else
                eCnt +=1

    [hiLo, pHi, pLo, iHi, iLo, cnt, eCnt, diffHi, diffLo, startHi, startLo]

// @function             TODO: Adds and updates High-Low related arrays from given parameters
// @param _HiLo          TODO: High-Low flag
// @param _Histories     TODO: Array size (High-Low length)
// @param _PHi           TODO: Price Hi
// @param _PLo           TODO: Price Lo
// @param _IHi           TODO: Index Hi
// @param _ILo           TODO: Index Lo
// @param _DiffHi        TODO: Difference in High
// @param _DiffLo        TODO: Difference in Low
// @param _a_PHiLo       TODO: High-Low price array
// @param _a_IHiLo       TODO: High-Low INDEX array
// @param _a_FHiLo       TODO: High-Low flag array 1:High 2:Low
// @param _a_DHiLo       TODO: High-Low Price Differential Array 
// @param _a_PHigh       TODO: High Price Differential Array 
// @param _a_IHigh       TODO: High INDEX array
// @param _a_PLow        TODO: Low  Price Differential Array 
// @param _a_ILow        TODO: Low  INDEX array
// @returns              TODO: _PHiLo price array、_IHiLo indexed array、_FHiLo flag array、_DHiLo price-matching array、
//                             Provisional_PHiLo Provisional price、Provisional_IHiLo 暫定インデックス
export High_Low_Data_AddedAndUpdated(
  int     _HiLo,    int     _Histories,
  float   _PHi,     float   _PLo,
  int     _IHi,     int     _ILo,
  float   _DiffHi,  float   _DiffLo,
  float[] _a_PHiLo, int[]   _a_IHiLo,
  int[]   _a_FHiLo, float[] _a_DHiLo,
  float[] _a_PHigh, int[]   _a_IHigh,
  float[] _a_PLow,  int[]   _a_ILow) =>

    float Provisional_PHiLo =na																			    // 暫定High-Low 価格
    int   Provisional_IHiLo =na																			    // 暫定High-Low インデックス

    if _HiLo != 0																					        // High-Low フラグ 0=初期値でない時
        if array.get(_a_FHiLo, _Histories-1) == 1													        // 前回High-Low フラグ 1=High(高値)の時
            if _PHi >= array.get(_a_PHiLo, _Histories-1)											        // 前回High-Low 価格より現在High(高値)の方が高い(以上)
                ChangeHighLowArray(_a_PHiLo, _a_IHiLo, _a_DHiLo,
                  _PHi, _IHi, _DiffHi, _a_PHigh, _a_IHigh)	                                                // 前回High-Low 価格、インデックス、価格差を置き換える
                if _IHi <= _ILo																		        // 前回High-Low 情報(High)を置き換えたので、HighインデックスよりLowインデックスの方が大きいか確認する
																									        // ↑ちなみにHighは書き換え済みなので何もしない
                    if _HiLo == 2																	        // 今回がLowの時
                        SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                          _PLo, _ILo, _HiLo, _DiffLo, _a_PLow, _a_ILow)	                                    // 配列にLow値を追記する
            else																					        // 前回High-Low 価格より現在High(高値)の方が低い
                if _HiLo == 2																		        // 今回がLowの時
                    SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                      _PLo, _ILo, _HiLo, _DiffLo, _a_PLow, _a_ILow)		                                    // 配列にLow値を追記する
                else if _HiLo == 3																	        // 今回がHigh-Lowの時(HighでもLowでもある時)
                    SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                      _PLo, _ILo, 2, _DiffLo, _a_PLow, _a_ILow)			                                    // 前回がHighなので、配列にLow値から追記する
                    SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                      _PHi, _IHi, 1, _DiffHi, _a_PHigh, _a_IHigh)			                                // 配列にLow値を追記したのでHigh値を追記する
        if array.get(_a_FHiLo, _Histories-1) == 2														    // 前回High-Low フラグ 2=Low(安値)の時
            if _PLo <= array.get(_a_PHiLo, _Histories-1)												    // 前回High-Low 価格より現在Low(安値)の方が低い(以下)
                ChangeHighLowArray(_a_PHiLo, _a_IHiLo, _a_DHiLo,
                  _PLo, _ILo, _DiffLo, _a_PLow, _a_ILow)			                                        // 前回High-Low 価格、インデックス、価格差を置き換える
                if _ILo <= _IHi																		        // 前回High-Low 情報(Low)を置き換えたので、LowインデックスよりHighインデックスの方が大きいか確認する
																									        // ↑ちなみにLowは書き換え済みなので何もしない
                    if _HiLo == 1																	        // 今回がHighの時
                        SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                          _PHi, _IHi, _HiLo, _DiffHi, _a_PHigh, _a_IHigh)	                                // 配列にHigh値を追記する
            else
                if _HiLo == 1																		        // 今回がHighの時
                    SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                      _PHi, _IHi, _HiLo, _DiffHi, _a_PHigh, _a_IHigh)		                                // 配列にHigh値を追記する
                else if _HiLo == 3																	        // 今回がHigh-Lowの時(HighでもLowでもある時)
                    SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                      _PHi, _IHi, 1, _DiffHi, _a_PHigh, _a_IHigh)			                                // 前回Lowなので、配列にHigh値から追記する
                    SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                      _PLo, _ILo, 2, _DiffLo, _a_PLow,  _a_ILow)			                                // 配列にHigh値を追記したのでLow値を追記する
        if array.get(_a_FHiLo, _Histories-1) == 0														    // 前回High-Low フラグ 0=初回の時
            if _HiLo == 1																			        // 今回がHighの時
                SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                  _PHi, _IHi, _HiLo, _DiffHi, _a_PHigh, _a_IHigh)			                                // 配列にHigh値を追記する
            else if _HiLo == 2																		        // 今回がLowの時
                SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                  _PLo, _ILo, _HiLo, _DiffLo, _a_PLow, _a_ILow)			                                    // 配列にLow値を追記する
            else if _HiLo == 3																		        // 今回がHigh-Lowの時(HighでもLowでもある時)
                SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                  _PHi, _IHi, 1, _DiffHi, _a_PHigh, _a_IHigh)				                                // とりあえず、配列にHigh値から追記する
                SetHighLowArray(_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo,
                  _PLo, _ILo, 2, _DiffLo, _a_PLow,  _a_ILow)				                                // 配列にHigh値を追記したのでLow値を追記する
    else																							        // High-Low フラグ 0=初期値の時
        float _price =array.get(_a_PHiLo, _Histories-1)												        // 前回価格の取得
        if array.get(_a_FHiLo, _Histories-1) == 1														    // 前回High-Low フラグ 1=High(高値)の時
            if _price <= _PHi																		        // 前回High-Low 価格より現在High(高値)の方が高い(以上)
                ChangeHighLowArray(_a_PHiLo, _a_IHiLo, _a_DHiLo,
                  _PHi, _IHi, _DiffHi, _a_PHigh, _a_IHigh)			                                        // 前回High-Low 価格、インデックス、価格差を置き換える
            else
                Provisional_PHiLo :=_PLo																    // Lowとして暫定価格をセット
                Provisional_IHiLo :=_ILo																    // Lowとして暫定インデックスをセット
        if array.get(_a_FHiLo, _Histories-1) == 2														    // 前回High-Low フラグ 2=Low(安値)の時
            if _price >= _PLo																		        // 前回High-Low 価格より現在Low(安値)の方が低い(以下)
                ChangeHighLowArray(_a_PHiLo, _a_IHiLo, _a_DHiLo,
                  _PLo, _ILo, _DiffLo, _a_PLow,  _a_ILow)			                                        // 前回High-Low 価格、インデックス、価格差を置き換える
            else
                Provisional_PHiLo :=_PHi																    // Highとして暫定価格をセット
                Provisional_IHiLo :=_IHi																    // Highとして暫定インデックスをセット

    [_a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo, Provisional_PHiLo, Provisional_IHiLo]

// @function                  TODO: Draw the contents of the High-Low array.
// @param _High               TODO: High Source
// @param _Low                TODO: Low  Source
// @param _a_PHiLo            TODO: High-Low price array
// @param _a_IHiLo            TODO: High-Low index array
// @param _a_FHiLo            TODO: High-Low flag sequence 1:High 2:Low
// @param _a_DHiLo            TODO: High-Low price differential array
// @param _a_PHigh            TODO: High price array
// @param _a_PLow             TODO: Low  price array
// @param _a_IHigh            TODO: High index array
// @param _a_ILow             TODO: Low  index array
// @param _a_Fibonacci        TODO: Fibonacci gnar matching
// @param _Length             TODO: Length of confirmation
// @param _Extension          TODO: Extension length of extension when the difference did not open
// @param _Difference         TODO: Difference size
// @param _Histories          TODO: High-Low length
// @param _ShowZigZag         TODO: ZigZag display
// @param _ZigZagColor1       TODO: Colors of ZigZag1
// @param _ZigZagWidth1       TODO: Width of ZigZag1
// @param _ZigZagColor2       TODO: Colors of ZigZag2
// @param _ZigZagWidth2       TODO: Width of ZigZag2
// @param _ShowZigZagLabel    TODO: ZigZagLabel display
// @param _ShowHighLowBar     TODO: High-Low bar display
// @param _ShowTrendLine      TODO: Trend line display
// @param _TrendMultiLine     TODO: Trend multi line display
// @param _TrendStartWidth    TODO: Line width start value 
// @param _TrendEndWidth      TODO: Line width end value
// @param _TrendIncreWidth    TODO: Line width increment value
// @param _TrendStartTrans    TODO: Starting transmittance value
// @param _TrendEndTrans      TODO: Transmittance end value
// @param _TrendIncreTrans    TODO: Increased transmittance value
// @param _TrendColorMode     TODO: color mode
// @param _TrendColor1_1      TODO: Trend color 1_1
// @param _TrendColor1_2      TODO: Trend color 1_2
// @param _TrendColor2_1      TODO: Trend color 2_1
// @param _TrendColor2_2      TODO: Trend color 2_2
// @param _ShowFibonacci1     TODO: Fibonacci1 display
// @param _FibIndex1          TODO: Fibonacci1 index no.
// @param _FibFrontMargin1    TODO: Fibonacci1 front margin
// @param _FibBackMargin1     TODO: Fibonacci1 back margin
// @param _FibTransparent1    TODO: Fibonacci1 transmittance
// @param _FibTextColor1      TODO: Fibonacci1 text color
// @param _FibTextMargin1     TODO: Fibonacci1 text margin
// @param _FibLabelSize1      TODO: Fibonacci1 label size
// @param _FibTitle1          TODO: Fibonacci1 title
// @param _FibTitleMargin1    TODO: Fibonacci1 title margin
// @param _FibChangeColor1    TODO: Fibonacci1 change the color of the trend line?
// @param _FibTrendColor1     TODO: Fibonacci1 trend line color
// @param _FibTrendLineWidth1 TODO: Fibonacci1 trend line width
// @param _ShowFibonacci2     TODO: Fibonacci2 display
// @param _FibIndex2          TODO: Fibonacci2 index no.
// @param _FibFrontMargin2    TODO: Fibonacci2 front margin
// @param _FibBackMargin2     TODO: Fibonacci2 back margin
// @param _FibTransparent2    TODO: Fibonacci2 transmittance
// @param _FibTextColor2      TODO: Fibonacci2 text color
// @param _FibTextMargin2     TODO: Fibonacci2 text margin
// @param _FibLabelSize2      TODO: Fibonacci2 label size
// @param _FibTitle2          TODO: Fibonacci2 title
// @param _FibTitleMargin2    TODO: Fibonacci2 title margin
// @param _FibChangeColor2    TODO: Fibonacci2 change the color of the trend line?
// @param _FibTrendColor2     TODO: Fibonacci2 trend line color
// @param _FibTrendLineWidth2 TODO: Fibonacci2 trend line width
// @param _ShowInfoTable1     TODO: InfoTable1 display
// @param _TablePosition1     TODO: InfoTable1 position
// @param _ShowInfoTable2     TODO: InfoTable2 display
// @param _TablePosition2     TODO: InfoTable2 position
// @returns                   TODO: 無し
export High_Low(
  float   _High,            float   _Low,
  float[] _a_PHiLo,         int[]   _a_IHiLo,
  int[]   _a_FHiLo,         float[] _a_DHiLo,
  float[] _a_PHigh,         float[] _a_PLow,
  int[]   _a_IHigh,         int[]   _a_ILow,
  float[] _a_Fibonacci,
  int     _Length,          int     _Extension,
  float   _Difference,      int     _Histories,
  bool    _ShowZigZag,
  color   _ZigZagColor1,    int     _ZigZagWidth1,
  color   _ZigZagColor2,    int     _ZigZagWidth2,
  bool    _ShowZigZagLabel, string  _ZigZagLabelSize,
  bool    _ShowHighLowBar,  int     _HighLowBarWidth,
  bool    _ShowTrendLine,   bool    _TrendMultiLine,
  float   _TrendStartWidth, float   _TrendEndWidth,
  float   _TrendIncreWidth, int     _TrendStartTrans,
  int     _TrendEndTrans,   int     _TrendIncreTrans,
  int     _TrendColorMode,
  color   _TrendColor1_1,   color   _TrendColor1_2,
  color   _TrendColor2_1,   color   _TrendColor2_2,
  string  _ShowFibonacci,
  int     _FibIndex1,       int     _FibFrontMargin1,
  int     _FibBackMargin1,  int     _FibTransparent1,
  color   _FibTextColor1,   int     _FibTextMargin1,
  string  _FibLabelSize1,   
  string  _FibTitle1,       float   _FibTitleMargin1,
  bool    _FibChangeColor1,
  color   _FibTrendColor1,  int     _FibTrendLineWidth1,
  int     _FibIndex2,       int     _FibFrontMargin2,
  int     _FibBackMargin2,  int     _FibTransparent2,
  color   _FibTextColor2,   int     _FibTextMargin2,
  string  _FibLabelSize2,   
  string  _FibTitle2,       float   _FibTitleMargin2,
  bool    _FibChangeColor2,
  color   _FibTrendColor2,  int     _FibTrendLineWidth2,
  bool    _ShowInfoTable1,  string  _TablePosition1,
  bool    _ShowInfoTable2,  string  _TablePosition2) =>

    [rHiLo, rPHi, rPLo, rIHi, rILo, rCnt, rECnt, rDiffHi, rDiffLo, rStartHi, rStartLo]
      =High_Low_Judgment(_High, _Low, _Length, _Extension, _Difference)

    [rPHiLo, rIHiLo, rFHiLo, rDHiLo, Provisional_PHiLo, Provisional_IHiLo]
      =High_Low_Data_AddedAndUpdated(rHiLo, _Histories, rPHi, rPLo, rIHi, rILo, rDiffHi, rDiffLo, _a_PHiLo, _a_IHiLo, _a_FHiLo, _a_DHiLo, _a_PHigh, _a_IHigh, _a_PLow, _a_ILow)

    if _ShowInfoTable1
        var table    TBL_INFO_1    =na
        // テーブル作成
        TBL_INFO_1 :=table.new(Tbl_position(_TablePosition1), _Histories + 1, 3, border_width = 1)
        //// テーブルに値セット
        // 左タイトル
        table.cell(TBL_INFO_1, 0, 0, "",      bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_1, 0, 1, "PRICE", bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_1, 0, 2, "INDEX", bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
    
        for i =0 to _Histories - 1
            // ヘッダー
            table.cell(TBL_INFO_1, i + 1, 0, str.tostring(i),                               bgcolor =color.new(color.lime,  0), text_color =color.black, text_size =size.auto)
            table.cell(TBL_INFO_1, i + 1, 1, str.tostring(array.get(_a_PHiLo, i), "0.000"), bgcolor =color.new(color.blue, 80), text_color =color.black, text_size =size.auto)
            table.cell(TBL_INFO_1, i + 1, 2, str.tostring(array.get(_a_IHiLo, i)),          bgcolor =color.new(color.red,  80), text_color =color.black, text_size =size.auto)

    if _ShowInfoTable2
        var table    TBL_INFO_2    =na
        // テーブル作成
        TBL_INFO_2 :=table.new(Tbl_position(_TablePosition2), 2, 12, border_width = 1)

        // 左タイトル
        table.cell(TBL_INFO_2, 0, 0,  "HiLo",    bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 1,  "PHi",     bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 2,  "PLo",     bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 3,  "IHi",     bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 4,  "ILo",     bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 5,  "Cnt",     bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 6,  "ECnt",    bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 7,  "DiffHi",  bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 8,  "DiffLo",  bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 9,  "StartHi", bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 10, "StartLo", bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 0, 11, "INDEX",   bgcolor =color.new(color.lime, 0), text_color =color.black, text_size =size.auto)
    
        table.cell(TBL_INFO_2, 1, 0,  str.tostring(rHiLo),             bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 1,  str.tostring(rPHi, "0.000"),     bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 2,  str.tostring(rPLo, "0.000"),     bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 3,  str.tostring(rIHi),              bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 4,  str.tostring(rILo),              bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 5,  str.tostring(rCnt),              bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 6,  str.tostring(rECnt),             bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 7,  str.tostring(rDiffHi,  "0.000"), bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 8,  str.tostring(rDiffLo,  "0.000"), bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 9,  str.tostring(rStartHi, "0.000"), bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 10, str.tostring(rStartLo, "0.000"), bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)
        table.cell(TBL_INFO_2, 1, 11, str.tostring(bar_index),         bgcolor =color.new(color.red, 80), text_color =color.black, text_size =size.auto)

    if _ShowZigZag
        ZigZag(rPHiLo, rIHiLo, rFHiLo, rDHiLo, _Histories, Provisional_PHiLo, Provisional_IHiLo,
          _ZigZagColor1, _ZigZagWidth1, _ZigZagColor2, _ZigZagWidth2, _ShowZigZagLabel, _ZigZagLabelSize, _ShowHighLowBar, _HighLowBarWidth)
    if _ShowTrendLine
        TrendLine(_a_PHigh, _a_PLow, _a_IHigh, _a_ILow, _Histories,
          _TrendMultiLine, _TrendStartWidth, _TrendEndWidth, _TrendIncreWidth, _TrendStartTrans, _TrendEndTrans, _TrendIncreTrans,
          _TrendColorMode, _TrendColor1_1, _TrendColor1_2, _TrendColor2_1, _TrendColor2_2)
    if _ShowFibonacci == "double"
        Fibonacci(_a_Fibonacci, rPHiLo, rIHiLo, Provisional_PHiLo,
          _FibIndex1, _FibFrontMargin1, _FibBackMargin1, _FibTransparent1, _FibTextColor1, _FibTextMargin1, _FibLabelSize1,
          _FibTitle1, _FibTitleMargin1, _FibChangeColor1, _FibTrendColor1, _FibTrendLineWidth1,
          _FibIndex2, _FibFrontMargin2, _FibBackMargin2, _FibTransparent2, _FibTextColor2, _FibTextMargin2, _FibLabelSize2,
          _FibTitle2, _FibTitleMargin2, _FibChangeColor2, _FibTrendColor2, _FibTrendLineWidth2)
    else
        Fibonacci(_a_Fibonacci, rPHiLo, rIHiLo, Provisional_PHiLo, _FibIndex1, _FibFrontMargin1, _FibBackMargin1, _FibTransparent1, _FibTextColor1, _FibTextMargin1, _FibLabelSize1,
          _FibTitle1, _FibTitleMargin1, _FibChangeColor1, _FibTrendColor1, _FibTrendLineWidth1)
