TR High-Low
// 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
indicator("TR High-Low", overlay=true, max_labels_count=500, max_lines_count=500, max_bars_back=500)

import Tommy_Rich/TR_HighLow_Lib/7 as trHL

// //////       //
// //// input ////             **********インプット**********
// //       //////

// ZigZag
INP_HighLow_Length      =input.int   (defval =6,          minval =2, step =1,    title ="High-Low Length",     group ="ZigZag",            inline ='inline1-1')
INP_HighLow_Extension   =input.int   (defval =3,          minval =0, step =1,    title =" Extension",          group ="ZigZag",            inline ='inline1-1')
INP_HighLow_Difference  =input.float (defval =0.3,        minval =0, step =0.01, title ="Difference",          group ="ZigZag",            inline ='inline1-1')
INP_HighLow_Histories   =input.int   (defval =10,         minval =4, step =2,    title =" Histories",          group ="ZigZag",            inline ='inline1-1')
INP_HighLow_Color_1     =input       (defval =color.blue,                        title ="Color1",              group ="ZigZag",            inline ='inline1-2')
INP_HighLow_Width_1     =input.int   (defval =2,          minval =1, step =1,    title =" Width1",             group ="ZigZag",            inline ='inline1-2')
INP_HighLow_Color_2     =input       (defval =color.red,                         title =" Color2",             group ="ZigZag",            inline ='inline1-2')
INP_HighLow_Width_2     =input.int   (defval =2,          minval =1, step =1,    title =" Width2",             group ="ZigZag",            inline ='inline1-2')
INP_HighLow_ShowLabel   =input.bool  (defval =true,                              title ="Show Label",          group ="ZigZag",            inline ='inline1-3')
INP_HighLow_ShowZigZag  =input.bool  (defval =true,                              title ="Show ZigZag",         group ="ZigZag",            inline ='inline1-3')
INP_HighLow_ShowHiLoBar =input.bool  (defval =true,                              title ="Show Hi-Lo Bar",      group ="ZigZag",            inline ='inline1-4')
INP_HighLow_BarWidth    =input.int   (defval =1,          minval =1, step =1,    title =" Bar Width",          group ="ZigZag",            inline ='inline1-4')
INP_HighLow_LabelSize   =input.string(
                           defval =size.normal,
                           options =[size.auto,size.huge,size.large,size.normal,size.small,size.tiny],
                           title ="Label Size",
                           group ="ZigZag",
                           inline ="inline1-5")

// Trend Line
INP_Trend_ShowLine      =input.bool  (defval =true,                              title ="Show Trend Line",     group ="Trend Line",        inline ='inline2-1')
INP_Trend_MultiLine     =input.bool  (defval =false,                             title =" Multi Line",         group ="Trend Line",        inline ='inline2-1')
INP_Trend_ColorMode     =input.int   (
                           defval =1,
                           options =[0, 1],
                           title ="Color Mode 0:Nomal / 1:Gradiation",
                           group ="Trend Line",
                           inline ='inline2-2')

INP_Trend_Color1_1      =input       (defval =color.rgb(252, 203, 205),          title ="Trend Line Color1_1", group ="Trend Line Color",  inline ='inline2-3')
INP_Trend_Color1_2      =input       (defval =color.rgb(255,   0,   0),          title =" Color1_2",           group ="Trend Line Color",  inline ='inline2-3')
INP_Trend_Color2_1      =input       (defval =color.rgb(187, 217, 251),          title =" Color2_1",           group ="Trend Line Color",  inline ='inline2-3')
INP_Trend_Color2_2      =input       (defval =color.rgb(  0,   0, 255),          title =" Color2_2",           group ="Trend Line Color",  inline ='inline2-3')
INP_Trend_StartWidth    =input.float (defval =0.02,       minval =0, step =0.01, title ="Start Width",         group ="Trend Line Width",  inline ='inline2-4')
INP_Trend_EndWidth      =input.float (defval =0.02,       minval =0, step =0.01, title =" End Width",          group ="Trend Line Width",  inline ='inline2-4')
INP_Trend_IncreWidth    =input.float (defval =-0.02,                 step =0.01, title =" Increment Width",    group ="Trend Line Width",  inline ='inline2-4')
INP_Trend_StartTrans    =input.int   (defval =60,                    step =1,    title ="Start Trans",         group ="Trend Line Trans",  inline ='inline2-5')
INP_Trend_EndTrans      =input.int   (defval =60,                    step =1,    title =" End Trans",          group ="Trend Line Trans",  inline ='inline2-5')
INP_Trend_IncreTrans    =input.int   (defval =-2,                    step =1,    title =" Increment Trans",    group ="Trend Line Trans",  inline ='inline2-5')

