MultiTF 6 MA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jxjay

//@version=5
indicator("MultiTF 6 MA", shorttitle='MTF6MA', overlay=true ,max_lines_count = 100, max_labels_count = 100)



COLOR_01 = #ffffff //1
COLOR_02 = #ffee99 //5
COLOR_03 = #ffff00 //15
COLOR_04 = #ffaa00 //30
COLOR_05 = #ffffff //1h
COLOR_06 = #ffee99 //2h
COLOR_07 = #ffff00 //4h
COLOR_08 = #ffaa00 //6h
COLOR_09 = #ff6600 //8h
COLOR_10 = #ff0000 //12h
COLOR_11 = #ffffff //1d
COLOR_12 = #ffee99 //2d
COLOR_13 = #ffff00 //3d
COLOR_14 = #ffaa00 //1w
COLOR_15 = #ff6600 //1mo


OPACITY = 50



var realTimeLabels = array.new_label()
var realTimeLines = array.new_line()

deleteRealtimeDrawings() =>
    i = 0
    while i < array.size(realTimeLabels)
        label.delete(array.get(realTimeLabels,i))
        array.remove(realTimeLabels, i)
    while i < array.size(realTimeLines)
        line.delete(array.get(realTimeLines,i))
        array.remove(realTimeLines, i)

deleteRealtimeDrawings()


fnc = input.string(title="MA type", defval="EMA", options=["EMA", "SMA", "WMA", "HMA", "RMA", "SWMA", "ALMA", "VWMA", "VWAP"])

srcInput = input.source(close, "Source")

show_len1 = input.bool(defval=true, title="1:",  group="Lengths", inline="len1")
len1 = input.int(defval=21, title="Len", group="Lengths", inline="len1")
lw1 = input.int(defval=2, title="Width", group="Lengths", inline="len1", minval=1, maxval=4, step=1)

show_len2 = input.bool(defval=true, title="2:",  group="Lengths", inline="len2")
len2 = input.int(defval=34, title="Len",  group="Lengths", inline="len2")
lw2 = input.int(defval=1, title="Width", group="Lengths", inline="len2", minval=1, maxval=4, step=1)

show_len3 = input.bool(defval=false, title="3:",  group="Lengths", inline="len3")
len3 = input.int(defval=55, title="Len",  group="Lengths", inline="len3")
lw3 = input.int(defval=1, title="Width", group="Lengths", inline="len3", minval=1, maxval=4, step=1)




c1m  = input.color(defval=color.new(COLOR_01, OPACITY), title="1m",  group="TimeFrame Settings", inline="1m")
show_1m_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="1m")
show_1m_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="1m")
show_1m_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="1m")
show_1m = show_1m_1 or show_1m_2 or show_1m_3

c5m  = input.color(defval=color.new(COLOR_02, OPACITY), title="5m",  group="TimeFrame Settings", inline="5m")
show_5m_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="5m")
show_5m_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="5m")
show_5m_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="5m")
show_5m = show_5m_1 or show_5m_2 or show_5m_3

c15m  = input.color(defval=color.new(COLOR_03, OPACITY), title="15m",  group="TimeFrame Settings", inline="15m")
show_15m_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="15m")
show_15m_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="15m")
show_15m_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="15m")
show_15m = show_15m_1 or show_15m_2 or show_15m_3

c30m  = input.color(defval=color.new(COLOR_04, OPACITY), title="30m",  group="TimeFrame Settings", inline="30m")
show_30m_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="30m")
show_30m_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="30m")
show_30m_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="30m")
show_30m = show_30m_1 or show_30m_2 or show_30m_3

c1h  = input.color(defval=color.new(COLOR_05, OPACITY), title="1H",  group="TimeFrame Settings", inline="1h")
show_1h_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="1h")
show_1h_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="1h")
show_1h_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="1h")
show_1h = show_1h_1 or show_1h_2 or show_1h_3

c2h  = input.color(defval=color.new(COLOR_06, OPACITY), title="2H",  group="TimeFrame Settings", inline="2h")
show_2h_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="2h")
show_2h_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="2h")
show_2h_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="2h")
show_2h = show_2h_1 or show_2h_2 or show_2h_3

c4h  = input.color(defval=color.new(COLOR_07, OPACITY), title="4H",  group="TimeFrame Settings", inline="4h")
show_4h_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="4h")
show_4h_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="4h")
show_4h_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="4h")
show_4h = show_4h_1 or show_4h_2 or show_4h_3

c6h  = input.color(defval=color.new(COLOR_08, OPACITY), title="6H",  group="TimeFrame Settings", inline="6h")
show_6h_1  = input.bool(defval=false, title="Len1",  group="TimeFrame Settings", inline="6h")
show_6h_2  = input.bool(defval=false, title="Len2",  group="TimeFrame Settings", inline="6h")
show_6h_3  = input.bool(defval=false, title="Len3",  group="TimeFrame Settings", inline="6h")
show_6h = show_6h_1 or show_6h_2 or show_6h_3

