Multi Timeframe Moving Averages
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © pripalda
//@version=5
//Thanks and refferences:
//This script includes code from other tradingview community users. I'd like to thank and mention them abobe: 
//User Frien_dd-DisDev (https://es.tradingview.com/u/Frien_dd-DisDev/)
//https://es.tradingview.com/script/MPZkvXfR-Chart-Champions-Part-1-nPOC-Levels-VWAPs/

indicator(title='Multi Timeframe Moving Averages', shorttitle='MTF MAs', overlay=true)


////////////////////////////////////////////////// Inputs

//Inputs - Moving Averages General Configuration

lenFastEMA = input(20, 'Length', inline='Fast', group='Fast Moving Average (EMA) Configuration:')
srcFastEMA = input(close, 'Source', inline='Fast', group='Fast Moving Average (EMA) Configuration:')
colorFastEMA = input.color(color.white, title='Color', inline='Fast', group='Fast Moving Average (EMA) Configuration:')
showCTFastEMA = input(title='Show on All Timeframes', defval=true, inline='Fast', group='Fast Moving Average (EMA) Configuration:')

lenMidSMA  = input(50, 'Length   ', inline='Mid', group='Middle Moving Average (SMA) Configuration:')
srcMidSMA  = input(close, 'Source', inline='Mid', group='Middle Moving Average (SMA) Configuration:')
colorMidSMA = input.color(color.yellow, title='Color', inline='Mid', group='Middle Moving Average (SMA) Configuration:')
showCTMidSMA = input(title='Show on All Timeframes   ', defval=true, inline='Mid', group='Middle Moving Average (SMA) Configuration:')

lenSlowSMA = input(200, 'Length', inline='Slow', group='Slow Moving Average (SMA) Configuration:')
srcSlowSMA = input(close, 'Source', inline='Slow', group='Slow Moving Average (SMA) Configuration:')
colorSlowSMA = input.color(color.red, title='Color', inline='Slow', group='Slow Moving Average (SMA) Configuration:')
showCTSlowSMA = input(title='Show on All Timeframes', defval=true, inline='Slow', group='Slow Moving Average (SMA) Configuration:')

colorVWAP = input.color(color.aqua, title='Color', inline='VWAP', group='Daily VWAP Configuration:')
showVWAP = input(title='Show on All Timeframes', defval=true, inline='VWAP', group='Daily VWAP Configuration:')
history_VWAP = input(title='Show History', defval=true, inline='VWAP', group='Daily VWAP Configuration:')

//Inputs - Select options for PDC VWAP
colorpdVWAP = input.color(color.fuchsia, title='Color', inline='pdVWAP', group='Previous Day VWAP Close Configuration:')
showpdVWAP = input(title='Show on All Timeframes', defval=true, inline='pdVWAP', group='Previous Day VWAP Close Configuration:')
history_pdVWAP = input(title='Show History', defval=false, inline='pdVWAP', group='Previous Day VWAP Close Configuration:')



//Assign Length and Source from general configuration to all timeframe
//In case we want to config one by one use the code comented 
lenOHFastEMA = lenFastEMA//input(20, 'EMA #1 FH Length')
srcOHFastEMA = srcFastEMA//input(close, 'Source')
lenOHMidSMA = lenMidSMA//input(50, 'MA1 FH Length')
srcOHMidSMA = srcMidSMA//input(close, 'Source')
lenOHSlowSMA = lenSlowSMA//input(200, 'MA2 FH Length')
srcOHSlowSMA = srcSlowSMA//input(close, 'Source')

lenFHFastEMA = lenFastEMA//input(20, 'EMA #1 FH Length')
srcFHFastEMA = srcFastEMA//input(close, 'Source')
lenFHMidSMA = lenMidSMA//input(50, 'MA1 FH Length')
srcFHMidSMA = srcMidSMA//input(close, 'Source')
lenFHSlowSMA = lenSlowSMA//input(200, 'MA2 FH Length')
srcFHSlowSMA = srcSlowSMA//input(close, 'Source')

lenDFastEMA = lenFastEMA//input(20, 'EMA1 D Length')
srcDFastEMA = srcFastEMA//input(close, 'Source')
lenDMidSMA = lenMidSMA//input(50, 'MA1 D Length')
srcDMidSMA = srcMidSMA//input(close, 'Source')
lenDSlowSMA = lenSlowSMA//input(200, 'MA2 D Length')
srcDSlowSMA = srcSlowSMA//input(close, 'Source')

lenWFastEMA = lenFastEMA//input(20, 'EMA1 W Length')
srcWFastEMA = srcFastEMA//input(close, 'Source')
lenWMidSMA = lenMidSMA//input(50, 'MA1 W Length')
srcWMidSMA = srcMidSMA//input(close, 'Source')
lenWSlowSMA = lenSlowSMA//input(200, 'MA2 W Length')
srcWSlowSMA = srcSlowSMA//input(close, 'Source')