// Fibonacci
INP_Fib_Show            =input.string(
                           defval="double",
                           options=["double", "single"],
                           title="Fibonacci type",
                           group="Fibonacci",
                           inline='inline3-1')
                           
INP_Fib1_Index          =input.int   (defval =1,                     step =1,    title ="Index",               group ="Fibonacci 1",       inline ='inline3-2')
INP_Fib1_FrontMargin    =input.int   (defval =0,                     step =1,    title ="Margin Front",        group ="Fibonacci 1",       inline ='inline3-2')
INP_Fib1_BackMargin     =input.int   (defval =10,                    step =1,    title ="Rear",                group ="Fibonacci 1",       inline ='inline3-2')
INP_Fib1_Transparent    =input.int   (defval =80,                    step =1,    title ="Transparent",         group ="Fibonacci 1",       inline ='inline3-3')
INP_Fib1_TextColor      =input       (defval =color.black,                       title ="Color1",              group ="Fibonacci 1",       inline ='inline3-3')
INP_Fib1_TextMargin     =input.int   (defval =5,                     step =1,    title ="Text Margin",         group ="Fibonacci 1",       inline ='inline3-3')
INP_Fib1_LabelSize      =input.string(
                           defval =size.small,
                           options =[size.auto,size.huge,size.large,size.normal,size.small,size.tiny],
                           title ="Label Size",
                           group ="Fibonacci 1",
                           inline ="inline3-4")
INP_Fib1_Title          =input.string(defval ="Fibonacci 1",                     title ="Title",               group ="Fibonacci 1",       inline ='inline3-4')
INP_Fib1_TitleMargin    =input.float (defval =0.25,                              title ="Title Margin",        group ="Fibonacci 1",       inline ='inline3-4')
INP_Fib1_ChangeColor    =input.bool  (defval =true,                              title ="Change Trend Color1", group ="Fibonacci 1",       inline ='inline3-5')
INP_Fib1_TrendColor     =input       (defval =color.purple,                      title ="Trend Color1",        group ="Fibonacci 1",       inline ='inline3-5')
INP_Fib1_TrendLineWidth =input.int   (defval =4,                     step =1,    title ="Trend Line Width1",   group ="Fibonacci 1",       inline ='inline3-5')

INP_Fib2_Index          =input.int   (defval =2,                     step =1,    title ="Index",               group ="Fibonacci 2",       inline ='inline3-6')
INP_Fib2_FrontMargin    =input.int   (defval =15,                    step =1,    title ="Margin Front",        group ="Fibonacci 2",       inline ='inline3-6')
INP_Fib2_BackMargin     =input.int   (defval =25,                    step =1,    title ="Rear",                group ="Fibonacci 2",       inline ='inline3-6')
INP_Fib2_Transparent    =input.int   (defval =80,                    step =1,    title ="Transparent",         group ="Fibonacci 2",       inline ='inline3-7')
INP_Fib2_TextColor      =input       (defval =color.black,                       title ="Color2",              group ="Fibonacci 2",       inline ='inline3-7')
INP_Fib2_TextMargin     =input.int   (defval =5,                     step =1,    title ="Text Margin",         group ="Fibonacci 2",       inline ='inline3-7')
INP_Fib2_LabelSize      =input.string(
                           defval =size.small,
                           options =[size.auto,size.huge,size.large,size.normal,size.small,size.tiny],
                           title ="Label Size",
                           group ="Fibonacci 2",
                           inline ="inline3-8")
INP_Fib2_Title          =input.string(defval ="Fibonacci 2",                     title ="Title",               group ="Fibonacci 2",       inline ='inline3-8')
INP_Fib2_TitleMargin    =input.float (defval =0.25,                              title ="Title Margin",        group ="Fibonacci 2",       inline ='inline3-4')
INP_Fib2_ChangeColor    =input.bool  (defval =true,                              title ="Change Trend Color2", group ="Fibonacci 2",       inline ='inline3-9')
INP_Fib2_TrendColor     =input       (defval =color.orange,                      title ="Trend Color2",        group ="Fibonacci 2",       inline ='inline3-9')
INP_Fib2_TrendLineWidth =input.int   (defval =4,                    step =1,     title ="Trend Line Width2",   group ="Fibonacci 2",       inline ='inline3-9')