c8h  = input.color(defval=color.new(COLOR_09, OPACITY), title="8H",  group="TimeFrame Settings", inline="8h")
show_8h_1  = input.bool(defval=false, title="Len1",  group="TimeFrame Settings", inline="8h")
show_8h_2  = input.bool(defval=false, title="Len2",  group="TimeFrame Settings", inline="8h")
show_8h_3  = input.bool(defval=false, title="Len3",  group="TimeFrame Settings", inline="8h")
show_8h = show_8h_1 or show_8h_2 or show_8h_3

c12h  = input.color(defval=color.new(COLOR_10, OPACITY), title="12H",  group="TimeFrame Settings", inline="12h")
show_12h_1  = input.bool(defval=false, title="Len1",  group="TimeFrame Settings", inline="12h")
show_12h_2  = input.bool(defval=false, title="Len2",  group="TimeFrame Settings", inline="12h")
show_12h_3  = input.bool(defval=false, title="Len3",  group="TimeFrame Settings", inline="12h")
show_12h = show_12h_1 or show_12h_2 or show_12h_3

c1D  = input.color(defval=color.new(COLOR_11, OPACITY), title="1D",  group="TimeFrame Settings", inline="1d")
show_1d_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="1d")
show_1d_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="1d")
show_1d_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="1d")
show_1d = show_1d_1 or show_1d_2 or show_1d_3

c2D  = input.color(defval=color.new(COLOR_12, OPACITY), title="2D",  group="TimeFrame Settings", inline="2d")
show_2d_1  = input.bool(defval=false, title="Len1",  group="TimeFrame Settings", inline="2d")
show_2d_2  = input.bool(defval=false, title="Len2",  group="TimeFrame Settings", inline="2d")
show_2d_3  = input.bool(defval=false, title="Len3",  group="TimeFrame Settings", inline="2d")
show_2d = show_2d_1 or show_2d_2 or show_2d_3

c3D  = input.color(defval=color.new(COLOR_13, OPACITY), title="3D",  group="TimeFrame Settings", inline="3d")
show_3d_1  = input.bool(defval=false, title="Len1",  group="TimeFrame Settings", inline="3d")
show_3d_2  = input.bool(defval=false, title="Len2",  group="TimeFrame Settings", inline="3d")
show_3d_3  = input.bool(defval=false, title="Len3",  group="TimeFrame Settings", inline="3d")
show_3d = show_3d_1 or show_3d_2 or show_3d_3

c1W  = input.color(defval=color.new(COLOR_14, OPACITY), title="1W",  group="TimeFrame Settings", inline="1w")
show_1w_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="1w")
show_1w_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="1w")
show_1w_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="1w")
show_1w = show_1w_1 or show_1w_2 or show_1w_3

c1M  = input.color(defval=color.new(COLOR_15, OPACITY), title="1M",  group="TimeFrame Settings", inline="1mo")
show_1mo_1  = input.bool(defval=true, title="Len1",  group="TimeFrame Settings", inline="1mo")
show_1mo_2  = input.bool(defval=true, title="Len2",  group="TimeFrame Settings", inline="1mo")
show_1mo_3  = input.bool(defval=true, title="Len3",  group="TimeFrame Settings", inline="1mo")
show_1mo = show_1mo_1 or show_1mo_2 or show_1mo_3

show_extend  = input.bool(defval=true, title="Extend lines")
show_label  = input.bool(defval=true, title="Show labels")

// pstyle = plot.style_stepline
pstyle = plot.style_line

future_intervals = 20
draw_by = 5


MovAvgType(averageType, averageSource, averageLength) =>
	switch str.upper(averageType)
        "SMA"  => ta.sma(averageSource, averageLength)
        "EMA"  => ta.ema(averageSource, averageLength)
        "WMA"  => ta.wma(averageSource, averageLength)
        "HMA"  => ta.hma(averageSource, averageLength)
        "RMA"  => ta.rma(averageSource, averageLength)
        "SWMA" => ta.swma(averageSource)
        "ALMA" => ta.alma(averageSource, averageLength, 0.85, 6)
        "VWMA" => ta.vwma(averageSource, averageLength)
        "VWAP" => ta.vwap(averageSource)
        => runtime.error("Moving average type '" + averageType +
             "' not found!"), na


estimate_future(data, future_intervals_draw, do_full_estimation = true) =>
    dd1 = data - data[1]
    dd2 = data[1] - data[2]
    future = array.new_float()
    i=1
    float current_val = na
    array.push(future, data)
    while i <= future_intervals_draw and do_full_estimation
        change = dd2-(dd2-dd1)*i*math.pow(0.8,i)
        new_val = nz(current_val, data) + change
        array.push(future, new_val)
        current_val := new_val
        i += 1
    future


get_line_data(src, len, future_intervals_draw, do_full_estimation = true) =>
    data = MovAvgType(fnc,src,len)
    time_diff = time-time[1]
    time_current = time
    time_next = time+time_diff
    future = estimate_future(data, future_intervals_draw, do_full_estimation)

    [data, time_diff, time_next, future]


