Key Levels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MensaTrader

//@version=5
indicator('Key Levels', overlay=true)

//Titles
plot            = "=== Plots ==="

//Inputs

plotDaily       = input.bool(true, title=' Daily Levels........', group=plot, inline="1")
plotWeekly      = input.bool(true, title=' Weekly Levels....  ', group=plot, inline="2")
plotMonthly     = input.bool(true, title=' Monthly Levels... ', group=plot, inline="3")

showDVwap       = input.bool(true, title='  Vwaps', group=plot, inline="1")
showWVwap       = input.bool(true, title='  Vwaps', group=plot, inline="2")
showMVwap       = input.bool(true, title='  Vwaps', group=plot, inline="3")

plotBack        = input.int(15, title='Plot bars back', group=plot)


//Vwap
v               = ta.vwap(close)
av              = ta.vwap(hlc3)

//Monthly Vwap
mv              = request.security(syminfo.tickerid, 'M', v[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
mav             = request.security(syminfo.tickerid, 'M', av[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

//Weekly Vwap
wv              = request.security(syminfo.tickerid, 'W', v[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
wav             = request.security(syminfo.tickerid, 'W', av[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

//Daily Vwap
dv              = request.security(syminfo.tickerid, 'D', v[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
dav             = request.security(syminfo.tickerid, 'D', av[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)



//Monthly Levels
monthly         = request.security(syminfo.tickerid, 'M', close[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
monthlyHigh     = request.security(syminfo.tickerid, 'M', high[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
monthlyLow      = request.security(syminfo.tickerid, 'M', low[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
monthlyOpen     = request.security(syminfo.tickerid, 'M', open[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

//Weekly Levels
weekly          = request.security(syminfo.tickerid, 'W', close[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
weeklyHigh      = request.security(syminfo.tickerid, 'W', high[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
weeklyLow       = request.security(syminfo.tickerid, 'W', low[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
weeklyOpen      = request.security(syminfo.tickerid, 'W', open[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

//Daily Levels
daily           = request.security(syminfo.tickerid, 'D', close[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
dailyHigh       = request.security(syminfo.tickerid, 'D', high[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
dailyLow        = request.security(syminfo.tickerid, 'D', low[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
dailyOpen       = request.security(syminfo.tickerid, 'D', open[1], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)



//Colours
dailyC          = input.color(color.new(color.teal, 0), title='Daily Levels', group='=== Colors ===')
weeklyC         = input.color(color.new(color.yellow, 0), title='Weekly Levels', group='=== Colors ===')
monthlyC        = input.color(color.new(color.purple, 0), title='Monthly Levels', group='=== Colors ===')


//Key levels
m               = monthly
mH              = monthlyHigh
mL              = monthlyLow
mO              = monthlyOpen

var mPlot       = m
var mHPlot      = m
var mLPlot      = m
var mOPlot      = m


newMonth        = ta.change(m)

if newMonth
    mPlot       := m
    mHPlot      := mH
    mLPlot      := mL
    mOPlot      := mO
    mOPlot

//Plot 
plot(plotMonthly ? monthly : na, title='Monthly Close', color=monthlyC, show_last=plotBack)
plot(plotMonthly ? monthlyHigh : na, title='Monthly High', color=monthlyC, show_last=plotBack)
plot(plotMonthly ? monthlyLow : na, title='Monthly Low', color=monthlyC, show_last=plotBack)
plot(plotMonthly ? monthlyOpen : na, title='Monthly Open', color=monthlyC, show_last=plotBack)

plot(plotWeekly ? weekly : na, title='Weekly Close', color=weeklyC, show_last=plotBack)
plot(plotWeekly ? weeklyHigh : na, title='Weekly High', color=weeklyC, show_last=plotBack)
plot(plotWeekly ? weeklyLow : na, title='Weekly Low', color=weeklyC, show_last=plotBack)
plot(plotWeekly ? weeklyOpen : na, title='Weekly Open', color=weeklyC, show_last=plotBack)

plot(plotDaily ? daily : na, title='Daily Close', color=dailyC, show_last=plotBack)
plot(plotDaily ? dailyHigh : na, title='Daily High', color=dailyC, show_last=plotBack)
plot(plotDaily ? dailyLow : na, title='Daily Low', color=dailyC, show_last=plotBack)
plot(plotDaily ? dailyOpen : na, title='Daily Open', color=dailyC, show_last=plotBack)


//Vwap Plots
plot(showMVwap ? mv : na, title='Monthly Vwap', color=monthlyC, show_last=plotBack)
plot(showMVwap ? mav : na, title='Monthly Anchored Vwap', color=monthlyC, show_last=plotBack)

plot(showWVwap ? wv : na, title='Weekly Vwap', color=weeklyC, show_last=plotBack)
plot(showWVwap ? wav : na, title='Weekly Anchored Vwap', color=weeklyC, show_last=plotBack)

plot(showDVwap ? dv : na, title='Daily Vwap', color=dailyC, show_last=plotBack)
plot(showDVwap ? dav : na, title='Daily Anchored Vwap', color=dailyC, show_last=plotBack)


//Labels
textStyle       = label.style_none

m1 = label.new(plotMonthly ? bar_index : na, monthly, text='Monthly', style=textStyle)
m2 = label.new(plotMonthly ? bar_index : na, monthlyHigh, text='Monthly High', style=textStyle)
m3 = label.new(plotMonthly ? bar_index : na, monthlyLow, text='Monthly Low', style=textStyle)
m4 = label.new(plotMonthly ? bar_index : na, monthlyOpen, text='Monthly Open', style=textStyle)

//Weekly Labels
w1 = label.new(plotWeekly ? bar_index : na, weekly, text='Weekly', style=textStyle)
w2 = label.new(plotWeekly ? bar_index : na, weeklyHigh, text='Weekly High', style=textStyle)
w3 = label.new(plotWeekly ? bar_index : na, weeklyLow, text='Weekly Low', style=textStyle)
w4 = label.new(plotWeekly ? bar_index : na, weeklyOpen, text='Weekly Open', style=textStyle)

//Daily Labels
d1 = plotDaily ? label.new(plotDaily ? bar_index : na, daily, text='Daily', style=textStyle) : na
d2 = plotDaily ? label.new(plotDaily ? bar_index : na, dailyHigh, text='Daily High', style=textStyle) : na
d3 = plotDaily ? label.new(plotDaily ? bar_index : na, dailyLow, text='Daily Low', style=textStyle) : na
d4 = plotDaily ? label.new(plotDaily ? bar_index : na, dailyOpen, text='Daily Open', style=textStyle) : na

//Vwap Labels
m11 = label.new(showMVwap ? bar_index : na, mv, text='Monthly Vwap', style=textStyle)
m22 = label.new(showMVwap ? bar_index : na, mav, text='Monthly Awap', style=textStyle)
w11 = label.new(showWVwap ? bar_index : na, wv, text='Weekly Vwap', style=textStyle)
w22 = label.new(showWVwap ? bar_index : na, wav, text='Weekly Awap', style=textStyle)
d11 = label.new(showDVwap ? bar_index : na, dv, text='Daily Vwap', style=textStyle)
d22 = label.new(showDVwap ? bar_index : na, dav, text='Daily Awap', style=textStyle)



//Delete Labels
label.delete(m1[1])
label.delete(m2[1])
label.delete(m3[1])
label.delete(m4[1])

label.delete(w1[1])
label.delete(w2[1])
label.delete(w3[1])
label.delete(w4[1])

label.delete(d1[1])
label.delete(d2[1])
label.delete(d3[1])
label.delete(d4[1])

//Delete Vwap Labels
label.delete(m11[1])
label.delete(m22[1])

label.delete(w11[1])
label.delete(w22[1])

label.delete(d11[1])
label.delete(d22[1])

