Yearly Monthly Vertical Lines [MsF]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Trader_Morry

//@version=5

////////
// Title 
indicator(title='Vertical & Open Lines - Yearly Monthly Weekly Daily [MsF]', shorttitle='YMWD V&O Lines', overlay=true, max_lines_count=500, max_labels_count=500)

////////
// Input values 
// [
iBaseTime = input.time(defval=timestamp('01 Jan 2018 0:00 +0000'), title='Base Time')

yearlyColor = input.color(defval=color.red, title='', inline='year')
iYearly = input.bool(true, title='Show Yearly', inline='year')
hyearlyColor = input.color(defval=color.red, title='', inline='year')
ihYearly = input.bool(false, title='Open Line', inline='year')

monthlyColor = input.color(defval=color.orange, title='', inline='month')
iMonthly = input.bool(true, title='Show Monthly', inline='month')
hmonthlyColor = input.color(defval=color.orange, title='', inline='month')
ihMonthly = input.bool(false, title='Open Line', inline='month')

dailyColor = input.color(defval=color.yellow, title='', inline='day')
iDaily = input.bool(false, title='Show Daily', inline='day')
hdailyColor = input.color(defval=color.yellow, title='', inline='day')
ihDaily = input.bool(false, title='Open Line', inline='day')

weeklyColor = input.color(defval=color.lime, title='', inline='week')
iWeekly = input.bool(false, title='Show Weekly', inline='week')
hweeklyColor = input.color(defval=color.lime, title='', inline='week')
ihWeekly = input.bool(false, title='Open Line', inline='week')

iSelectWeek = input.string(title="Select Week", defval="MON", options=["MON","TUE","WED","THU","FRI","SAT","SUN"])

lineStyle = input.string(title='Vertical Line Style', defval=line.style_dashed, options=[line.style_dotted, line.style_dashed, line.style_solid])
hLineStyle = input.string(title='Horizontal Line Style', defval=line.style_solid, options=[line.style_dotted, line.style_dashed, line.style_solid])
// ]

//////// 
// Preparing
// [
[yearlyTime, yearlyOpen] = request.security(syminfo.tickerid, '12M', [time, open], lookahead=barmerge.lookahead_on)
[monthlyTime, monthlyOpen] = request.security(syminfo.tickerid, 'M', [time, open], lookahead=barmerge.lookahead_on)
[dailyTime, dailyOpen] = request.security(syminfo.tickerid, 'D', [time, open], lookahead=barmerge.lookahead_on)
[weeklyTime, weeklyOpen] = request.security(syminfo.tickerid, 'W', [time, open], lookahead=barmerge.lookahead_on)
// ]

//////// 
// Making
// [
var line hLine_yearly = na
var label hlabel_yearly = na
if iYearly
    var iDrawTimeY_= 0
    yy = year
    mm = month(iBaseTime)
    dd = dayofmonth(iBaseTime)
    hh = hour(iBaseTime)
    m2 = minute(iBaseTime)

    iDrawTime = timestamp(yy, mm, dd, hh, m2)

    // double drawing prevention
    if iDrawTime != iDrawTimeY_ and iDrawTime < timenow
        // Horizontal
        if ihYearly
            line.new(iDrawTimeY_, yearlyOpen, iDrawTime, yearlyOpen, xloc=xloc.bar_time, style=hLineStyle, extend=extend.none, color=hyearlyColor, width=1)
        // Vertical
        line.new(iDrawTime, yearlyOpen, iDrawTime, yearlyOpen + syminfo.mintick, xloc=xloc.bar_time, style=lineStyle, extend=extend.both, color=yearlyColor, width=1)
        iDrawTimeY_ := iDrawTime
    if ihYearly
        line.delete(hLine_yearly)
        label.delete(hlabel_yearly)
        hLine_yearly  := line.new(iDrawTimeY_, yearlyOpen, last_bar_time, yearlyOpen, xloc=xloc.bar_time, style=hLineStyle, extend=extend.right, color=hyearlyColor, width=1)
        hlabel_yearly := label.new(last_bar_index+50, yearlyOpen, "Yearly Open", xloc=xloc.bar_index, color=color.rgb(255,255,255,100), style=label.style_label_down, textcolor=hyearlyColor, size=size.normal)