get_lines_data(src, l1, l2, l3, future_intervals_draw, isDisplayable = true, do_full_estimation = true) =>
    if isDisplayable  == false
        [na,na,na,na,na,na,na,na,na,na]
    [data1, time_diff1, time_next1, future1] = get_line_data(src, l1, future_intervals_draw, do_full_estimation)
    [data2, time_diff2, time_next2, future2] = get_line_data(src, l2, future_intervals_draw, do_full_estimation)
    [data3, time_diff3, time_next3, future3] = get_line_data(src, l3, future_intervals_draw, do_full_estimation)
    [data1, future1, data2, future2,  data3, future3, time_diff1, time_next1]

is_displayable(tf) =>
    disp = false
    disp1 = false
    disp2 = false
    disp3 = false
    if tf == "1"
        time_disp = ( timeframe.isseconds or (timeframe.isminutes and timeframe.multiplier == 1) )
        disp := show_1m and time_disp
        disp1 := show_1m_1 and time_disp
        disp2 := show_1m_2 and time_disp
        disp3 := show_1m_3 and time_disp
    if tf == "5"
        time_disp = ( timeframe.isseconds or (timeframe.isminutes and timeframe.multiplier <= 5) )
        disp := show_5m and time_disp
        disp1 := show_5m_1 and time_disp
        disp2 := show_5m_2 and time_disp
        disp3 := show_5m_3 and time_disp
    if tf == "15"
        time_disp = ( timeframe.isseconds or (timeframe.isminutes and timeframe.multiplier <= 15) )
        disp := show_15m and time_disp
        disp1 := show_15m_1 and time_disp
        disp2 := show_15m_2 and time_disp
        disp3 := show_15m_3 and time_disp
    if tf == "30"
        time_disp = ( timeframe.isseconds or (timeframe.isminutes and timeframe.multiplier <= 30) )
        disp := show_30m and time_disp
        disp1 := show_30m_1 and time_disp
        disp2 := show_30m_2 and time_disp
        disp3 := show_30m_3 and time_disp
    if tf == "60"
        time_disp = ( timeframe.isseconds or (timeframe.isminutes and timeframe.multiplier <= 60) )
        disp := show_1h and time_disp
        disp1 := show_1h_1 and time_disp
        disp2 := show_1h_2 and time_disp
        disp3 := show_1h_3 and time_disp
    if tf == "120"
        time_disp = ( timeframe.isminutes and timeframe.multiplier <= 120 )
        disp := show_2h and time_disp
        disp1 := show_2h_1 and time_disp
        disp2 := show_2h_2 and time_disp
        disp3 := show_2h_3 and time_disp
    if tf == "240"
        time_disp = ( timeframe.isminutes and timeframe.multiplier <= 240 )
        disp :=  show_4h and time_disp
        disp1 := show_4h_1 and time_disp
        disp2 := show_4h_2 and time_disp
        disp3 := show_4h_3 and time_disp
    if tf == "360"
        time_disp = ( timeframe.isminutes and timeframe.multiplier <= 360 )
        disp :=  show_6h and time_disp
        disp1 := show_6h_1 and time_disp
        disp2 := show_6h_2 and time_disp
        disp3 := show_6h_3 and time_disp
    if tf == "480"
        time_disp = ( timeframe.isminutes and timeframe.multiplier <= 480 )
        disp :=  show_8h and time_disp
        disp1 := show_8h_1 and time_disp
        disp2 := show_8h_2 and time_disp
        disp3 := show_8h_3 and time_disp
    if tf == "720"
        time_disp = ( timeframe.isminutes and timeframe.multiplier <= 720 )
        disp :=  show_12h and time_disp
        disp1 := show_12h_1 and time_disp
        disp2 := show_12h_2 and time_disp
        disp3 := show_12h_3 and time_disp
    if tf == "1D"
        time_disp = ( timeframe.isminutes or timeframe.isdaily and timeframe.multiplier == 1  )
        disp :=  show_1d and time_disp
        disp1 := show_1d_1 and time_disp
        disp2 := show_1d_2 and time_disp
        disp3 := show_1d_3 and time_disp
    if tf == "2D"
        time_disp = ( timeframe.isminutes or timeframe.isdaily and timeframe.multiplier <= 2  )
        disp :=  show_2d and time_disp
        disp1 := show_2d_1 and time_disp
        disp2 := show_2d_2 and time_disp
        disp3 := show_2d_3 and time_disp
    if tf == "3D"
        time_disp = ( timeframe.isminutes or timeframe.isdaily and timeframe.multiplier <= 3  )
        disp :=  show_3d and time_disp
        disp1 := show_3d_1 and time_disp
        disp2 := show_3d_2 and time_disp
        disp3 := show_3d_3 and time_disp
    if tf == "1W"
        time_disp = ( timeframe.isminutes or timeframe.isdaily or timeframe.isweekly )
        disp := show_1w and time_disp
        disp1 := show_1w_1 and time_disp
        disp2 := show_1w_2 and time_disp
        disp3 := show_1w_3 and time_disp
    if tf == "1M"
        time_disp = (  timeframe.isminutes or timeframe.isdaily or timeframe.isweekly or timeframe.ismonthly)
        disp := show_1mo and time_disp
        disp1 := show_1mo_1 and time_disp
        disp2 := show_1mo_2 and time_disp
        disp3 := show_1mo_3 and time_disp
    [disp, disp1, disp2, disp3]


