Multi-timeframe Moving Average with Summary Table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © john_everist

//@version=5
indicator("Multi-timeframe Moving Average with Summary Table", overlay = true)

get_colour(ave_tr1, tolerance, price_close, ma, ma_back,        col_up_above,col_down_below,col_flat, col_text_flat, col_text_flat_above, col_text_flat_below)=>

    ave_tr = (ave_tr1/2)*tolerance



    a = 1-ma / ma_back
    b = 1-ma_back/ma
    compare = a<0?b:    b<0?a:  1

    if syminfo.ticker == "NZDJPY" or syminfo.ticker == "AUDJPY" or syminfo.ticker == "EURJPY" or syminfo.ticker == "CHFJPY" or syminfo.ticker == "GBPJPY" or syminfo.ticker == "CADJPY" or syminfo.ticker == "USDJPY"
        compare := compare * 100

    color colour_text = col_text_flat
    color colour = col_flat
    
    //flat ma
    if compare<ave_tr
        if price_close > ma
            colour_text := col_text_flat_above
        else if price_close < ma
            colour_text := col_text_flat_below
    
    //ma up
    else if ma > ma_back
        colour := col_up_above
        if price_close < ma
            colour_text := col_down_below  
    
    //ma down
    else if ma < ma_back
        colour := col_down_below
        if price_close > ma
            colour_text := col_up_above
        
        

     
    [colour, colour_text]
 
 
 
 
 

make_sma_ema(type, length, tf) =>

    sma = request.security(syminfo.tickerid, tf, ta.sma(close, length))
    ema = request.security(syminfo.tickerid, tf, ta.ema(close, length))

    ma = type == 'SMA' ? sma : ema
    ma


draw_line(ma_value, line_colour, line_width, line_style,line_ex_type) =>

    ex_line = line.new(x1=bar_index[0], x2=bar_index[1], y1=ma_value, y2=ma_value, style=line_style, color=line_colour, width=line_width, extend=line_ex_type)
    line.set_xloc(id=ex_line, x1=time, x2=time + 1, xloc=xloc.bar_time)
    line.delete(ex_line[1])




recalc = input.bool(false, "Recalc")
john = recalc?1:0
var include_override = "Inclusion Overrides"
show_ma_curr = input.bool(false, 'Show ONLY Current timeframe Moving Average', group=include_override)
show_all_ma = input.bool(true, 'Show Moving average from ALL timeframes', group=include_override)
show_hl_curr = input.bool(false, 'Show ONLY Current Moving Average horizontal line', group=include_override)
show_all_hline = input.bool(true, "Show ALL ma horizontal lines", group=include_override)
show_tables = input.bool(true, "Show the summary tables of the options selected below")



var core = 'MA Specifications'
ma_type = input.string(title='Moving Average Type', options=['EMA', 'SMA'], defval='SMA', group=core)
ma_len = input.int(title='Moving Average Length', step=10, defval=20, group=core)
bars_back = input.int(10, title = "Bars back to compare for trend", group=core, tooltip = "How many bars back to compare the current MA level to define a trend on the summary table ")
tolerance = input.float(.4,title = "Tolerance to define 'flat' MA", step = .05, maxval = 100, group=core, tooltip = "A lower tolerance will require a steeper MA to show a trend on the summary table.\n\nWhether the MA is trending or not is defined by comparing the difference between the current level and level at the defined bars back against the average true range")



var line_options = 'MA Line Inclusion'
show_ma_3 = input.bool(title='Show 3M MA Line', defval=false, group=line_options)
show_ma_15 = input.bool(title='Show 15M MA Line', defval=false, group=line_options)
show_ma_60 = input.bool(title='Show 60M MA Line', defval=false, group=line_options)
show_ma_240 = input.bool(title='Show 240M MA Line', defval=false, group=line_options)
show_ma_D = input.bool(title='Show D MA Line', defval=false, group=line_options)
show_ma_W = input.bool(title='Show W MA Line', defval=false, group=line_options)
show_ma_M = input.bool(title='Show M MA Line', defval=false, group=line_options)