lenMFastEMA = lenFastEMA//input(20, 'EMA1 M Length')
srcMFastEMA = srcFastEMA//input(close, 'Source')
lenMMidSMA = lenMidSMA//input(50, 'MA1 M Length')
srcMMidSMA = srcMidSMA//input(close, 'Source')
lenMSlowSMA = lenSlowSMA//input(200, 'MA2 M Length')
srcMSlowSMA = srcSlowSMA//input(close, 'Source')



//Inputs - Select and options for 1h Fast
showOHFastEMA = input(title='Show Fast EMA', defval=true, inline='OHFast', group='Configure 1 Hour:')
//track_OHFastEMA   = input(title='Extend to the left', defval=false, inline='OHFast', group='Configure 1 Hour:')
history_OHFastEMA = input(title='Show History', defval=false, inline='OHFast', group='Configure 1 Hour:')
//Inputs - Select and options for 1h Mid
showOHMidSMA = input(title='Show Mid SMA   ', defval=true, inline='OHMid', group='Configure 1 Hour:')
//track_OHMidSMA   = input(title='Extend to the left', defval=false, inline='OHMid', group='Configure 1 Hour:')
history_OHMidSMA = input(title='Show History', defval=false, inline='OHMid', group='Configure 1 Hour:')
//Inputs - Select and options for 1h Slow
showOHSlowSMA = input(title='Show Slow SMA', defval=true, inline='OHSlow', group='Configure 1 Hour:')
//track_OHSlowSMA   = input(title='Extend to the left', defval=false, inline='OHSlow', group='Configure 1 Hour:')
history_OHSlowSMA = input(title='Show History', defval=false, inline='OHSlow', group='Configure 1 Hour:')

//Inputs - Select and options for 4h Fast
showFHFastEMA = input(title='Show Fast EMA', defval=true, inline='FHFast', group='Configure 4 Hour:')
//track_FHFastEMA   = input(title='Extend to the left', defval=false, inline='FHFast', group='Configure 4 Hour:')
history_FHFastEMA = input(title='Show History', defval=false, inline='FHFast', group='Configure 4 Hour:')
//Inputs - Select and options for 4h Mid
showFHMidSMA = input(title='Show Mid SMA   ', defval=true, inline='FHMid', group='Configure 4 Hour:')
//track_FHMidSMA   = input(title='Extend to the left', defval=false, inline='FHMid', group='Configure 4 Hour:')
history_FHMidSMA = input(title='Show History', defval=false, inline='FHMid', group='Configure 4 Hour:')
//Inputs - Select and options for 4h Slow
showFHSlowSMA = input(title='Show Slow SMA', defval=true, inline='FHSlow', group='Configure 4 Hour:')
//track_FHSlowSMA   = input(title='Extend to the left', defval=false, inline='FHSlow', group='Configure 4 Hour:')
history_FHSlowSMA = input(title='Show History', defval=false, inline='FHSlow', group='Configure 4 Hour:')

//Inputs - Select and options for D Fast
showDFastEMA = input(title='Show Fast EMA', defval=true, inline='DFast', group='Configure Daily:')
//track_DFastEMA   = input(title='Extend to the left', defval=false, inline='DFast', group='Configure Daily:')
history_DFastEMA = input(title='Show History', defval=false, inline='DFast', group='Configure Daily:')
//Inputs - Select and options for D Mid
showDMidSMA = input(title='Show Mid SMA   ', defval=true, inline='DMid', group='Configure Daily:')
//track_DMidSMA   = input(title='Extend to the left', defval=false, inline='DMid', group='Configure Daily:')
history_DMidSMA = input(title='Show History', defval=false, inline='DMid', group='Configure Daily:')
//Inputs - Select and options for D Slow
showDSlowSMA = input(title='Show Slow SMA', defval=true, inline='DSlow', group='Configure Daily:')
//track_DSlowSMA   = input(title='Extend to the left', defval=false, inline='DSlow', group='Configure Daily:')
history_DSlowSMA = input(title='Show History', defval=false, inline='DSlow', group='Configure Daily:')

//Inputs - Select and options for W Fast
showWFastEMA = input(title='Show Fast EMA', defval=true, inline='WFast', group='Configure Weekly:')
//track_WFastEMA   = input(title='Extend to the left', defval=false, inline='WFast', group='Configure Weekly:')
history_WFastEMA = input(title='Show History', defval=false, inline='WFast', group='Configure Weekly:')
//Inputs - Select and options for W Mid
showWMidSMA = input(title='Show Mid SMA   ', defval=true, inline='WMid', group='Configure Weekly:')
//track_WMidSMA   = input(title='Extend to the left', defval=false, inline='WMid', group='Configure Weekly:')
history_WMidSMA = input(title='Show History', defval=false, inline='WMid', group='Configure Weekly:')
//Inputs - Select and options for W Slow
showWSlowSMA = input(title='Show Slow SMA', defval=true, inline='WSlow', group='Configure Weekly:')
//track_WSlowSMA   = input(title='Extend to the left', defval=false, inline='WSlow', group='Configure Weekly:')
history_WSlowSMA = input(title='Show History', defval=false, inline='WSlow', group='Configure Weekly:')