get_tf_by_order(order) =>
    o_move = 0
    ret = "0"
    found = false
    if not show_1m
        o_move := o_move +1
    if order == 1 - o_move
        ret := "1"
        found := true

    if not show_5m
        o_move := o_move +1
    if order == 2 - o_move and not found
        ret := "5"
        found := true

    if not show_15m
        o_move := o_move +1
    if order == 3 - o_move and not found
        ret := "15"
        found := true

    if not show_30m
        o_move := o_move +1
    if order == 4 - o_move and not found
        ret := "30"
        found := true

    if not show_1h
        o_move := o_move +1
    if order == 5 - o_move and not found
        ret := "60"
        found := true

    if not show_2h
        o_move := o_move +1
    if order == 6 - o_move and not found
        ret := "120"
        found := true

    if not show_4h
        o_move := o_move +1
    if order == 7 - o_move and not found
        ret := "240"
        found := true

    if not show_6h
        o_move := o_move +1
    if order == 8 - o_move and not found
        ret := "360"
        found := true

    if not show_8h
        o_move := o_move +1
    if order == 9 - o_move and not found
        ret := "480"
        found := true

    if not show_12h
        o_move := o_move +1
    if order == 10 - o_move and not found
        ret := "720"
        found := true

    if not show_1d
        o_move := o_move +1
    if order == 11 - o_move and not found
        ret := "1D"
        found := true

    if not show_2d
        o_move := o_move +1
    if order == 12 - o_move and not found
        ret := "2D"
        found := true

    if not show_3d
        o_move := o_move +1
    if order == 13 - o_move and not found
        ret := "3D"
        found := true

    if not show_1w
        o_move := o_move +1
    if order == 14 - o_move and not found
        ret := "1W"
        found := true

    if not show_1mo
        o_move := o_move +1
    if order == 15 - o_move and not found
        ret := "1M"
        found := true

    ret


get_tf_name_by_order(order) =>
    o_move = 0
    ret = "0"
    found = false
    if not show_1m
        o_move := o_move +1
    if order == 1 - o_move and not found
        ret := "1m"
        found := true

    if not show_5m
        o_move := o_move +1
    if order == 2 - o_move and not found
        ret := "5m"
        found := true

    if not show_15m
        o_move := o_move +1
    if order == 3 - o_move and not found
        ret := "15m"
        found := true

    if not show_30m
        o_move := o_move +1
    if order == 4 - o_move and not found
        ret := "30m"
        found := true

    if not show_1h
        o_move := o_move +1
    if order == 5 - o_move and not found
        ret := "1H"
        found := true

    if not show_2h
        o_move := o_move +1
    if order == 6 - o_move and not found
        ret := "2H"
        found := true

    if not show_4h
        o_move := o_move +1
    if order == 7 - o_move and not found
        ret := "4H"
        found := true

    if not show_6h
        o_move := o_move +1
    if order == 8 - o_move and not found
        ret := "6H"
        found := true

    if not show_8h
        o_move := o_move +1
    if order == 9 - o_move and not found
        ret := "8H"
        found := true

    if not show_12h
        o_move := o_move +1
    if order == 10 - o_move and not found
        ret := "12H"
        found := true

    if not show_1d
        o_move := o_move +1
    if order == 11 - o_move and not found
        ret := "1D"
        found := true

    if not show_2d
        o_move := o_move +1
    if order == 12 - o_move and not found
        ret := "2D"
        found := true

    if not show_3d
        o_move := o_move +1
    if order == 13 - o_move and not found
        ret := "3D"
        found := true

    if not show_1w
        o_move := o_move +1
    if order == 14 - o_move and not found
        ret := "1W"
        found := true

    if not show_1mo
        o_move := o_move +1
    if order == 15 - o_move and not found
        ret := "1M"
        found := true

    ret



get_color_by_order(order) =>
    o_move = 0
    ret = c1M
    found = false
    if not show_1m
        o_move := o_move +1
    if order == 1 - o_move and not found
        ret := c1m
        found := true

    if not show_5m
        o_move := o_move +1
    if order == 2 - o_move and not found
        ret := c5m
        found := true

    if not show_15m
        o_move := o_move +1
    if order == 3 - o_move and not found
        ret := c15m
        found := true

    if not show_30m
        o_move := o_move +1
    if order == 4 - o_move and not found
        ret := c30m
        found := true

    if not show_1h
        o_move := o_move +1
    if order == 5 - o_move and not found
        ret := c1h
        found := true

    if not show_2h
        o_move := o_move +1
    if order == 6 - o_move and not found
        ret := c2h
        found := true

    if not show_4h
        o_move := o_move +1
    if order == 7 - o_move and not found
        ret := c4h
        found := true

    if not show_6h
        o_move := o_move +1
    if order == 8 - o_move and not found
        ret := c6h
        found := true

    if not show_8h
        o_move := o_move +1
    if order == 9 - o_move and not found
        ret := c8h
        found := true

    if not show_12h
        o_move := o_move +1
    if order == 10 - o_move and not found
        ret := c12h
        found := true

    if not show_1d
        o_move := o_move +1
    if order == 11 - o_move and not found
        ret := c1D
        found := true

    if not show_2d
        o_move := o_move +1
    if order == 12 - o_move and not found
        ret := c2D
        found := true

    if not show_3d
        o_move := o_move +1
    if order == 13 - o_move and not found
        ret := c3D
        found := true

    if not show_1w
        o_move := o_move +1
    if order == 14 - o_move and not found
        ret := c1W
        found := true

