Multi-timeframe Moving Average with Summary Table

mail@pastecode.io avatar
unknown
plain_text
a year ago
46 kB
3
Indexable
Never
// 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("Clean 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(true, '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=200, 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")
src = input(close, title="MA Source 200", group=core)


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
MABgColour = input(color.rgb(146, 147, 150, 51), "MA background Colour", group= table_format)
color col_text_flat = input.color(color.new(#f3efef, 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,transp = 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=2, group=ma_line)


var ma_ex_line = 'Horizontal Line Format'
line_style_temp = input.string("dotted", 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(true, title = "Include Description on label")
lab_size_ma_temp = input.string('normal', 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)



////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                             Calculations for 3m TF                                                       \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

//Calculation for Difference between Price and SMA 200                    \\

sma_3m = request.security(syminfo.tickerid, '3',  ta.sma(src, ma_len))
CurrDiffVar_3m = (src / sma_3m  - 1 ) * 100
CurrDiff_3m = request.security(syminfo.tickerid, '', CurrDiffVar_3m)

//Calculation for Since when and bars the price is above/below 200 SMA

above_since_3m = (time - ta.valuewhen(ta.crossover(close, sma_3m), time, 0))/1000/3600
below_since_3m = (time - ta.valuewhen(ta.crossunder(close, sma_3m), time, 0))/1000/3600
_below_since_3m = (time - ta.valuewhen(ta.crossunder(close, sma_3m), time, 0))/1000/60
_above_since_3m = (time - ta.valuewhen(ta.crossover(close, sma_3m), time, 0))/1000/60

belowSinceStrDaysHrs_3m = str.tostring((below_since_3m-below_since_3m %24)/24, '00') + " days " +str.tostring(below_since_3m %24, '00') + " hrs, BELOW"
aboveSinceStrDaysHrs_3m = str.tostring((above_since_3m-above_since_3m %24)/24, '00') + " days " +str.tostring(above_since_3m %24, '00') + " hrs, ABOVE"

above_since_bar_3m = ta.barssince(close > sma_3m)
below_since_bar_3m = ta.barssince(close < sma_3m)

cond3  =  math.round(src, 5) > math.round(sma_3m, 5)


////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                             Calculations for 15m TF                                                       \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

//Calculation for Difference between Price and SMA 200                    \\
sma_15m = request.security(syminfo.tickerid, '15',  ta.sma(src , ma_len))
CurrDiffVar_15m = (src / sma_15m  - 1 ) * 100
CurrDiff_15m = request.security(syminfo.tickerid, '', CurrDiffVar_15m)

//Calculation for Since when and bars the price is above/below 200 SMA
above_since_15m = (time - ta.valuewhen(ta.crossover(close, sma_15m), time, 0))/1000/3600
below_since_15m = (time - ta.valuewhen(ta.crossunder(close, sma_15m), time, 0))/1000/3600
_below_since_15m = (time - ta.valuewhen(ta.crossunder(close, sma_15m), time, 0))/1000/60
_above_since_15m = (time - ta.valuewhen(ta.crossover(close, sma_15m), time, 0))/1000/60

belowSinceStrDaysHrs_15m = str.tostring((below_since_15m-below_since_15m %24)/24, '00') + " days " +str.tostring(below_since_15m %24, '00') + " hrs, BELOW"
aboveSinceStrDaysHrs_15m = str.tostring((above_since_15m-above_since_15m %24)/24, '00') + " days " +str.tostring(above_since_15m %24, '00') + " hrs, ABOVE"

cond15 =  math.round(close, 1) > math.round(sma_15m, 1)



////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                             Calculations for 1h TF                                                       \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


//Calculation for Difference between Price and SMA 200                    \\
sma_1h = request.security(syminfo.tickerid, '60',  ta.sma(src , ma_len))
CurrDiffVar_1h = (src / sma_1h  - 1 ) * 100
CurrDiff_1h = request.security(syminfo.tickerid, '', CurrDiffVar_1h)

//Calculation for Since when and bars the price is above/below 200 SMA
above_since_1h = (time - ta.valuewhen(ta.crossover(close, sma_1h), time, 0))/1000/3600
below_since_1h = (time - ta.valuewhen(ta.crossunder(close, sma_1h), time, 0))/1000/3600
_below_since_1h = (time - ta.valuewhen(ta.crossunder(close, sma_1h), time, 0))/1000/60
_above_since_1h = (time - ta.valuewhen(ta.crossover(close, sma_1h), time, 0))/1000/60

belowSinceStrDaysHrs_1h = str.tostring((below_since_1h-below_since_1h %24)/24, '00') + " days " +str.tostring(below_since_1h %24, '00') + " hrs, BELOW"
aboveSinceStrDaysHrs_1h = str.tostring((above_since_1h-above_since_1h %24)/24, '00') + " days " +str.tostring(above_since_1h %24, '00') + " hrs, ABOVE"

cond1h =  math.round(close, 1) > math.round(sma_1h, 1)

////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                             Calculations for 4h TF                                                       \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

//Calculation for Difference between Price and SMA 200                    \\
sma_4h = request.security(syminfo.tickerid, '240',  ta.sma(src , 200))
CurrDiffVar_4h = (src / sma_4h  - 1 ) * 100
CurrDiff_4h = request.security(syminfo.tickerid, '', CurrDiffVar_4h)

//Calculation for Since when and bars the price is above/below 200 SMA
above_since_4h = (time - ta.valuewhen(ta.crossover(close, sma_4h), time, 0))/1000/3600
below_since_4h = (time - ta.valuewhen(ta.crossunder(close, sma_4h), time, 0))/1000/3600
_below_since_4h = (time - ta.valuewhen(ta.crossunder(close, sma_4h), time, 0))/1000/60
_above_since_4h = (time - ta.valuewhen(ta.crossover(close, sma_4h), time, 0))/1000/60

belowSinceStrDaysHrs_4h = str.tostring((below_since_4h-below_since_4h %24)/24, '00') + " days " +str.tostring(below_since_4h %24, '00') + " hrs, BELOW"
aboveSinceStrDaysHrs_4h = str.tostring((above_since_4h-above_since_4h %24)/24, '00') + " days " +str.tostring(above_since_4h %24, '00') + " hrs, ABOVE"

cond4h =  math.round(close, 1) > math.round(sma_4h, 1)

////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                             Calculations for D TF                                                       \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

//Calculation for Difference between Price and SMA 200                    \\
sma_d = request.security(syminfo.tickerid, 'D',  ta.sma(src , 200))
CurrDiffVar_d = (src / sma_d  - 1 ) * 100
CurrDiff_d = request.security(syminfo.tickerid, '', CurrDiffVar_d)

//Calculation for Since when and bars the price is above/below 200 SMA
above_since_d = (time - ta.valuewhen(ta.crossover(close, sma_d), time, 0))/1000/3600
below_since_d = (time - ta.valuewhen(ta.crossunder(close, sma_d), time, 0))/1000/3600
_below_since_d = (time - ta.valuewhen(ta.crossunder(close, sma_d), time, 0))/1000/60
_above_since_d = (time - ta.valuewhen(ta.crossover(close, sma_d), time, 0))/1000/60

belowSinceStrDaysHrs_d = str.tostring((below_since_d-below_since_d %24)/24, '00') + " days " +str.tostring(below_since_d %24, '00') + " hrs, BELOW"
aboveSinceStrDaysHrs_d = str.tostring((above_since_d-above_since_d %24)/24, '00') + " days " +str.tostring(above_since_d %24, '00') + " hrs, ABOVE"

condd =  math.round(close, 1) > math.round(sma_d, 1)

////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                             Calculations for W TF                                                       \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

//Calculation for Difference between Price and SMA 200                    \\
sma_w = request.security(syminfo.tickerid, 'W',  ta.sma(src , 200))
CurrDiffVar_w = (src / sma_w  - 1 ) * 100
CurrDiff_w = request.security(syminfo.tickerid, '', CurrDiffVar_w)

//Calculation for Since when and bars the price is above/below 200 SMA
above_since_w = (time - ta.valuewhen(ta.crossover(close, sma_w), time, 0))/1000/3600
below_since_w = (time - ta.valuewhen(ta.crossunder(close, sma_w), time, 0))/1000/3600
_below_since_w = (time - ta.valuewhen(ta.crossunder(close, sma_w), time, 0))/1000/60
_above_since_w = (time - ta.valuewhen(ta.crossover(close, sma_w), time, 0))/1000/60

belowSinceStrDaysHrs_w = str.tostring((below_since_w-below_since_w %24)/24, '00') + " days " +str.tostring(below_since_w %24, '00') + " hrs, BELOW"
aboveSinceStrDaysHrs_w = str.tostring((above_since_w-above_since_w %24)/24, '00') + " days " +str.tostring(above_since_w %24, '00') + " hrs, ABOVE"

condw =  math.round(close, 1) > math.round(sma_w, 1)

////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                             Calculations for M TF                                                       \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


//Calculation for Difference between Price and SMA 200                    \\
sma_m = request.security(syminfo.tickerid, 'M',  ta.sma(src , 200))
CurrDiffVar_m = (src / sma_m  - 1 ) * 100
CurrDiff_m = request.security(syminfo.tickerid, '', CurrDiffVar_m)

//Calculation for Since when and bars the price is above/below 200 SMA
above_since_m = (time - ta.valuewhen(ta.crossover(close, sma_m), time, 0))/1000/3600
below_since_m = (time - ta.valuewhen(ta.crossunder(close, sma_m), time, 0))/1000/3600
_below_since_m = (time - ta.valuewhen(ta.crossunder(close, sma_m), time, 0))/1000/60
_above_since_m = (time - ta.valuewhen(ta.crossover(close, sma_m), time, 0))/1000/60

belowSinceStrDaysHrs_m = str.tostring((below_since_m-below_since_m %24)/24, '00') + " days " +str.tostring(below_since_m %24, '00') + " hrs, BELOW"
aboveSinceStrDaysHrs_m = str.tostring((above_since_m-above_since_m %24)/24, '00') + " days " +str.tostring(above_since_m %24, '00') + " hrs, ABOVE"

condm =  math.round(close, 1) > math.round(sma_m, 1)

////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                                Table Settings                           \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

tablePositionY = input.string('top', title='Y-Position', options=['top', 'middle', 'bottom'], group="Table", inline="2")
tablePositionX = input.string('right', title='X-Position', options=['left', 'center', 'right'], group="Table", inline="2")


ma_table = table.new(tablePositionY + '_' + tablePositionX, columns = 11, rows = 50, bgcolor = na, border_color = boarder_col, border_width = 1)

string CurrString_3m=close>sma_3m?"ABOVE":"BELOW"
string CurrString_15m=close>sma_15m?"ABOVE":"BELOW"
string CurrString_1h=close>sma_1h?"ABOVE":"BELOW"
string CurrString_4h=close>sma_4h?"ABOVE":"BELOW"
string CurrString_d=close>sma_d?"ABOVE":"BELOW"
string CurrString_w=close>sma_w?"ABOVE":"BELOW"
string CurrString_m=close>sma_m?"ABOVE":"BELOW"


table.cell(table_id = ma_table, column = 0, row = 1, bgcolor=color.rgb(175,175,175), width=10, text="Time Frame")
table.cell(table_id = ma_table, column = 1, row = 1, bgcolor=color.rgb(175,175,175), width=10, text="DIFF")
table.cell(table_id = ma_table, column = 2, row = 1, bgcolor=color.rgb(175,175,175), text="Since When")
//table.cell(table_id = ma_table, column = 3, row = 1, bgcolor=color.rgb(175,175,175), text="Bars")
//table.cell(table_id = ma_table, column = 4, row = 1, bgcolor=color.rgb(175,175,175), text="Actual Price")
//table.cell(table_id = ma_table, column = 5, row = 1, bgcolor=color.rgb(175,175,175), text="SMA TF Price")
//table.cell(table_id = ma_table, column = 6, row = 1, bgcolor=color.rgb(175,175,175), text="MA")
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 = 6, row = 2, bgcolor = MABgColour, 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 = 0, row = 2,  bgcolor = ma_3_col, text = "3", text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = ma_3_col_text, height = cell_height, width = cell_width, column = 4, row = 2,text = str.tostring(math.round(src,2)), bgcolor = ma_3_col, text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = ma_3_col_text, height = cell_height, width = cell_width, column = 5, row = 2,text = str.tostring(math.round(sma_3m ,2)), bgcolor = ma_3_col, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white, height = cell_height, width = cell_width, column = 1, row = 2,text = str.tostring(math.round(CurrDiff_3m,2)) + " % " + CurrString_3m, bgcolor= math.round(src, 1) > math.round(sma_3m, 1) ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)  
table.cell(table_id = ma_table, text_color = color.white,  column = 2, row = 2,text = cond3 ? aboveSinceStrDaysHrs_3m:belowSinceStrDaysHrs_3m, bgcolor=cond3 ?  color.green : color.red,text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = color.white,  column = 3, row = 2,text = str.tostring(math.round((cond3 ? below_since_bar_3m:above_since_bar_3m),2)) +' ' + above_below_string_bar_3m, bgcolor=cond3 ?  color.green : color.red, text_size = size.normal)

//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 = 0, row = 3, bgcolor = ma_15_col, text = "15", text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = ma_15_col_text, height = cell_height, width = cell_width, column = 4, row = 3,text = str.tostring(math.round(src,2)), bgcolor =  ma_15_col, text_halign = h_align, text_valign = v_align, text_size = t_size)  
//table.cell(table_id = ma_table, text_color = ma_15_col_text, height = cell_height, width = cell_width, column = 5, row = 3,text = str.tostring(math.round(sma_15m ,2)), bgcolor =  ma_15_col, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white, height = cell_height, width = cell_width, column = 1, row = 3,text = str.tostring(math.round(CurrDiff_15m,2)) + " % " + CurrString_15m, bgcolor= math.round(src, 1) > math.round(sma_15m, 1) ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white,  column = 2, row = 3,text = cond15 ?aboveSinceStrDaysHrs_15m:belowSinceStrDaysHrs_15m, bgcolor=cond15 ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = color.white,  column = 3, row = 3,text = str.tostring(math.round((cond15? below_since_bar_15m:above_since_bar_15m),2)) +' ' + above_below_string_bar_15m, bgcolor=cond15 ?  color.green : color.red, text_size = size.normal)

//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 = 0, row = 4, bgcolor = ma_60_col, text = "1H", text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = ma_60_col_text, height = cell_height, width = cell_width, column = 3, row = 4,text = str.tostring(math.round(src,2)), bgcolor = ma_60_col, text_halign = h_align, text_valign = v_align, text_size = t_size)  
//table.cell(table_id = ma_table, text_color = ma_60_col_text, height = cell_height, width = cell_width, column = 4, row = 4,text = str.tostring(math.round(sma_1h ,2)), bgcolor = ma_60_col, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white, height = cell_height, width = cell_width, column = 1, row = 4,text = str.tostring(math.round(CurrDiff_1h,2)) + " % " + CurrString_1h, bgcolor= math.round(src, 1) > math.round(sma_1h, 1) ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white,  column = 2, row = 4,text = cond1h ?aboveSinceStrDaysHrs_1h:belowSinceStrDaysHrs_1h, bgcolor=cond1h ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)

//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 = 0, row = 5, bgcolor = ma_240_col, text = "4H", text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = ma_240_col_text, height = cell_height, width = cell_width, column = 3, row = 5,text = str.tostring(math.round(src,2)), bgcolor = ma_240_col, text_halign = h_align, text_valign = v_align, text_size = t_size)  
//table.cell(table_id = ma_table, text_color = ma_240_col_text, height = cell_height, width = cell_width, column = 4, row = 5,text = str.tostring(math.round(sma_4h ,2)), bgcolor = ma_240_col, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white, height = cell_height, width = cell_width, column = 1, row = 5,text = str.tostring(math.round(CurrDiff_4h,2)) + " % " + CurrString_4h, bgcolor= math.round(src, 1) > math.round(sma_4h, 1) ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white,  column = 2, row = 5,text = cond4h ?aboveSinceStrDaysHrs_4h:belowSinceStrDaysHrs_4h, bgcolor=cond4h ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)


//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 = 0, row = 6, bgcolor = ma_D_col, text = "D", text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = ma_D_col_text, height = cell_height, width = cell_width, column = 3, row = 6,text = str.tostring(math.round(src,2)), bgcolor = ma_D_col, text_halign = h_align, text_valign = v_align, text_size = t_size)   
//table.cell(table_id = ma_table, text_color = ma_D_col_text, height = cell_height, width = cell_width, column = 4, row = 6,text = str.tostring(math.round(sma_d ,2)), bgcolor = ma_D_col, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white, height = cell_height, width = cell_width, column = 1, row = 6,text = str.tostring(math.round(CurrDiff_d,2)) + " % " + CurrString_d, bgcolor= math.round(src, 1) > math.round(sma_d, 1) ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white,  column = 2, row = 6,text = condd ?aboveSinceStrDaysHrs_d:belowSinceStrDaysHrs_d, bgcolor=condd ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)

//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 = 0, row =7, bgcolor = ma_W_col, text = "W", text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = ma_W_col_text, height = cell_height, width = cell_width, column = 3, row = 7,text = str.tostring(math.round(src,2)), bgcolor = ma_W_col, text_halign = h_align, text_valign = v_align, text_size = t_size)  
//table.cell(table_id = ma_table, text_color = ma_W_col_text, height = cell_height, width = cell_width, column = 4, row = 7,text = str.tostring(math.round(sma_w ,2)), bgcolor = ma_W_col, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white, height = cell_height, width = cell_width, column = 1, row = 7,text = str.tostring(math.round(CurrDiff_w,2)) + " % " + CurrString_w, bgcolor= math.round(src, 1) > math.round(sma_w, 1) ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)             
table.cell(table_id = ma_table, text_color = color.white,  column = 2, row = 7,text = condw ?aboveSinceStrDaysHrs_w:belowSinceStrDaysHrs_w, bgcolor=condw ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)

//if show_ma_M_table
table.cell(table_id = ma_table, text_color = ma_M_col_text, height = cell_height, width = cell_width, column = 0, row = 8, bgcolor = ma_M_col, text = "M", text_halign = h_align, text_valign = v_align, text_size = t_size)
//table.cell(table_id = ma_table, text_color = ma_M_col_text, height = cell_height, width = cell_width, column = 3, row = 8,text = str.tostring(math.round(src,2)), bgcolor = ma_M_col, text_halign = h_align, text_valign = v_align, text_size = t_size)      
//table.cell(table_id = ma_table, text_color = ma_M_col_text, height = cell_height, width = cell_width, column = 4, row = 8,text = str.tostring(math.round(sma_m ,2)), bgcolor = ma_M_col, text_halign = h_align, text_valign = v_align, text_size = t_size)    
table.cell(table_id = ma_table, text_color = color.white, height = cell_height, width = cell_width, column = 1, row = 8,text = str.tostring(math.round(CurrDiff_m,2)) + " % " + CurrString_m, bgcolor= math.round(src, 1) > math.round(sma_m, 1) ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)                
table.cell(table_id = ma_table, text_color = color.white,  column = 2, row = 8,text = condm ?aboveSinceStrDaysHrs_m:belowSinceStrDaysHrs_m, bgcolor=condm ?  color.green : color.red, text_halign = h_align, text_valign = v_align, text_size = t_size)
//==================================


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)