INP_Fib_m1000           =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-1')
INP_Fib_m1000_Value     =input.float (defval =-1.000,                step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-1')
INP_Fib_m0764           =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-1')
INP_Fib_m0764_Value     =input.float (defval =-0.764,                step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-1')
INP_Fib_m0618           =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-1')
INP_Fib_m0618_Value     =input.float (defval =-0.618,                step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-1')
INP_Fib_m0500           =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-2')
INP_Fib_m0500_Value     =input.float (defval =-0.500,                step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-2')
INP_Fib_m0382           =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-2')
INP_Fib_m0382_Value     =input.float (defval =-0.382,                step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-2')
INP_Fib_m0236           =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-2')
INP_Fib_m0236_Value     =input.float (defval =-0.236,                step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-2')

INP_Fib_0000            =input.bool  (defval =true,                              title ="",                    group ="Fibonacci Level",   inline ='inline4-3')
INP_Fib_0000_Value      =input.float (defval =0.000,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-3')
INP_Fib_0236            =input.bool  (defval =true,                              title ="",                    group ="Fibonacci Level",   inline ='inline4-3')
INP_Fib_0236_Value      =input.float (defval =0.236,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-3')
INP_Fib_0382            =input.bool  (defval =true,                              title ="",                    group ="Fibonacci Level",   inline ='inline4-3')
INP_Fib_0382_Value      =input.float (defval =0.382,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-3')
INP_Fib_0500            =input.bool  (defval =true,                              title ="",                    group ="Fibonacci Level",   inline ='inline4-4')
INP_Fib_0500_Value      =input.float (defval =0.500,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-4')
INP_Fib_0618            =input.bool  (defval =true,                              title ="",                    group ="Fibonacci Level",   inline ='inline4-4')
INP_Fib_0618_Value      =input.float (defval =0.618,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-4')
INP_Fib_0764            =input.bool  (defval =true,                              title ="",                    group ="Fibonacci Level",   inline ='inline4-4')
INP_Fib_0764_Value      =input.float (defval =0.764,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-4')
INP_Fib_1000            =input.bool  (defval =true,                              title ="",                    group ="Fibonacci Level",   inline ='inline4-5')
INP_Fib_1000_Value      =input.float (defval =1.000,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-5')

INP_Fib_1236            =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-5')
INP_Fib_1236_Value      =input.float (defval =1.236,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-5')
INP_Fib_1382            =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-5')
INP_Fib_1382_Value      =input.float (defval =1.382,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-5')
INP_Fib_1500            =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-6')
INP_Fib_1500_Value      =input.float (defval =1.500,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-6')
INP_Fib_1618            =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-6')
INP_Fib_1618_Value      =input.float (defval =1.618,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-6')
INP_Fib_1764            =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-6')
INP_Fib_1764_Value      =input.float (defval =1.764,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-6')
INP_Fib_2000            =input.bool  (defval =false,                             title ="",                    group ="Fibonacci Level",   inline ='inline4-7')
INP_Fib_2000_Value      =input.float (defval =2.000,                 step =0.01, title ="",                    group ="Fibonacci Level",   inline ='inline4-7')

// Table
INP_Tbl_show_1          =input.bool  (defval =false,                             title="Show Table 1",         group ="Table",             inline ='inline5-1')
INP_Tbl_position_1      =input.string(
                           defval="bottom_left",
                           options=["top_left", "top_center", "top_right", "middle_left", "middle_center", "middle_right", "bottom_left", "bottom_center", "bottom_right"],
                           title="Display position",
                           group="Table",
                           inline='inline5-1')
INP_Tbl_show_2          =input.bool  (defval=false,                              title="Show Table 2",         group ="Table",             inline ='inline5-2')
INP_Tbl_position_2      =input.string(
                           defval="top_right",
                           options=["top_left", "top_center", "top_right", "middle_left", "middle_center", "middle_right", "bottom_left", "bottom_center", "bottom_right"],
                           title="Display position",
                           group="Table",
                           inline='inline5-2')

//////                 //
//// global variable ////   **********グローバル変数**********
//                 //////

var float[]  a_PHiLo       =array.new_float (INP_HighLow_Histories, 0.0)        // Array Price
var int[]    a_IHiLo       =array.new_int   (INP_HighLow_Histories, bar_index)  // Array Index
var int[]    a_FHiLo       =array.new_int   (INP_HighLow_Histories, 0)          // Array Flag   0:初回、1:High、2:Low
var float[]  a_DHiLo       =array.new_float (INP_HighLow_Histories, 0.0)        // Array Difference

var float[]  a_PHigh       =array.new_float (INP_HighLow_Histories / 2, 0.)     // Array High Price
var float[]  a_PLow        =array.new_float (INP_HighLow_Histories / 2, 0.)     // Array Low  Price
var int[]    a_IHigh       =array.new_int   (INP_HighLow_Histories / 2, 0)      // Array High Index
var int[]    a_ILow        =array.new_int   (INP_HighLow_Histories / 2, 0)      // Array Low  Index

//////          //
//// function ////   **********関数**********
//          //////

SetFibonacci() =>
    float[]  _Fibonacci =array.new_float()

    if INP_Fib_0000
        array.push(_Fibonacci,  INP_Fib_0000_Value)
    if INP_Fib_0236
        array.push(_Fibonacci,  INP_Fib_0236_Value)
    if INP_Fib_0382
        array.push(_Fibonacci,  INP_Fib_0382_Value)
    if INP_Fib_0500
        array.push(_Fibonacci,  INP_Fib_0500_Value)
    if INP_Fib_0618
        array.push(_Fibonacci,  INP_Fib_0618_Value)
    if INP_Fib_0764
        array.push(_Fibonacci,  INP_Fib_0764_Value)
    if INP_Fib_1000
        array.push(_Fibonacci,  INP_Fib_1000_Value)
    if INP_Fib_1236
        array.push(_Fibonacci,  INP_Fib_1236_Value)
    if INP_Fib_1382
        array.push(_Fibonacci,  INP_Fib_1382_Value)
    if INP_Fib_1500
        array.push(_Fibonacci,  INP_Fib_1500_Value)
    if INP_Fib_1618
        array.push(_Fibonacci,  INP_Fib_1618_Value)
    if INP_Fib_1764
        array.push(_Fibonacci,  INP_Fib_1764_Value)
    if INP_Fib_2000
        array.push(_Fibonacci,  INP_Fib_2000_Value)
    if INP_Fib_m0236
        array.push(_Fibonacci,  INP_Fib_m0236_Value)
    if INP_Fib_m0382
        array.push(_Fibonacci,  INP_Fib_m0382_Value)
    if INP_Fib_m0500
        array.push(_Fibonacci,  INP_Fib_m0500_Value)
    if INP_Fib_m0618
        array.push(_Fibonacci,  INP_Fib_m0618_Value)
    if INP_Fib_m0764
        array.push(_Fibonacci,  INP_Fib_m0764_Value)
    if INP_Fib_m1000
        array.push(_Fibonacci,  INP_Fib_m1000_Value)

    _Fibonacci

var float[]  a_Fibonacci =SetFibonacci()

// Delete Object
trHL.DeleteLine()
trHL.DeleteLabel()

trHL.High_Low(
  high, low,
  a_PHiLo, a_IHiLo, a_FHiLo, a_DHiLo, a_PHigh, a_PLow, a_IHigh, a_ILow, a_Fibonacci,
  INP_HighLow_Length, INP_HighLow_Extension, INP_HighLow_Difference, INP_HighLow_Histories,
  INP_HighLow_ShowZigZag,
  INP_HighLow_Color_1, INP_HighLow_Width_1,
  INP_HighLow_Color_2, INP_HighLow_Width_2,
  INP_HighLow_ShowLabel, INP_HighLow_LabelSize, INP_HighLow_ShowHiLoBar, INP_HighLow_BarWidth,
  INP_Trend_ShowLine, INP_Trend_MultiLine,
  INP_Trend_StartWidth, INP_Trend_EndWidth, INP_Trend_IncreWidth,
  INP_Trend_StartTrans, INP_Trend_EndTrans, INP_Trend_IncreTrans, INP_Trend_ColorMode,
  INP_Trend_Color1_1, INP_Trend_Color1_2, INP_Trend_Color2_1, INP_Trend_Color2_2,
  INP_Fib_Show,
  INP_Fib1_Index, INP_Fib1_FrontMargin, INP_Fib1_BackMargin, INP_Fib1_Transparent, INP_Fib1_TextColor, INP_Fib1_TextMargin, INP_Fib1_LabelSize,
  INP_Fib1_Title, INP_Fib1_TitleMargin, INP_Fib1_ChangeColor, INP_Fib1_TrendColor, INP_Fib1_TrendLineWidth,
  INP_Fib2_Index, INP_Fib2_FrontMargin, INP_Fib2_BackMargin, INP_Fib2_Transparent, INP_Fib2_TextColor, INP_Fib2_TextMargin, INP_Fib2_LabelSize,
  INP_Fib2_Title, INP_Fib2_TitleMargin, INP_Fib2_ChangeColor, INP_Fib2_TrendColor, INP_Fib2_TrendLineWidth,
  INP_Tbl_show_1, INP_Tbl_position_1, INP_Tbl_show_2, INP_Tbl_position_2)