//    if not show_1mo
//        o_move := o_move +1
//    if order == 10 - o_move and not found
//        ret := "1M"

    ret




get_ratio_by_order(order) =>
    3



identify_tf_order(tf) =>
    ( timeframe.isseconds or (timeframe.isminutes and timeframe.multiplier == 1) ) ? 1
      : (timeframe.isminutes and timeframe.multiplier > 1 and timeframe.multiplier <= 5) ? 2
      : (timeframe.isminutes and timeframe.multiplier > 5 and timeframe.multiplier <= 15) ? 3
      : (timeframe.isminutes and timeframe.multiplier > 15 and timeframe.multiplier <= 30) ? 4
      : (timeframe.isminutes and timeframe.multiplier > 30 and timeframe.multiplier <= 60) ? 5
      : (timeframe.isminutes and timeframe.multiplier > 60 and timeframe.multiplier <= 120) ? 6
      : (timeframe.isminutes and timeframe.multiplier > 120 and timeframe.multiplier <= 240) ? 7
      : (timeframe.isminutes and timeframe.multiplier > 240 and timeframe.multiplier <= 360) ? 8
      : (timeframe.isminutes and timeframe.multiplier > 360 and timeframe.multiplier <= 480) ? 9
      : (timeframe.isminutes and timeframe.multiplier > 480 and timeframe.multiplier <= 720) ? 10
      : (timeframe.isminutes and timeframe.multiplier > 720 or timeframe.isdaily and timeframe.multiplier == 1 ) ? 11
      : (timeframe.isminutes or timeframe.isdaily and timeframe.multiplier == 2 ) ? 12
      : (timeframe.isminutes or timeframe.isdaily and timeframe.multiplier == 3 ) ? 13
      : (timeframe.isdaily and timeframe.multiplier > 3 or timeframe.isweekly and timeframe.multiplier == 1 ) ? 14
      : 15



get_tf_params(tf, instance) =>
    tf_order = identify_tf_order(tf)
    disp_order = tf_order + instance - 1
    [get_tf_by_order(disp_order), get_tf_name_by_order(disp_order), get_color_by_order(disp_order), get_ratio_by_order(disp_order) ]

draw_label(interval_name, len, fnc, label_shift, y, cm, display = false) =>
    if display and show_label
        _lab = label.new(text=interval_name+ " " + str.tostring(len) + " " + fnc ,x = bar_index+label_shift, y=y, style=label.style_label_left)
        label.set_textcolor(id=_lab, textcolor=cm)
        label.set_color(id=_lab, color=color.new(color.black, 100))
        array.push(realTimeLabels, _lab)

draw_future_line(x1, y1, x2, y2, lw, cm) =>
    _line = line.new(
      x1 = x1, y1 = y1, x2 = x2, y2 = y2,
      xloc=xloc.bar_time, style=line.style_dotted, width=lw, color=cm
      )
    array.push(realTimeLines, _line)


draw_future_lines(future, future_intervals_draw, time_next, time_diff, lw, cm, display = false) =>
    fid = future_intervals_draw
    if fid < draw_by
        fid := draw_by
    if display and show_extend
        i=draw_by
        while i+draw_by-1 < array.size(future) and i <= fid
            x1 = i<draw_by+1?time:time_next+time_diff*(i-draw_by-1)
            y1 = array.get(future, i-draw_by)
            x2 = time_next+time_diff*(i-1)
            y2 = array.get(future, i)
            draw_future_line(x1, y1, x2, y2, lw, cm)
            i+=draw_by



label_shift = 5
label_shift_change = 4
future_intervals_draw = future_intervals


[intrvl, interval_name, cm, ratio_to_prev] = get_tf_params(timeframe.period, 1)

[isDisplayable, isDisplayable1, isDisplayable2, isDisplayable3] = is_displayable(intrvl)

[data1, future1, data2, future2, data3, future3, time_diff, time_next] = request.security(syminfo.tickerid, isDisplayable?intrvl:timeframe.period, get_lines_data(srcInput,len1,len2,len3, future_intervals_draw,isDisplayable, barstate.islast))

