Sessions & Days Of The Week
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nowtech

//@version=5
indicator("Sessions & Days Of The Week", overlay=true, scale=scale.none)

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Background Overlay

show_frankfurt = input(title='Show Frankfurt', defval=false)
frankfurt = input.session(title='Frankfurt Session', defval='0200-1100')

show_london = input(title='Show London', defval=true)
london = input.session(title='London Session', defval='0300-1200')

show_ny = input(title='Show New York', defval=true)
ny = input.session(title='New York Session', defval='0800-1700')

show_sydney = input(title='Show Sydney', defval=false)
sydney = input.session(title='Sydney Session', defval='1700-0200')

show_tokyo = input(title='Show Tokyo', defval=true)
tokyo = input.session(title='Tokyo Session', defval='1900-0400')

ff_color = color.new(color.gray, 95)
london_color = color.new(color.green, 95)
ny_color = color.new(color.red, 95)
sy_color = color.new(color.purple, 95)
tokyo_color = color.new(color.blue, 95)

is_session(session) =>
    not na(time(timeframe.period, session))

is_frankfurt = is_session(frankfurt)
is_london = is_session(london)
is_ny = is_session(ny)
is_sydney = is_session(sydney)
is_tokyo = is_session(tokyo)

bgcolor(show_frankfurt and is_frankfurt ? ff_color : na)
bgcolor(show_london and is_london ? london_color : na)
bgcolor(show_ny and is_ny ? ny_color : na)
bgcolor(show_sydney and is_sydney ? sy_color : na)
bgcolor(show_tokyo and is_tokyo ? tokyo_color : na)

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Days Of The Week

bartimeoffset = time - time[1]

plotchar(dayofweek == dayofweek.monday,    char='M', title='Monday', location=location.bottom, color=color.new(color.purple, 0))
plotchar(dayofweek == dayofweek.tuesday,   char='T', title='Tuesday', location=location.bottom, color=color.new(color.blue, 0))
plotchar(dayofweek == dayofweek.wednesday, char='W', title='Wednesday', location=location.bottom, color=color.new(color.green, 0))
plotchar(dayofweek == dayofweek.thursday,  char='T', title='Thursday', location=location.bottom, color=color.new(color.orange, 0))
plotchar(dayofweek == dayofweek.friday,    char='F', title='Friday', location=location.bottom, color=color.new(color.red, 0))
plotchar(dayofweek == dayofweek.saturday,  char='S', title='Saturday', location=location.bottom, color=color.new(color.gray, 0))
plotchar(dayofweek == dayofweek.sunday,    char='S', title='Sunday', location=location.bottom, color=color.new(#b2b5be, 0))

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Monthly & Weekly Dividing Lines

//Inputs - Weekly Time Break Line
i_wb_show_intraday = input.bool(title='Show Intraday', defval=true, group='Week Break')
i_wb_show_daily = input.bool(title='Show Daily', defval=false, group='Week Break')

//Inputs - Monthly Time Break Line
i_mb_show_intraday = input.bool(title='Show Intraday', defval=true, group='Month Break')
i_mb_show_daily = input.bool(title='Show Daily', defval=true, group='Month Break')
i_mb_show_weekly = input.bool(title='Show Weekly', defval=false, group='Month Break')

//Logic
show_month_break() =>
    timeframe.isintraday and i_mb_show_intraday or timeframe.isdaily and i_mb_show_daily or timeframe.isweekly and i_mb_show_weekly
show_week_break() =>
    timeframe.isintraday and i_wb_show_intraday or timeframe.isdaily and i_wb_show_daily

is_new(resolution) =>
    t = time(resolution)
    not na(t) and (na(t[1]) or t > t[1])

is_new_month() =>
    is_new('M') and show_month_break()
is_new_week() =>
    is_new('W') and show_week_break()

//Plotting
plot(is_new_month() ? 1 : na, style=plot.style_histogram, color=color.new(color.blue, 0), title='Month Breaks', linewidth=2)
plot(is_new_week() ? 1 : na, style=plot.style_histogram, color=color.new(color.gray, 0), title='Week Breaks', linewidth=1)