var line hLine_monthly = na
var label hlabel_monthly = na
if iMonthly
    var iDrawTimeM_= 0
    yy = year
    mm = month
    dd = dayofmonth(iBaseTime)
    hh = hour(iBaseTime)
    m2 = minute(iBaseTime)

    iDrawTime = timestamp(yy, mm, dd, hh, m2)

    // double drawing prevention
    if iDrawTime != iDrawTimeM_ and iDrawTime < timenow
        // Horizontal
        if ihMonthly
            line.new(iDrawTimeM_, monthlyOpen, iDrawTime, monthlyOpen, xloc=xloc.bar_time, style=hLineStyle, extend=extend.none, color=hmonthlyColor, width=1)
        // Vertical
        line.new(iDrawTime, monthlyOpen, iDrawTime, monthlyOpen + syminfo.mintick, xloc=xloc.bar_time, style=lineStyle, extend=extend.both, color=monthlyColor, width=1)
        iDrawTimeM_ := iDrawTime
    if ihMonthly
        line.delete(hLine_monthly)
        label.delete(hlabel_monthly)
        hLine_monthly  := line.new(iDrawTimeM_, monthlyOpen, last_bar_time, monthlyOpen, xloc=xloc.bar_time, style=hLineStyle, extend=extend.right, color=hmonthlyColor, width=1)
        hlabel_monthly := label.new(last_bar_index+50, monthlyOpen, "Monthly Open", xloc=xloc.bar_index, color=color.rgb(255,255,255,100), style=label.style_label_down, textcolor=hmonthlyColor, size=size.normal)

var line hLine_daily = na
var label hlabel_daily = na
// flag for plotshape()
iDspWeekOnDaily=false
if iDaily
    // H4を超える時間足は出力しない
    if timeframe.in_seconds(timeframe.period) <= 14400
        var iDrawTimeD_= 0
        var iOpenD_ = 0.0
        yy = year
        mm = month
        dd = dayofmonth
        hh = hour(iBaseTime)
        m2 = minute(iBaseTime)

        iDrawTime = timestamp(yy, mm, dd, hh, m2)

        hour_j = hour(iDrawTime, "GMT+9")
        dayofweek_j = dayofweek(iDrawTime, "GMT+9")

        iDsp = true
        if dayofweek_j == dayofweek.saturday and hour_j >= 6
            iDsp := false
        else if dayofweek_j == dayofweek.sunday
            iDsp := false
        else if dayofweek_j == dayofweek.monday and hour_j < 7
            iDsp := false

        if syminfo.type == "crypto"
            iDsp := true

        // double drawing prevention
        if iDrawTime != iDrawTimeD_ and iDsp and iDrawTime < timenow 
            // Horizontal
            if ihDaily
                line.new(iDrawTimeD_, iOpenD_, iDrawTime, iOpenD_, xloc=xloc.bar_time, style=hLineStyle, extend=extend.none, color=hdailyColor, width=1)
            // Vertical
            line.new(iDrawTime, dailyOpen, iDrawTime, dailyOpen + syminfo.mintick, xloc=xloc.bar_time, style=lineStyle, extend=extend.both, color=dailyColor, width=1)
            iDspWeekOnDaily:=true
            iDrawTimeD_ := iDrawTime
            iOpenD_     := dailyOpen
        if ihDaily
            line.delete(hLine_daily)
            label.delete(hlabel_daily)
            hLine_daily  := line.new(iDrawTimeD_, dailyOpen, last_bar_time, dailyOpen, xloc=xloc.bar_time, style=hLineStyle, extend=extend.right, color=hdailyColor, width=1)
            hlabel_daily := label.new(last_bar_index+50, dailyOpen, "Daily Open", xloc=xloc.bar_index, color=color.rgb(255,255,255,100), style=label.style_label_down, textcolor=hdailyColor, size=size.normal)