plot(isDisplayable and isDisplayable1 and show_len1 ?data1:na, color=cm, linewidth = lw1, style=pstyle)
plot(isDisplayable and isDisplayable2 and show_len2 ?data2:na, color=cm, linewidth = lw2, style=pstyle)
plot(isDisplayable and isDisplayable3 and show_len3 ?data3:na, color=cm, linewidth = lw3, style=pstyle)

if barstate.islast and isDisplayable
    f1 = array.new_float()
    i=0
    while i < array.size(future1)
        array.push(f1, array.get(future1, i))
        i += 1
    f2 = array.new_float()
    i:=0
    while i < array.size(future2)
        array.push(f2, array.get(future2, i))
        i += 1
    f3 = array.new_float()
    i:=0
    while i < array.size(future3)
        array.push(f3, array.get(future3, i))
        i += 1

    draw_future_lines(f1, future_intervals_draw, time_next, time_diff, lw1, cm, show_len1 and isDisplayable1)
    draw_future_lines(f2, future_intervals_draw, time_next, time_diff, lw2, cm, show_len2 and isDisplayable2)
    draw_future_lines(f3, future_intervals_draw, time_next, time_diff, lw3, cm, show_len3 and isDisplayable3)
    draw_label(interval_name, len1, fnc, label_shift, data1, cm, show_len1 and isDisplayable1)
    draw_label(interval_name, len2, fnc, label_shift, data2, cm, show_len2 and isDisplayable2)
    draw_label(interval_name, len3, fnc, label_shift, data3, cm, show_len3 and isDisplayable3)

    label_shift += label_shift_change





if true
    [intrvl, _interval_name, _cm, _ratio_to_prev] = get_tf_params(timeframe.period, 2)
    interval_name := _interval_name
    cm := _cm
    ratio_to_prev := _ratio_to_prev

    [_isDisplayable, _isDisplayable1, _isDisplayable2, _isDisplayable3] = is_displayable(intrvl)
    isDisplayable := _isDisplayable
    isDisplayable1 := _isDisplayable1
    isDisplayable2 := _isDisplayable2
    isDisplayable3 := _isDisplayable3

    if isDisplayable
        future_intervals_draw := math.ceil(future_intervals_draw/2.5) + draw_by

    [_data1, _future1, _data2, _future2, _data3, _future3, _time_diff, _time_next] = request.security(syminfo.tickerid, isDisplayable?intrvl:timeframe.period, get_lines_data(srcInput,len1,len2,len3, future_intervals_draw,isDisplayable, barstate.islast))
    data1   := _data1
    future1 := _future1
    data2   := _data2
    future2 := _future2
    data3   := _data3
    future3 := _future3
    time_diff := _time_diff
    time_next := _time_next

plot(isDisplayable and isDisplayable1 and show_len1 ?data1:na, color=cm, linewidth = lw1, style=pstyle)
plot(isDisplayable and isDisplayable2 and show_len2 ?data2:na, color=cm, linewidth = lw2, style=pstyle)
plot(isDisplayable and isDisplayable3 and show_len3 ?data3:na, color=cm, linewidth = lw3, style=pstyle)

if barstate.islast and isDisplayable
    f1 = array.new_float()
    i=0
    while i < array.size(future1)
        array.push(f1, array.get(future1, i))
        i += 1
    f2 = array.new_float()
    i:=0
    while i < array.size(future2)
        array.push(f2, array.get(future2, i))
        i += 1
    f3 = array.new_float()
    i:=0
    while i < array.size(future3)
        array.push(f3, array.get(future3, i))
        i += 1

    draw_future_lines(f1, future_intervals_draw, time_next, time_diff, lw1, cm, show_len1 and isDisplayable1)
    draw_future_lines(f2, future_intervals_draw, time_next, time_diff, lw2, cm, show_len2 and isDisplayable2)
    draw_future_lines(f3, future_intervals_draw, time_next, time_diff, lw3, cm, show_len3 and isDisplayable3)
    draw_label(interval_name, len1, fnc, label_shift, data1, cm, show_len1 and isDisplayable1)
    draw_label(interval_name, len2, fnc, label_shift, data2, cm, show_len2 and isDisplayable2)
    draw_label(interval_name, len3, fnc, label_shift, data3, cm, show_len3 and isDisplayable3)

    label_shift += label_shift_change






if true
    [intrvl, _interval_name, _cm, _ratio_to_prev] = get_tf_params(timeframe.period, 3)
    interval_name := _interval_name
    cm := _cm
    ratio_to_prev := _ratio_to_prev

    [_isDisplayable, _isDisplayable1, _isDisplayable2, _isDisplayable3] = is_displayable(intrvl)
    isDisplayable := _isDisplayable
    isDisplayable1 := _isDisplayable1
    isDisplayable2 := _isDisplayable2
    isDisplayable3 := _isDisplayable3

    if isDisplayable
        future_intervals_draw := math.ceil(future_intervals_draw/2.5) + draw_by

    [_data1, _future1, _data2, _future2, _data3, _future3, _time_diff, _time_next] = request.security(syminfo.tickerid, isDisplayable?intrvl:timeframe.period, get_lines_data(srcInput,len1,len2,len3, future_intervals_draw,isDisplayable, barstate.islast))
    data1   := _data1
    future1 := _future1
    data2   := _data2
    future2 := _future2
    data3   := _data3
    future3 := _future3
    time_diff := _time_diff
    time_next := _time_next