//Inputs - Select and options for M Fast
showMFastEMA = input(title='Show Fast EMA', defval=true, inline='MFast', group='Configure Monthly:')
//track_MFastEMA   = input(title='Extend to the left', defval=false, inline='MFast', group='Configure Monthly:')
history_MFastEMA = input(title='Show History', defval=false, inline='MFast', group='Configure Monthly:')
//Inputs - Select and options for M Mid
showMMidSMA = input(title='Show Mid SMA   ', defval=true, inline='MMid', group='Configure Monthly:')
//track_MMidSMA   = input(title='Extend to the left', defval=false, inline='MMid', group='Configure Monthly:')
history_MMidSMA = input(title='Show History', defval=false, inline='MMid', group='Configure Monthly:')
//Inputs - Select and options for M Slow
showMSlowSMA = input(title='Show Slow SMA', defval=true, inline='MSlow', group='Configure Monthly:')
//track_MSlowSMA   = input(title='Extend to the left', defval=false, inline='MSlow', group='Configure Monthly:')
history_MSlowSMA = input(title='Show History', defval=false, inline='MSlow', group='Configure Monthly:')



//Inputs - Labels configuration

showlabels   = input(title='Show Labels', defval=true, group='Labels Configuration:')
ext_labels   = input(title='Use Extended Text Labels', defval=false, group='Labels Configuration:')
popup_label  = input(title='Show current MA price Pop Up when mouse is over label', defval=true, group='Labels Configuration:')
offset_label = input(title='Label Offset', defval=15, group='Labels Configuration:')
offset_line  = input(title='Dashed Line before label Offset', defval=1, group='Labels Configuration:')


//Create the input with pull-down menu
styleOption = input.string(title="Labels Style",
     options=["Triangle up (▲)", "Triangle down (▼)",
         "Arrow up (↑)", "Arrow down (↓)", "Label Center", "Label up (⬆)",
         "Label down (⬇)", "Label left (⬇)", "Label right (⬇)",
         "Label upper left (⬇)", "Label upper right (⬇)",
         "Label lower left (⬇)", "Label lower right (⬇)", "Plus (+)",
         "Cross (⨯)", "Circle (●)", "Diamond (◆)", "Flag (⚑)",
         "Square (■)", "None"],
     defval="Label left (⬇)", group='Labels Configuration:')

sizeOption = input.string(title="Labels Size",
     options=["Auto", "Tiny", "Small", "Normal", "Large",
     "Huge"], defval="Normal", group='Labels Configuration:')



//Turn the input into a proper label style value
labelStyle = (styleOption == "Triangle up (▲)") ? label.style_triangleup :
     (styleOption == "Triangle down (▼)") ? label.style_triangledown :
     (styleOption == "Arrow up (↑)") ? label.style_arrowup :
     (styleOption == "Arrow down (↓)") ? label.style_arrowdown :
     (styleOption == "Label Center") ? label.style_label_center :
     (styleOption == "Label up (⬆)") ? label.style_label_up :
     (styleOption == "Label down (⬇)") ? label.style_label_down :
     (styleOption == "Label left (⬇)") ? label.style_label_left :
     (styleOption == "Label right (⬇)") ? label.style_label_right :
     (styleOption == "Label upper left (⬇)") ? label.style_label_upper_left :
     (styleOption == "Label upper right (⬇)") ? label.style_label_upper_right :
     (styleOption == "Label lower left (⬇)") ? label.style_label_lower_left :
     (styleOption == "Label lower right (⬇)") ? label.style_label_lower_right :
     (styleOption == "Plus (+)") ? label.style_cross:
     (styleOption == "Cross (⨯)") ? label.style_xcross :
     (styleOption == "Circle (●)") ? label.style_circle :
     (styleOption == "Diamond (◆)") ? label.style_diamond :
     (styleOption == "Flag (⚑)") ? label.style_flag :
     (styleOption == "Square (■)") ? label.style_square :
     (styleOption == "None") ? label.style_none :
          label.style_none

   
//Turn the input into a proper label style value
labelSize = (sizeOption == "Auto") ? size.auto :
     (sizeOption == "Tiny") ? size.tiny :
     (sizeOption == "Small") ? size.small :
     (sizeOption == "Normal") ? size.normal :
     (sizeOption == "Large") ? size.large :
     (sizeOption == "Huge") ? size.huge :
         size.normal



////////////////////////////////////////////////// VWAP

//Calculate if it is the current day. We´ll use this var later as a condition for plotting the VWAP and pdVWAP
//Taken from "Chart Champions - Part 1 - nPOC - Levels - VWAPs" script writen by user Frien_dd-DisDev
is_today = year == year(timenow) and month == month(timenow) and dayofmonth == dayofmonth(timenow)

///VWAP rounding issue resolution
//Taken from "Chart Champions - Part 1 - nPOC - Levels - VWAPs" script writen by user Frien_dd-DisDev
f_round_up_to_tick(x, mintick) =>
    mult = 1 / mintick
    value = math.ceil(x * mult) / mult
    value

f_round_down_to_tick(x, mintick) =>
    mult = 1 / mintick
    value = math.floor(x * mult) / mult
    value