var hor_line_options = 'Horizontal Line Inclusion'

show_hl_3 = input.bool(title='Show 3M MA Line', defval=false, group=hor_line_options)
show_hl_15 = input.bool(title='Show 15M MA Line', defval=false, group=hor_line_options)
show_hl_60 = input.bool(title='Show 60M MA Line', defval=false, group=hor_line_options)
show_hl_240 = input.bool(title='Show 240M MA Line', defval=false, group=hor_line_options)
show_hl_D = input.bool(title='Show D MA Line', defval=false, group=hor_line_options)
show_hl_W = input.bool(title='Show W MA Line', defval=false, group=hor_line_options)
show_hl_M = input.bool(title='Show M MA Line', defval=false, group=hor_line_options)



var table_options = 'MA Table Inclusion'
show_ma_3_table = input.bool(title='Show 3M MA Table', defval=true, group=table_options)
show_ma_15_table = input.bool(title='Show 15M MA Table', defval=true, group=table_options)
show_ma_60_table = input.bool(title='Show 60M MA Table', defval=true, group=table_options)
show_ma_240_table = input.bool(title='Show 240M MA Table', defval=true, group=table_options)
show_ma_D_table = input.bool(title='Show D MA Table', defval=true, group=table_options)
show_ma_W_table = input.bool(title='Show W MA Table', defval=false, group=table_options)
show_ma_M_table = input.bool(title='Show M MA Table', defval=false, group=table_options)