plot(isDisplayable and isDisplayable1 and show_len1 ?data1:na, color=cm, linewidth = lw1, style=pstyle)
plot(isDisplayable and isDisplayable2 and show_len2 ?data2:na, color=cm, linewidth = lw2, style=pstyle)
plot(isDisplayable and isDisplayable3 and show_len3 ?data3:na, color=cm, linewidth = lw3, style=pstyle)

if barstate.islast and isDisplayable
    f1 = array.new_float()
    i=0
    while i < array.size(future1)
        array.push(f1, array.get(future1, i))
        i += 1
    f2 = array.new_float()
    i:=0
    while i < array.size(future2)
        array.push(f2, array.get(future2, i))
        i += 1
    f3 = array.new_float()
    i:=0
    while i < array.size(future3)
        array.push(f3, array.get(future3, i))
        i += 1

    draw_future_lines(f1, future_intervals_draw, time_next, time_diff, lw1, cm, show_len1 and isDisplayable1)
    draw_future_lines(f2, future_intervals_draw, time_next, time_diff, lw2, cm, show_len2 and isDisplayable2)
    draw_future_lines(f3, future_intervals_draw, time_next, time_diff, lw3, cm, show_len3 and isDisplayable3)
    draw_label(interval_name, len1, fnc, label_shift, data1, cm, show_len1 and isDisplayable1)
    draw_label(interval_name, len2, fnc, label_shift, data2, cm, show_len2 and isDisplayable2)
    draw_label(interval_name, len3, fnc, label_shift, data3, cm, show_len3 and isDisplayable3)

    label_shift += label_shift_change







if true
    [intrvl, _interval_name, _cm, _ratio_to_prev] = get_tf_params(timeframe.period, 4)
    interval_name := _interval_name
    cm := _cm
    ratio_to_prev := _ratio_to_prev

    [_isDisplayable, _isDisplayable1, _isDisplayable2, _isDisplayable3] = is_displayable(intrvl)
    isDisplayable := _isDisplayable
    isDisplayable1 := _isDisplayable1
    isDisplayable2 := _isDisplayable2
    isDisplayable3 := _isDisplayable3

    if isDisplayable
        future_intervals_draw := math.ceil(future_intervals_draw/2.5) + draw_by

    [_data1, _future1, _data2, _future2, _data3, _future3, _time_diff, _time_next] = request.security(syminfo.tickerid, isDisplayable?intrvl:timeframe.period, get_lines_data(srcInput,len1,len2,len3, future_intervals_draw,isDisplayable, barstate.islast))
    data1   := _data1
    future1 := _future1
    data2   := _data2
    future2 := _future2
    data3   := _data3
    future3 := _future3
    time_diff := _time_diff
    time_next := _time_next

plot(isDisplayable and isDisplayable1 and show_len1 ?data1:na, color=cm, linewidth = lw1, style=pstyle)
plot(isDisplayable and isDisplayable2 and show_len2 ?data2:na, color=cm, linewidth = lw2, style=pstyle)
plot(isDisplayable and isDisplayable3 and show_len3 ?data3:na, color=cm, linewidth = lw3, style=pstyle)

if barstate.islast and isDisplayable
    f1 = array.new_float()
    i=0
    while i < array.size(future1)
        array.push(f1, array.get(future1, i))
        i += 1
    f2 = array.new_float()
    i:=0
    while i < array.size(future2)
        array.push(f2, array.get(future2, i))
        i += 1
    f3 = array.new_float()
    i:=0
    while i < array.size(future3)
        array.push(f3, array.get(future3, i))
        i += 1

    draw_future_lines(f1, future_intervals_draw, time_next, time_diff, lw1, cm, show_len1 and isDisplayable1)
    draw_future_lines(f2, future_intervals_draw, time_next, time_diff, lw2, cm, show_len2 and isDisplayable2)
    draw_future_lines(f3, future_intervals_draw, time_next, time_diff, lw3, cm, show_len3 and isDisplayable3)
    draw_label(interval_name, len1, fnc, label_shift, data1, cm, show_len1 and isDisplayable1)
    draw_label(interval_name, len2, fnc, label_shift, data2, cm, show_len2 and isDisplayable2)
    draw_label(interval_name, len3, fnc, label_shift, data3, cm, show_len3 and isDisplayable3)

    label_shift += label_shift_change