//Daily VWAP
//Taken from "Chart Champions - Part 1 - nPOC - Levels - VWAPs" script writen by user Frien_dd-DisDev
roundedVWAP = f_round_up_to_tick(ta.vwap, syminfo.mintick)
VWAP = roundedVWAP

//Previous Day's Closing Vwap
//Taken from "Chart Champions - Part 1 - nPOC - Levels - VWAPs" script writen by user Frien_dd-DisDev
newday(res) =>
    t = time(res)
    ta.change(t) != 0 ? 1 : 0
new_day = newday('D')
pdVWAP = request.security(syminfo.tickerid, '1', ta.valuewhen(new_day, VWAP[1], 0))



////////////////////////////////////////////////// Moving Averages

//Calculate Moving Averages for each timeframe

//Current Time
CTFastEMA = ta.ema(srcFastEMA, lenFastEMA)
CTMidSMA  = ta.sma(srcMidSMA, lenMidSMA)
CTSlowSMA = ta.sma(srcSlowSMA, lenSlowSMA)
//1 Hour
OHFastEMA = request.security(syminfo.tickerid, '60', ta.ema(srcOHFastEMA, lenOHFastEMA))
OHMidSMA  = request.security(syminfo.tickerid, '60', ta.sma(srcOHMidSMA, lenOHMidSMA))
OHSlowSMA = request.security(syminfo.tickerid, '60', ta.sma(srcOHSlowSMA, lenOHSlowSMA))
//4 Hours
FHFastEMA = request.security(syminfo.tickerid, '240', ta.ema(srcFHFastEMA, lenFHFastEMA))
FHMidSMA  = request.security(syminfo.tickerid, '240', ta.sma(srcFHMidSMA, lenFHMidSMA))
FHSlowSMA = request.security(syminfo.tickerid, '240', ta.sma(srcFHSlowSMA, lenFHSlowSMA))
//Daily
DFastEMA  = request.security(syminfo.tickerid, 'D', ta.ema(srcDFastEMA, lenDFastEMA))
DMidSMA   = request.security(syminfo.tickerid, 'D', ta.sma(srcDMidSMA, lenDMidSMA))
DSlowSMA  = request.security(syminfo.tickerid, 'D', ta.sma(srcDSlowSMA, lenDSlowSMA))
//Weekly
WFastEMA  = request.security(syminfo.tickerid, 'W', ta.ema(srcWFastEMA, lenWFastEMA))
WMidSMA   = request.security(syminfo.tickerid, 'W', ta.sma(srcWMidSMA, lenWMidSMA))
WSlowSMA  = request.security(syminfo.tickerid, 'W', ta.sma(srcWSlowSMA, lenWSlowSMA))
//Monthly
MFastEMA  = request.security(syminfo.tickerid, 'M', ta.ema(srcMFastEMA, lenMFastEMA))
MMidSMA   = request.security(syminfo.tickerid, 'M', ta.sma(srcMMidSMA, lenMMidSMA))
MSlowSMA  = request.security(syminfo.tickerid, 'M', ta.sma(srcMSlowSMA, lenMSlowSMA))



////////////////////////////////////////////////// Plot labels