// Draw a vertical line to display the day of the week below the chart
plotshape(iDspWeekOnDaily and dayofweek==dayofweek.monday,    offset=0, style=shape.diamond, text="MON" , color=color.lime  , location = location.bottom, textcolor=color.lime  )
plotshape(iDspWeekOnDaily and dayofweek==dayofweek.tuesday,   offset=0, style=shape.diamond, text="TUE" , color=color.red   , location = location.bottom, textcolor=color.red   )
plotshape(iDspWeekOnDaily and dayofweek==dayofweek.wednesday, offset=0, style=shape.diamond, text="WED" , color=color.aqua  , location = location.bottom, textcolor=color.aqua  )
plotshape(iDspWeekOnDaily and dayofweek==dayofweek.thursday,  offset=0, style=shape.diamond, text="THU" , color=color.yellow, location = location.bottom, textcolor=color.yellow)
plotshape(iDspWeekOnDaily and dayofweek==dayofweek.friday,    offset=0, style=shape.diamond, text="FRI" , color=color.orange, location = location.bottom, textcolor=color.orange)
plotshape(iDspWeekOnDaily and dayofweek==dayofweek.saturday,  offset=0, style=shape.diamond, text="SAT" , color=color.gray  , location = location.bottom, textcolor=color.gray  )
plotshape(iDspWeekOnDaily and dayofweek==dayofweek.sunday,    offset=0, style=shape.diamond, text="SUN" , color=color.gray  , location = location.bottom, textcolor=color.gray  )

var line hLine_weekly = na
var label hlabel_weekly = na
if iWeekly
    // Convert day of week to number
    iDayofweek=iSelectWeek=="SUN"?0:iSelectWeek=="MON"?1:iSelectWeek=="TUE"?2:iSelectWeek=="WED"?3:iSelectWeek=="THU"?4:iSelectWeek=="FRI"?5:iSelectWeek=="SAT"?6:1

    // Selected day of week?
    if dayofweek==iDayofweek and ta.change(dayofweek)
        var iDrawTimeW_ = 0
        yy = year
        mm = month
        dd = dayofmonth
        hh = hour(iBaseTime)
        m2 = minute(iBaseTime)

        iDrawTime = timestamp(yy, mm, dd, hh, m2)

        if iDrawTime != iDrawTimeW_ and iDrawTime < timenow 
            // Horizontal
            if ihWeekly
                line.new(iDrawTimeW_, weeklyOpen[1], iDrawTime, weeklyOpen[1], xloc=xloc.bar_time, style=hLineStyle, extend=extend.none, color=hweeklyColor, width=1)
            // Vertical
            line.new(iDrawTime, weeklyOpen, iDrawTime, weeklyOpen + syminfo.mintick, xloc=xloc.bar_time, style=lineStyle, extend=extend.both, color=weeklyColor, width=1)
            iDrawTimeW_ := iDrawTime
        if ihWeekly
            line.delete(hLine_weekly)
            label.delete(hlabel_weekly)
            // hLine_weekly  := line.new(iDrawTimeW_, weeklyOpen, last_bar_time, weeklyOpen, xloc=xloc.bar_time, style=hLineStyle, extend=extend.right, color=hweeklyColor, width=1)
            if ta.change(weekofyear)
                hLine_weekly := line.new(na, na, na, na, style=hLineStyle, extend=extend.right, color=hweeklyColor, width=1)
                line.set_xy1(hLine_weekly, bar_index[1], open)
                line.set_xy2(hLine_weekly, bar_index,    open)
            hlabel_weekly := label.new(last_bar_index+50, weeklyOpen, "Weekly Open", xloc=xloc.bar_index, color=color.rgb(255,255,255,100), style=label.style_label_down, textcolor=hweeklyColor, size=size.normal)
// ]