if true
    [intrvl, _interval_name, _cm, _ratio_to_prev] = get_tf_params(timeframe.period, 5)
    interval_name := _interval_name
    cm := _cm
    ratio_to_prev := _ratio_to_prev

    [_isDisplayable, _isDisplayable1, _isDisplayable2, _isDisplayable3] = is_displayable(intrvl)
    isDisplayable := _isDisplayable
    isDisplayable1 := _isDisplayable1
    isDisplayable2 := _isDisplayable2
    isDisplayable3 := _isDisplayable3

    if isDisplayable
        future_intervals_draw := math.ceil(future_intervals_draw/2.5) + draw_by

    [_data1, _future1, _data2, _future2, _data3, _future3, _time_diff, _time_next] = request.security(syminfo.tickerid, isDisplayable?intrvl:timeframe.period, get_lines_data(srcInput,len1,len2,len3, future_intervals_draw,isDisplayable, barstate.islast))
    data1   := _data1
    future1 := _future1
    data2   := _data2
    future2 := _future2
    data3   := _data3
    future3 := _future3
    time_diff := _time_diff
    time_next := _time_next

plot(isDisplayable and isDisplayable1 and show_len1 ?data1:na, color=cm, linewidth = lw1, style=pstyle)
plot(isDisplayable and isDisplayable2 and show_len2 ?data2:na, color=cm, linewidth = lw2, style=pstyle)
plot(isDisplayable and isDisplayable3 and show_len3 ?data3:na, color=cm, linewidth = lw3, style=pstyle)

if barstate.islast and isDisplayable
    f1 = array.new_float()
    i=0
    while i < array.size(future1)
        array.push(f1, array.get(future1, i))
        i += 1
    f2 = array.new_float()
    i:=0
    while i < array.size(future2)
        array.push(f2, array.get(future2, i))
        i += 1
    f3 = array.new_float()
    i:=0
    while i < array.size(future3)
        array.push(f3, array.get(future3, i))
        i += 1

    draw_future_lines(f1, future_intervals_draw, time_next, time_diff, lw1, cm, show_len1 and isDisplayable1)
    draw_future_lines(f2, future_intervals_draw, time_next, time_diff, lw2, cm, show_len2 and isDisplayable2)
    draw_future_lines(f3, future_intervals_draw, time_next, time_diff, lw3, cm, show_len3 and isDisplayable3)
    draw_label(interval_name, len1, fnc, label_shift, data1, cm, show_len1 and isDisplayable1)
    draw_label(interval_name, len2, fnc, label_shift, data2, cm, show_len2 and isDisplayable2)
    draw_label(interval_name, len3, fnc, label_shift, data3, cm, show_len3 and isDisplayable3)

    label_shift += label_shift_change








if true
    [intrvl, _interval_name, _cm, _ratio_to_prev] = get_tf_params(timeframe.period, 6)
    interval_name := _interval_name
    cm := _cm
    ratio_to_prev := _ratio_to_prev

    [_isDisplayable, _isDisplayable1, _isDisplayable2, _isDisplayable3] = is_displayable(intrvl)
    isDisplayable := _isDisplayable
    isDisplayable1 := _isDisplayable1
    isDisplayable2 := _isDisplayable2
    isDisplayable3 := _isDisplayable3

    if isDisplayable
        future_intervals_draw := math.ceil(future_intervals_draw/2.5) + draw_by

    [_data1, _future1, _data2, _future2, _data3, _future3, _time_diff, _time_next] = request.security(syminfo.tickerid, isDisplayable?intrvl:timeframe.period, get_lines_data(srcInput,len1,len2,len3, future_intervals_draw,isDisplayable, barstate.islast))
    data1   := _data1
    future1 := _future1
    data2   := _data2
    future2 := _future2
    data3   := _data3
    future3 := _future3
    time_diff := _time_diff
    time_next := _time_next

plot(isDisplayable and isDisplayable1 and show_len1 ?data1:na, color=cm, linewidth = lw1, style=pstyle)
plot(isDisplayable and isDisplayable2 and show_len2 ?data2:na, color=cm, linewidth = lw2, style=pstyle)
plot(isDisplayable and isDisplayable3 and show_len3 ?data3:na, color=cm, linewidth = lw3, style=pstyle)

if barstate.islast and isDisplayable
    f1 = array.new_float()
    i=0
    while i < array.size(future1)
        array.push(f1, array.get(future1, i))
        i += 1
    f2 = array.new_float()
    i:=0
    while i < array.size(future2)
        array.push(f2, array.get(future2, i))
        i += 1
    f3 = array.new_float()
    i:=0
    while i < array.size(future3)
        array.push(f3, array.get(future3, i))
        i += 1

    draw_future_lines(f1, future_intervals_draw, time_next, time_diff, lw1, cm, show_len1 and isDisplayable1)
    draw_future_lines(f2, future_intervals_draw, time_next, time_diff, lw2, cm, show_len2 and isDisplayable2)
    draw_future_lines(f3, future_intervals_draw, time_next, time_diff, lw3, cm, show_len3 and isDisplayable3)
    draw_label(interval_name, len1, fnc, label_shift, data1, cm, show_len1 and isDisplayable1)
    draw_label(interval_name, len2, fnc, label_shift, data2, cm, show_len2 and isDisplayable2)
    draw_label(interval_name, len3, fnc, label_shift, data3, cm, show_len3 and isDisplayable3)

    label_shift += label_shift_change