var table_format = "Table Format"
color boarder_col = na
color col_text_flat = input.color(color.new(#000000,0), title = "Text Colour", group = table_format)
col_down_below1 = input.color(color.new(#FF0000, 0), title = "Colour for MA down", group = table_format)
col_up_above1 = input.color(color.new(#00FF00, 0), title = "Colour for MA up", group = table_format)
col_flat1 = input.color(color.gray,  title = "Colour for MA when MA is 'flat'", group = table_format, tooltip = "The definition of a flat MA is controlled with the tolerance")


var spacing_d = 'Table Shuffle Down'
spacer_down_1 = input.bool(true, "Add Down Spacer", group = spacing_d, tooltip = "Use to move the table")
spacer_down_2 = input.bool(false, "Add Down Spacer", group = spacing_d)
spacer_down_3 = input.bool(false, "Add Down Spacer", group = spacing_d)
spacer_down_4 = input.bool(false, "Add Down Spacer", group = spacing_d)
var spacing_a = 'Table Shuffle Across'
spacer_across_1 = input.bool(false, "Add Across Spacer", group = spacing_a)
spacer_across_2 = input.bool(false, "Add Across Spacer", group = spacing_a)
spacer_across_3 = input.bool(false, "Add Across Spacer", group = spacing_a)
spacer_across_4 = input.bool(false, "Add Across Spacer", group = spacing_a)



transp = input.int(30, title = "Summary Table Transparency", step = 5, group = table_format)

cell_height = input.int(5, title = "Summary Table Cell Height", group = table_format)
cell_width = input.int(3, title = "Summary Table Cell Width", group = table_format)


col_down_below = color.new(col_down_below1, transp)
col_up_above = color.new(col_up_above1, transp)

col_flat = color.new(col_flat1, transp)


color col_text_flat_above = col_up_above
color col_text_below = col_down_below





t_size_temp = input.string('normal', title='Text Size', options=['tiny', 'small', 'normal', 'large', 'huge'], group = table_format)
t_size = t_size_temp == 'tiny' ? size.tiny : t_size_temp == 'small' ? size.small : t_size_temp == 'normal' ? size.normal : t_size_temp == 'large' ? size.large : t_size_temp == 'huge' ? size.huge : size.normal
b_width = 0
h_align = text.align_center
v_align = text.align_center




show_ma_len = input.bool(false, "Show MA Length", group = table_format)




var ma_line = 'MA Format'
ma_color1 = input.color(title='Moving Average Colour', defval=color.new(color.yellow, 50), group=ma_line)
ma_transp = input.int(50, title = "Moving Average Transparency", step = 5, group = ma_line)
ma_color = color.new(ma_color1, ma_transp)

ma_width = input.int(title='Moving Average Width', defval=1, step = 1, group=ma_line)


line_width = input.int(title='MA Line Width', defval=1, group=ma_line)


var ma_ex_line = 'Horizontal Line Format'
line_style_temp = input.string("dashed", title='Horizontsal line style', options=['solid', 'dashed', 'dotted'], group=ma_ex_line)
line_style = line_style_temp == 'solid' ? line.style_solid : line_style_temp == 'dashed' ? line.style_dashed : line_style_temp == 'dotted' ? line.style_dotted : na

line_ex_type_temp= input.string("Both", title = "MA line extention type", options =  ["Left", "Right", "Both"], group=ma_ex_line)
line_ex_type =line_ex_type_temp == "Left" ? extend.left : line_ex_type_temp == "Right" ? extend.right : line_ex_type_temp == "Both" ? extend.both :extend.none

ex_line_width = input.int(title='horizontal line Width', defval=1, step = 1, group=ma_ex_line)


var label = 'labels'
show_ma_lab = input.bool(title='Show Moving Average Labels', defval=true, group=label)
lab_descrip = input.bool(false, title = "Include Description on label")
lab_size_ma_temp = input.string('small', title='Moving Average Label Size', options=['tiny', 'small', 'normal', 'large', 'huge'], group=label)
lab_size_ma = lab_size_ma_temp == 'tiny' ? size.tiny : lab_size_ma_temp == 'small' ? size.small : lab_size_ma_temp == 'normal' ? size.normal : lab_size_ma_temp == 'large' ? size.large : lab_size_ma_temp == 'huge' ? size.huge : size.normal
text_shift = input.int(0, title='Label Distance from MA', group=label, tooltip = "Increase the distance of the label from the MA", step = 1)

var check = 'Check trend definition'
check_trend = input.bool(false, "Check trend definition", group = check, tooltip = "This will highlight the background based on the trend definition of the current timeframe. Its clearest if you high the bars and only show the ma")


  




ave_tr_length = 20
[close_3, sma_3, sma_3_back, ema_3, ema_3_back, atr_3] =                request.security("","3",        [close, ta.sma(close, ma_len),  ta.sma(close[bars_back], ma_len),    ta.ema(close, ma_len),     ta.ema(close[bars_back], ma_len),   ta.sma(ta.tr(true),     ave_tr_length)])
[close_15, sma_15, sma_15_back, ema_15, ema_15_back, atr_15] =          request.security("","15",       [close, ta.sma(close, ma_len),  ta.sma(close[bars_back], ma_len),    ta.ema(close, ma_len),     ta.ema(close[bars_back], ma_len),   ta.sma(ta.tr(true),     ave_tr_length)])
[close_60, sma_60, sma_60_back, ema_60, ema_60_back, atr_60] =          request.security("","60",       [close, ta.sma(close, ma_len),  ta.sma(close[bars_back], ma_len),    ta.ema(close, ma_len),     ta.ema(close[bars_back], ma_len),   ta.sma(ta.tr(true),     ave_tr_length)])
[close_240, sma_240, sma_240_back, ema_240, ema_240_back, atr_240] =    request.security("","240",      [close, ta.sma(close, ma_len),  ta.sma(close[bars_back], ma_len),    ta.ema(close, ma_len),     ta.ema(close[bars_back], ma_len),   ta.sma(ta.tr(true),     ave_tr_length)])
[close_D, sma_D, sma_D_back, ema_D, ema_D_back, atr_D] =                request.security("","D",        [close, ta.sma(close, ma_len),  ta.sma(close[bars_back], ma_len),    ta.ema(close, ma_len),     ta.ema(close[bars_back], ma_len),   ta.sma(ta.tr(true),     ave_tr_length)])
[close_W, sma_W, sma_W_back, ema_W, ema_W_back, atr_W] =                request.security("","W",        [close, ta.sma(close, ma_len),  ta.sma(close[bars_back], ma_len),    ta.ema(close, ma_len),     ta.ema(close[bars_back], ma_len),   ta.sma(ta.tr(true),     ave_tr_length)])
[close_M, sma_M, sma_M_back, ema_M, ema_M_back, atr_M] =                request.security("","M",        [close, ta.sma(close, ma_len),  ta.sma(close[bars_back], ma_len),    ta.ema(close, ma_len),     ta.ema(close[bars_back], ma_len),   ta.sma(ta.tr(true),     ave_tr_length)])

ma_3 = ma_type == "SMA" ? sma_3 : ema_3 
ma_15 = ma_type == "SMA" ? sma_15 : ema_15 
ma_60 = ma_type == "SMA" ? sma_60 : ema_60 
ma_240 = ma_type == "SMA" ? sma_240 : ema_240 
ma_D = ma_type == "SMA" ? sma_D : ema_D 
ma_W = ma_type == "SMA" ? sma_W : ema_W 
ma_M = ma_type == "SMA" ? sma_M : ema_M 

ma_3_back = ma_type == "SMA" ? sma_3_back : ema_3_back 
ma_15_back = ma_type == "SMA" ? sma_15_back : ema_15_back 
ma_60_back = ma_type == "SMA" ? sma_60_back : ema_60_back 
ma_240_back = ma_type == "SMA" ? sma_240_back : ema_240_back 
ma_D_back = ma_type == "SMA" ? sma_D_back : ema_D_back 
ma_W_back = ma_type == "SMA" ? sma_W_back : ema_W_back 
ma_M_back = ma_type == "SMA" ? sma_M_back : ema_M_back 





[ma_3_col, ma_3_col_text] = get_colour(atr_3, tolerance, close_3, ma_3, ma_3_back, col_up_above,col_down_below,col_flat,  col_text_flat, col_text_flat_above, col_text_below)
[ma_15_col, ma_15_col_text] = get_colour(atr_15, tolerance, close_15, ma_15, ma_15_back, col_up_above,col_down_below,col_flat,  col_text_flat, col_text_flat_above, col_text_below)
[ma_60_col, ma_60_col_text] = get_colour(atr_60, tolerance, close_60, ma_60, ma_60_back, col_up_above,col_down_below,col_flat,  col_text_flat, col_text_flat_above, col_text_below)
[ma_240_col, ma_240_col_text] = get_colour(atr_240, tolerance, close_240, ma_240, ma_240_back, col_up_above,col_down_below,col_flat,  col_text_flat, col_text_flat_above, col_text_below)
[ma_D_col, ma_D_col_text] = get_colour(atr_D, tolerance, close_D, ma_D, ma_D_back, col_up_above,col_down_below,col_flat,  col_text_flat, col_text_flat_above, col_text_below)
[ma_W_col, ma_W_col_text] = get_colour(atr_W, tolerance, close_W, ma_W, ma_W_back, col_up_above,col_down_below,col_flat,  col_text_flat, col_text_flat_above, col_text_below)
[ma_M_col, ma_M_col_text] = get_colour(atr_M, tolerance, close_M, ma_M, ma_M_back, col_up_above,col_down_below,col_flat,  col_text_flat, col_text_flat_above, col_text_below)



color curr_colour = na



if not show_tables
    show_ma_3_table := false
    show_ma_15_table := false
    show_ma_60_table := false
    show_ma_240_table := false
    show_ma_D_table := false
    show_ma_W_table := false
    show_ma_M_table := false




if check_trend
    show_ma_curr := true
    
    show_ma_3_table := false
    show_ma_15_table := false
    show_ma_60_table := false
    show_ma_240_table := false
    show_ma_D_table := false
    show_ma_W_table := false
    show_ma_M_table := true
 
 
if show_all_ma
    show_ma_3 := true
    show_ma_15 := true
    show_ma_60 := true
    show_ma_240 := true
    show_ma_D := true
    show_ma_W := true
    show_ma_M := true
 
   
if show_all_hline
    show_hl_3 := true
    show_hl_15 := true
    show_hl_60 := true
    show_hl_240 := true
    show_hl_D := true
    show_hl_W := true
    show_hl_M := true

///------

if show_ma_curr
    show_ma_3 := false
    show_ma_15 := false
    show_ma_60 := false
    show_ma_240 := false
    show_ma_D := false
    show_ma_W := false
    show_ma_M := false


    if timeframe.multiplier == 3
        show_ma_3 := true
        curr_colour := ma_3_col

        
    if timeframe.multiplier == 15
        show_ma_15 := true
        curr_colour := ma_15_col


    if timeframe.multiplier == 60
        show_ma_60 := true
        curr_colour := ma_60_col


    if timeframe.multiplier == 240
        show_ma_240 := true
        curr_colour := ma_240_col


    if timeframe.isdaily
        show_ma_D  := true


    if timeframe.isweekly
        show_ma_W    := true
        curr_colour := ma_W_col


    if timeframe.ismonthly
        show_ma_M   := true 
        curr_colour := ma_M_col










if show_hl_curr
    show_hl_3 := false
    show_hl_15 := false
    show_hl_60 := false
    show_hl_240 := false
    show_hl_D := false
    show_hl_W := false
    show_hl_M := false

    if timeframe.multiplier == 3
        show_hl_3 := true

    if timeframe.multiplier == 15
        show_hl_15 := true

    if timeframe.multiplier == 60
        show_hl_60 := true

    if timeframe.multiplier == 240
        show_hl_240 := true

    if timeframe.isdaily
        show_hl_D := true

    if timeframe.isweekly
        show_hl_W := true

    if timeframe.ismonthly
        show_hl_M := true



bgcolor(check_trend?color.new(curr_colour, 85):na)


ma_table = table.new(position = position.top_left, columns = 5, rows = 11, bgcolor = na, border_color = boarder_col, border_width = 1)
int across = 0
int down = 0
if spacer_down_1
    table.cell(table_id = ma_table, height = cell_height, width = cell_width, column = across, row = down, bgcolor = na, text = "")
    down+=1
if spacer_down_2
    table.cell(table_id = ma_table, height = cell_height, width = cell_width, column = across, row = down, bgcolor = na, text = "")
    down+=1
if spacer_down_3
    table.cell(table_id = ma_table, height = cell_height, width = cell_width, column = across, row = down, bgcolor = na, text = "")
    down+=1
if spacer_down_4
    table.cell(table_id = ma_table, height = cell_height, width = cell_width, column = across, row = down, bgcolor = na, text = "")
    down+=1
    
if spacer_across_1
    table.cell(table_id = ma_table, height = cell_height, width = cell_width, column = across, row = down, bgcolor = na, text = "")
    across+=1
if spacer_across_2
    table.cell(table_id = ma_table, height = cell_height, width = cell_width, column = across, row = down, bgcolor = na, text = "")
    across+=1
if spacer_across_3
    table.cell(table_id = ma_table, height = cell_height, width = cell_width, column = across, row = down, bgcolor = na, text = "")
    across+=1
if spacer_across_4
    table.cell(table_id = ma_table, height = cell_height, width = cell_width, column = across, row = down, bgcolor = na, text = "")
    across+=1 

if show_ma_len
    table.cell(table_id = ma_table, text_color = col_text_flat, height = cell_height, width = cell_width, column =  across+0, row = down+0, bgcolor = na, text = str.tostring(ma_len), text_halign = h_align, text_valign = v_align, text_size = t_size)
    down+=1


if show_ma_3_table
    if timeframe.isintraday and timeframe.multiplier<=3
        table.cell(table_id = ma_table, text_color = ma_3_col_text, height = cell_height, width = cell_width, column = across+0, row = down+0, bgcolor = ma_3_col, text = "3", text_halign = h_align, text_valign = v_align, text_size = t_size)
        down+=1   

if show_ma_15_table
    if timeframe.isintraday and timeframe.multiplier<=15
        table.cell(table_id = ma_table, text_color = ma_15_col_text, height = cell_height, width = cell_width,column = across+0, row = down+0, bgcolor = ma_15_col, text = "15", text_halign = h_align, text_valign = v_align, text_size = t_size)
        down+=1  
if show_ma_60_table
    if timeframe.isintraday and timeframe.multiplier<=60
        table.cell(table_id = ma_table, text_color = ma_60_col_text, height = cell_height, width = cell_width, column = across+0, row = down+0, bgcolor = ma_60_col, text = "1H", text_halign = h_align, text_valign = v_align, text_size = t_size)
        down+=1  
if show_ma_240_table
    if timeframe.isintraday and timeframe.multiplier<=240
        table.cell(table_id = ma_table, text_color = ma_240_col_text, height = cell_height, width = cell_width, column = across+0, row = down+0, bgcolor = ma_240_col, text = "4H", text_halign = h_align, text_valign = v_align, text_size = t_size)
        down+=1  
if show_ma_D_table
    if not timeframe.isweekly and not timeframe.ismonthly
        table.cell(table_id = ma_table, text_color = ma_D_col_text, height = cell_height, width = cell_width, column = across+0, row = down+0, bgcolor = ma_D_col, text = "D", text_halign = h_align, text_valign = v_align, text_size = t_size)
        down+=1  
if show_ma_W_table
    if not timeframe.ismonthly
        table.cell(table_id = ma_table, text_color = ma_W_col_text, height = cell_height, width = cell_width, column = across+0, row = down+0, bgcolor = ma_W_col, text = "W", text_halign = h_align, text_valign = v_align, text_size = t_size)
        down+=1  
if show_ma_M_table
    table.cell(table_id = ma_table, text_color = ma_M_col_text, height = cell_height, width = cell_width, column = across+0, row = down+0, bgcolor = ma_M_col, text = "M", text_halign = h_align, text_valign = v_align, text_size = t_size)
    down+=1  
    
    
//==================================


bool show_lab_3 = false
bool show_lab_15 = false
bool show_lab_60 = false
bool show_lab_240 = false
bool show_lab_D = false
bool show_lab_W = false
bool show_lab_M = false


if show_ma_3
    if timeframe.isintraday and timeframe.multiplier<=3
        show_lab_3 := true

if show_ma_15
    if timeframe.isintraday and timeframe.multiplier<=15
        show_lab_15 := true

if show_ma_60
    if timeframe.isintraday and timeframe.multiplier<=60
        show_lab_60 := true

if show_ma_240
    if timeframe.isintraday and timeframe.multiplier<=240
        show_lab_240 := true

if show_ma_D
    if not timeframe.isweekly and not timeframe.ismonthly
        show_lab_D := true

if show_ma_W
    if not timeframe.ismonthly
        show_lab_W := true

if show_ma_M
    show_lab_M := true










if show_hl_3 
    draw_line(ma_3, ma_color, ex_line_width, line_style, line_ex_type)
    show_lab_3:=true
    
if show_hl_15 
    draw_line(ma_15, ma_color, ex_line_width, line_style, line_ex_type)
    show_lab_15:=true
    
if show_hl_60 
    draw_line(ma_60, ma_color, ex_line_width, line_style, line_ex_type)
    show_lab_60:=true
    
if show_hl_240 
    draw_line(ma_240, ma_color, ex_line_width, line_style, line_ex_type)
    show_lab_240:=true
    
if show_hl_D 
    draw_line(ma_D, ma_color, ex_line_width, line_style, line_ex_type)
    show_lab_D:=true
    
if show_hl_W 
    draw_line(ma_W, ma_color, ex_line_width, line_style, line_ex_type)
    show_lab_W:=true
    
if show_hl_M 
    draw_line(ma_M, ma_color, ex_line_width, line_style, line_ex_type)
    show_lab_M:=true



if show_ma_lab

    invisible = color.new(color.gray, 100)
    lab_style = label.style_label_left
    
    label lab_ma_3 = na
    label lab_ma_15 = na
    label lab_ma_60 = na
    label lab_ma_240 = na
    label lab_ma_D = na
    label lab_ma_W = na
    label lab_ma_M =   na   


    
    if lab_descrip
        lab_ma_3 := label.new(bar_index + text_shift, ma_3, text=      '3 ('+ str.tostring(ma_len) + " "+ ma_type +")",    textcolor=show_lab_3  ? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_15 := label.new(bar_index + text_shift, ma_15, text=    '15 ('+ str.tostring(ma_len) + " "+  ma_type +")",  textcolor=show_lab_15  ? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_60 := label.new(bar_index + text_shift, ma_60, text=    '60 ('+ str.tostring(ma_len) + " "+  ma_type +")",  textcolor=show_lab_60  ? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_240 := label.new(bar_index + text_shift, ma_240, text=  '240 ('+ str.tostring(ma_len) + " "+  ma_type +")", textcolor=show_lab_240  ? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_D := label.new(bar_index + text_shift, ma_D, text=      'D ('+ str.tostring(ma_len) + " "+  ma_type +")",   textcolor=show_lab_D  ? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_W := label.new(bar_index + text_shift, ma_W, text=      "W ("+str.tostring(ma_len) + " "+  ma_type +")",    textcolor=show_lab_W  ? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_M := label.new(bar_index + text_shift, ma_M, text=      'M ('+ str.tostring(ma_len) + " "+  ma_type +")",   textcolor=show_lab_M  ? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)        
    
    else
        lab_ma_3 := label.new(bar_index + text_shift, ma_3, text=      "3",     textcolor= show_lab_3? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_15 := label.new(bar_index + text_shift, ma_15, text=    "15",    textcolor= show_lab_15? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_60 := label.new(bar_index + text_shift, ma_60, text=    "60",    textcolor= show_lab_60? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_240 := label.new(bar_index + text_shift, ma_240, text=  "240",   textcolor= show_lab_240? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_D := label.new(bar_index + text_shift, ma_D, text=      "D",     textcolor= show_lab_D? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_W := label.new(bar_index + text_shift, ma_W, text=      "W",     textcolor= show_lab_W? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)
        lab_ma_M := label.new(bar_index + text_shift, ma_M, text=      "M",     textcolor= show_lab_M? ma_color : invisible, color=invisible, style=lab_style, size=lab_size_ma)

    label.delete(lab_ma_3[1])
    label.delete(lab_ma_15[1])
    label.delete(lab_ma_60[1])
    label.delete(lab_ma_240[1])
    label.delete(lab_ma_D[1])
    label.delete(lab_ma_W[1])
    label.delete(lab_ma_M[1])




//plot(ma_curr, title='current', color=ma_color, linewidth=ma_width)
plot(ma_3, title='3', color=show_ma_3?ma_color:na, linewidth=ma_width)
plot(ma_15, title='15', color=show_ma_15?ma_color:na, linewidth=ma_width)
plot(ma_60, title='60', color=show_ma_60?ma_color:na, linewidth=ma_width)
plot(ma_240, title='240', color=show_ma_240?ma_color:na, linewidth=ma_width)
plot(ma_D, title='d', color=show_ma_D?ma_color:na, linewidth=ma_width)
plot(ma_W, title='w', color=show_ma_W?ma_color:na, linewidth=ma_width)
plot(ma_M, title='m', color=show_ma_M?ma_color:na, linewidth=ma_width)



    