//Create Current Timeframe-based Moving Averages labels (uncomment if you want to see labels on current timeframe)
//CTFastEMA_label = showlabels and showCTFastEMA ? label.new(x=bar_index+offset_label, y=CTFastEMA, text = ext_labels ? "Fast EMA " + str.tostring(lenFastEMA) + " (1 Hour)" : "1h EMA " + str.tostring(lenFastEMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 50), style=labelStyle, textcolor=color.new(color.white, 0), size=labelSize, tooltip = popup_label ? str.tostring(CTFastEMA, "#.##") : "") :na
//label.delete(CTFastEMA_label[1])
//CTMidSMA_label = showlabels and showCTMidSMA ? label.new(x=bar_index+offset_label, y=CTMidSMA, text = ext_labels ? "Mid SMA " + str.tostring(lenMidSMA) + " (1 Hour)" : "1h SMA " + str.tostring(lenMidSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 50), style=labelStyle, textcolor=color.new(color.white, 0), size=labelSize, tooltip = popup_label ? str.tostring(CTMidSMA, "#.##") : "") :na
//label.delete(CTMidSMA_label[1])
//CTSlowSMA_label = showlabels and showCTSlowSMA ? label.new(x=bar_index+offset_label, y=CTSlowSMA, text = ext_labels ? "Slow SMA " + str.tostring(lenSlowSMA) + " (1 Hour)" : "1h SMA " + str.tostring(lenSlowSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 50), style=labelStyle, textcolor=color.new(color.white, 0), size=labelSize, tooltip = popup_label ? str.tostring(CTSlowSMA, "#.##") : "") :na
//label.delete(CTSlowSMA_label[1])
//Create 1 Hour labels
OHFastEMA_label = showlabels and showOHFastEMA and (timeframe.period != '60') ? label.new(x=bar_index+offset_label, y=OHFastEMA, text = ext_labels ? "Fast EMA " + str.tostring(lenOHFastEMA) + " (1 Hour)" : "1h EMA " + str.tostring(lenOHFastEMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorFastEMA, size=labelSize, tooltip = popup_label ? str.tostring(OHFastEMA, "#.##") : "") :na
label.delete(OHFastEMA_label[1])
OHMidSMA_label = showlabels and showOHMidSMA and (timeframe.period != '60') ? label.new(x=bar_index+offset_label, y=OHMidSMA, text = ext_labels ? "Mid SMA " + str.tostring(lenOHMidSMA) + " (1 Hour)" : "1h SMA " + str.tostring(lenOHMidSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorMidSMA, size=labelSize, tooltip = popup_label ? str.tostring(OHMidSMA, "#.##") : "") :na
label.delete(OHMidSMA_label[1])
OHSlowSMA_label = showlabels and showOHSlowSMA and (timeframe.period != '60') ? label.new(x=bar_index+offset_label, y=OHSlowSMA, text = ext_labels ? "Slow SMA " + str.tostring(lenOHSlowSMA) + " (1 Hour)" : "1h SMA " + str.tostring(lenOHSlowSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorSlowSMA, size=labelSize, tooltip = popup_label ? str.tostring(OHSlowSMA, "#.##") : "") :na
label.delete(OHSlowSMA_label[1])
//Create 4 Hours labels
FHFastEMA_label = showlabels and showFHFastEMA and (timeframe.period != '240') ? label.new(x=bar_index+offset_label, y=FHFastEMA, text = ext_labels ? "Fast EMA " + str.tostring(lenFHFastEMA) + " (4 Hours)" : "4h EMA " + str.tostring(lenFHFastEMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorFastEMA, size=labelSize, tooltip = popup_label ? str.tostring(FHFastEMA, "#.##") : "") :na
label.delete(FHFastEMA_label[1])
FHMidSMA_label = showlabels and showFHMidSMA and (timeframe.period != '240') ? label.new(x=bar_index+offset_label, y=FHMidSMA, text = ext_labels ? "Mid SMA " + str.tostring(lenFHMidSMA) + " (4 Hours)" : "4h SMA " + str.tostring(lenFHMidSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorMidSMA, size=labelSize, tooltip = popup_label ? str.tostring(FHMidSMA, "#.##") : "") :na
label.delete(FHMidSMA_label[1])
FHSlowSMA_label = showlabels and showFHSlowSMA and (timeframe.period != '240') ? label.new(x=bar_index+offset_label, y=FHSlowSMA, text = ext_labels ? "Slow SMA " + str.tostring(lenFHSlowSMA) + " (4 Hours)" : "4h SMA " + str.tostring(lenFHSlowSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorSlowSMA, size=labelSize, tooltip = popup_label ? str.tostring(FHSlowSMA, "#.##") : "") :na
label.delete(FHSlowSMA_label[1])
//Create Daily labels
DFastEMA_label = showlabels and showDFastEMA and (timeframe.period != 'D') ? label.new(x=bar_index+offset_label, y=DFastEMA, text = ext_labels ? "Fast EMA " + str.tostring(lenDFastEMA) + " (Daily)" : "D EMA " + str.tostring(lenDFastEMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorFastEMA, size=labelSize, tooltip = popup_label ? str.tostring(DFastEMA, "#.##") : "") :na
label.delete(DFastEMA_label[1])
DMidSMA_label = showlabels and showDMidSMA and (timeframe.period != 'D') ? label.new(x=bar_index+offset_label, y=DMidSMA, text = ext_labels ? "Mid SMA " + str.tostring(lenDMidSMA) + " (Daily)" : "D SMA " + str.tostring(lenDMidSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorMidSMA, size=labelSize, tooltip = popup_label ? str.tostring(DMidSMA, "#.##") : "") :na
label.delete(DMidSMA_label[1])
DSlowSMA_label = showlabels and showDSlowSMA and (timeframe.period != 'D') ? label.new(x=bar_index+offset_label, y=DSlowSMA, text = ext_labels ? "Slow SMA " + str.tostring(lenDSlowSMA) + " (Daily)" : "D SMA " + str.tostring(lenDSlowSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorSlowSMA, size=labelSize, tooltip = popup_label ? str.tostring(DSlowSMA, "#.##") : "") :na
label.delete(DSlowSMA_label[1])
//Create Weekly labels
WFastEMA_label = showlabels and showWFastEMA and (timeframe.period != 'W') ? label.new(x=bar_index+offset_label, y=WFastEMA, text = ext_labels ? "Fast EMA " + str.tostring(lenWFastEMA) + " (Weekly)" : "W EMA " + str.tostring(lenWFastEMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorFastEMA, size=labelSize, tooltip = popup_label ? str.tostring(WFastEMA, "#.##") : "") :na
label.delete(WFastEMA_label[1])
WMidSMA_label = showlabels and showWMidSMA and (timeframe.period != 'W') ? label.new(x=bar_index+offset_label, y=WMidSMA, text = ext_labels ? "Mid SMA " + str.tostring(lenWMidSMA) + " (Weekly)" : "W SMA " + str.tostring(lenWMidSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorMidSMA, size=labelSize, tooltip = popup_label ? str.tostring(WMidSMA, "#.##") : "") :na
label.delete(WMidSMA_label[1])
WSlowSMA_label = showlabels and showWSlowSMA and (timeframe.period != 'W') ? label.new(x=bar_index+offset_label, y=WSlowSMA, text = ext_labels ? "Slow SMA " + str.tostring(lenWSlowSMA) + " (Weekly)" : "W SMA " + str.tostring(lenWSlowSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorSlowSMA, size=labelSize, tooltip = popup_label ? str.tostring(WSlowSMA, "#.##") : "") :na
label.delete(WSlowSMA_label[1])
//Create Monthly labels
MFastEMA_label = showlabels and showMFastEMA and (timeframe.period != 'M') ? label.new(x=bar_index+offset_label, y=MFastEMA, text = ext_labels ? "Fast EMA " + str.tostring(lenMFastEMA) + " (Monthly)" : "M EMA " + str.tostring(lenMFastEMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorFastEMA, size=labelSize, tooltip = popup_label ? str.tostring(MFastEMA, "#.##") : "") :na
label.delete(MFastEMA_label[1])
MMidSMA_label = showlabels and showMMidSMA and (timeframe.period != 'M') ? label.new(x=bar_index+offset_label, y=MMidSMA, text = ext_labels ? "Mid SMA " + str.tostring(lenMMidSMA) + " (Monthly)" : "M SMA " + str.tostring(lenMMidSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorMidSMA, size=labelSize, tooltip = popup_label ? str.tostring(MMidSMA, "#.##") : "") :na
label.delete(MMidSMA_label[1])
MSlowSMA_label = showlabels and showMSlowSMA and (timeframe.period != 'M') ? label.new(x=bar_index+offset_label, y=MSlowSMA, text = ext_labels ? "Slow SMA " + str.tostring(lenMSlowSMA) + " (Monthly)" : "M SMA " + str.tostring(lenMSlowSMA), xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorSlowSMA, size=labelSize, tooltip = popup_label ? str.tostring(MSlowSMA, "#.##") : "") :na
label.delete(MSlowSMA_label[1])
//Create VWAP labels
VWAP_label = showlabels and showVWAP ? label.new(x=bar_index+offset_label, y=VWAP, text = ext_labels ? "VWAP (Daily)" : "D VWAP", xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorVWAP, size=labelSize, tooltip = popup_label ? str.tostring(VWAP, "#.##") : "") :na
label.delete(VWAP_label[1])
pdVWAP_label = showlabels and showpdVWAP ? label.new(x=bar_index+offset_label, y=pdVWAP, text = ext_labels ? "Previous Day Close VWAP (Daily)" : "PDC VWAP", xloc=xloc.bar_index, yloc=yloc.price, color=color.new(#2066D5, 100), style=labelStyle, textcolor=colorpdVWAP, size=labelSize, tooltip = popup_label ? str.tostring(pdVWAP, "#.##") : "") :na
label.delete(pdVWAP_label[1])


//Create Current Timeframe-based Moving Averages lines (uncomment if you want to see lines on current timeframe)
//CTFastEMA_line = showCTFastEMA ? line.new(x1=bar_index+offset_line, y1=CTFastEMA, x2=bar_index+offset_label, y2=CTFastEMA, xloc=xloc.bar_index, style=line.style_dashed, extend=track_CTFastEMA ? extend.left : extend.none, color=color.white) :na
//line.delete(CTFastEMA_line[1])  // remove the previous line when new bar appears
//CTMidSMA_line = showCTMidSMA ?line.new(x1=bar_index+offset_line, y1=CTMidSMA, x2=bar_index+offset_label, y2=CTMidSMA, xloc=xloc.bar_index, style=line.style_dashed, extend=track_CTMidSMA ? extend.left : extend.none, color=color.yellow) :na
//line.delete(CTMidSMA_line[1])  // remove the previous line when new bar appears
//CTSlowSMA_line = showCTSlowSMA ?line.new(x1=bar_index+offset_line, y1=CTSlowSMA, x2=bar_index+offset_label, y2=CTSlowSMA, xloc=xloc.bar_index, style=line.style_dashed, extend=track_CTSlowSMA ? extend.left : extend.none, color=color.red) :na
//line.delete(CTSlowSMA_line[1])  // remove the previous line when new bar appears
//Create 1 Hour lines  
OHFastEMA_line = showOHFastEMA and (timeframe.period != '60') ? line.new(x1=bar_index+offset_line, y1=OHFastEMA, x2=bar_index+offset_label, y2=OHFastEMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorFastEMA) :na
line.delete(OHFastEMA_line[1])  // remove the previous line when new bar appears
OHMidSMA_line = showOHMidSMA and (timeframe.period != '60') ? line.new(x1=bar_index+offset_line, y1=OHMidSMA, x2=bar_index+offset_label, y2=OHMidSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorMidSMA) :na
line.delete(OHMidSMA_line[1])  // remove the previous line when new bar appears
OHSlowSMA_line = showOHSlowSMA and (timeframe.period != '60') ? line.new(x1=bar_index+offset_line, y1=OHSlowSMA, x2=bar_index+offset_label, y2=OHSlowSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorSlowSMA) :na
line.delete(OHSlowSMA_line[1])  // remove the previous line when new bar appears
//Create 4 Hours lines  
FHFastEMA_line = showFHFastEMA and (timeframe.period != '240') ? line.new(x1=bar_index+offset_line, y1=FHFastEMA, x2=bar_index+offset_label, y2=FHFastEMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorFastEMA) :na
line.delete(FHFastEMA_line[1])  // remove the previous line when new bar appears
FHMidSMA_line = showFHMidSMA and (timeframe.period != '240') ?line.new(x1=bar_index+offset_line, y1=FHMidSMA, x2=bar_index+offset_label, y2=FHMidSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorMidSMA) :na
line.delete(FHMidSMA_line[1])  // remove the previous line when new bar appears
FHSlowSMA_line = showFHSlowSMA and (timeframe.period != '240') ?line.new(x1=bar_index+offset_line, y1=FHSlowSMA, x2=bar_index+offset_label, y2=FHSlowSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorSlowSMA) :na
line.delete(FHSlowSMA_line[1])  // remove the previous line when new bar appears
//Create Daily lines 
DFastEMA_line = showDFastEMA and (timeframe.period != 'D') ? line.new(x1=bar_index+offset_line, y1=DFastEMA, x2=bar_index+offset_label, y2=DFastEMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorFastEMA) :na
line.delete(DFastEMA_line[1])  // remove the previous line when new bar appears
DMidSMA_line = showDMidSMA and (timeframe.period != 'D') ? line.new(x1=bar_index+offset_line, y1=DMidSMA, x2=bar_index+offset_label, y2=DMidSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorMidSMA) :na
line.delete(DMidSMA_line[1])  // remove the previous line when new bar appears
DSlowSMA_line = showDSlowSMA and (timeframe.period != 'D') ? line.new(x1=bar_index+offset_line, y1=DSlowSMA, x2=bar_index+offset_label, y2=DSlowSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorSlowSMA) :na
line.delete(DSlowSMA_line[1])  // remove the previous line when new bar appears
//Create Weekly lines 
WFastEMA_line = showWFastEMA and (timeframe.period != 'W') ? line.new(x1=bar_index+offset_line, y1=WFastEMA, x2=bar_index+offset_label, y2=WFastEMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorFastEMA) :na
line.delete(WFastEMA_line[1])  // remove the previous line when new bar appears
WMidSMA_line = showWMidSMA  and (timeframe.period != 'W')? line.new(x1=bar_index+offset_line, y1=WMidSMA, x2=bar_index+offset_label, y2=WMidSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorMidSMA) :na
line.delete(WMidSMA_line[1])  // remove the previous line when new bar appears
WSlowSMA_line = showWSlowSMA and (timeframe.period != 'W') ? line.new(x1=bar_index+offset_line, y1=WSlowSMA, x2=bar_index+offset_label, y2=WSlowSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorSlowSMA) :na
line.delete(WSlowSMA_line[1])  // remove the previous line when new bar appears
//Create Monthly lines 
MFastEMA_line = showMFastEMA and (timeframe.period != 'M') ? line.new(x1=bar_index+offset_line, y1=MFastEMA, x2=bar_index+offset_label, y2=MFastEMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorFastEMA) :na
line.delete(MFastEMA_line[1])  // remove the previous line when new bar appears
MMidSMA_line = showMMidSMA and (timeframe.period != 'M') ? line.new(x1=bar_index+offset_line, y1=MMidSMA, x2=bar_index+offset_label, y2=MMidSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorMidSMA) :na
line.delete(MMidSMA_line[1])  // remove the previous line when new bar appears
MSlowSMA_line = showMSlowSMA and (timeframe.period != 'M') ? line.new(x1=bar_index+offset_line, y1=MSlowSMA, x2=bar_index+offset_label, y2=MSlowSMA, xloc=xloc.bar_index, style=line.style_dashed, color=colorSlowSMA) :na
line.delete(MSlowSMA_line[1])  // remove the previous line when new bar appeana
//Create VWAP lines
VWAP_line = showVWAP ? line.new(x1=bar_index+offset_line, y1=VWAP, x2=bar_index+offset_label, y2=VWAP, xloc=xloc.bar_index, style=line.style_dashed, color=colorVWAP) :na
line.delete(VWAP_line[1])  // remove the previous line when new bar appeana
pdVWAP_line = showpdVWAP ? line.new(x1=bar_index+offset_line, y1=pdVWAP, x2=bar_index+offset_label, y2=pdVWAP, xloc=xloc.bar_index, style=line.style_dashed, color=colorpdVWAP) :na
line.delete(pdVWAP_line[1])  // remove the previous line when new bar appeana



////////////////////////////////////////////////// Plot Moving Averages


//Plot current timeframe-based Moving Averages
plot(showCTFastEMA ? CTFastEMA : na, title='Current timeframe Fast EMA', style=plot.style_line, color=colorFastEMA, linewidth=2)
plot(showCTMidSMA ? CTMidSMA : na, title='Current timeframe Mid SMA', style=plot.style_line, color=color.new(color.yellow, 0), linewidth=2)
plot(showCTSlowSMA ? CTSlowSMA : na, title='Current timeframe Slow SMA', style=plot.style_line, color=colorSlowSMA, linewidth=2)
//Plot 1 Hour Moving Averages
plot(showOHFastEMA and history_OHFastEMA and (timeframe.period != '60') ? OHFastEMA : na, title='1 Hour Fast EMA', style=plot.style_line, color=colorFastEMA, show_last=history_OHFastEMA ? na : 1, offset=history_OHFastEMA ? 0 : -99999)
plot(showOHMidSMA and history_OHMidSMA and (timeframe.period != '60') ? OHMidSMA : na, title='1 Hour Mid SMA', style=plot.style_line, color=colorMidSMA, show_last=history_OHMidSMA ? na : 1, offset=history_OHMidSMA ? 0 : -99999)
plot(showOHSlowSMA and history_OHSlowSMA and (timeframe.period != '60') ? OHSlowSMA : na, title='1 Hour Slow SMA', style=plot.style_line, color=colorSlowSMA, show_last=history_OHSlowSMA ? na : 1, offset=history_OHSlowSMA ? 0 : -99999)
//Plot 4 Hours Moving Averages
plot(showFHFastEMA and history_FHFastEMA and (timeframe.period != '240') ? FHFastEMA : na, title='4 Hours Fast EMA', style=plot.style_line, color=colorFastEMA, show_last=history_FHFastEMA ? na : 1, offset=history_FHFastEMA ? 0 : -99999)
plot(showFHMidSMA and history_FHFastEMA and (timeframe.period != '240') ? FHMidSMA : na, title='4 Hours Mid SMA', style=plot.style_line, color=colorMidSMA, show_last=history_FHMidSMA ? na : 1, offset=history_FHMidSMA ? 0 : -99999)
plot(showFHSlowSMA and history_FHFastEMA and (timeframe.period != '240') ? FHSlowSMA : na, title='4 Hours Slow SMA', style=plot.style_line, color=colorSlowSMA, show_last=history_FHSlowSMA ? na : 1, offset=history_FHSlowSMA ? 0 : -99999)
//Plot Daily Moving Averages
plot(showDFastEMA and history_DFastEMA and (timeframe.period != 'D') ? DFastEMA : na, title='Daily Fast EMA', style=plot.style_line, color=colorFastEMA, show_last=history_DFastEMA ? na : 1, offset=history_DFastEMA ? 0 : -99999)
plot(showDMidSMA and history_DMidSMA and (timeframe.period != 'D') ? DMidSMA : na, title='Daily Mid SMA', style=plot.style_line, color=colorMidSMA, show_last=history_DMidSMA ? na : 1, offset=history_DMidSMA ? 0 : -99999)
plot(showDSlowSMA and history_DSlowSMA and (timeframe.period != 'D') ? DSlowSMA : na, title='Daily Slow SMA', style=plot.style_line, color=colorSlowSMA, show_last=history_DSlowSMA ? na : 1, offset=history_DSlowSMA ? 0 : -99999)
//Plot Weekly Moving Averages
plot(showWFastEMA and history_WFastEMA and (timeframe.period != 'W') ? WFastEMA : na, title='Weekly Fast EMA', style=plot.style_line, color=colorFastEMA, show_last=history_WFastEMA ? na : 1, offset=history_WFastEMA ? 0 : -99999)
plot(showWMidSMA and history_WMidSMA and (timeframe.period != 'W') ? WMidSMA : na, title='Weekly Mid SMA', style=plot.style_line, color=colorMidSMA, show_last=history_WMidSMA ? na : 1, offset=history_WMidSMA ? 0 : -99999)
plot(showWSlowSMA and history_WSlowSMA and (timeframe.period != 'W') ? WSlowSMA : na, title='Weekly Slow SMA', style=plot.style_line, color=colorSlowSMA, show_last=history_WSlowSMA ? na : 1, offset=history_WSlowSMA ? 0 : -99999)
//Plot Monthly Moving Averages
plot(showMFastEMA and history_MFastEMA and (timeframe.period != 'M') ? MFastEMA : na, title='Monthly Fast EMA', style=plot.style_line, color=colorFastEMA, show_last=history_MFastEMA ? na : 1, offset=history_MFastEMA ? 0 : -99999)
plot(showMMidSMA and history_MMidSMA and (timeframe.period != 'M') ? MMidSMA : na, title='Monthly Mid SMA', style=plot.style_line, color=colorMidSMA, show_last=history_MMidSMA ? na : 1, offset=history_MMidSMA ? 0 : -99999)
plot(showMSlowSMA and history_MSlowSMA and (timeframe.period != 'M') ? MSlowSMA : na, title='Monthly Slow SMA', style=plot.style_line, color=colorSlowSMA, show_last=history_MSlowSMA ? na : 1, offset=history_MSlowSMA ? 0 : -99999)
//Plot VWAP Moving Averages
plot(showVWAP and history_VWAP and is_today ? VWAP : na, title='VWAP', style=plot.style_line, color=colorVWAP)
plot(showpdVWAP and history_pdVWAP and is_today ? pdVWAP : na, title='pdVWAP', style=plot.style_line, color=colorpdVWAP)
