Monday Session High/Low
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rahaidar

//@version=5
indicator("[2023] Monday Session High/Low", max_lines_count=500, max_labels_count=500, overlay=true, max_bars_back=5000,max_boxes_count = 500)

type MONDAY
    int   []  uid
    line  []  highline
    line  []  lowline

//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
//----------------------------------------}
//Previous day/week high/low
//----------------------------------------{
//Monday
show_pmonhl = input(true, 'Monday', inline = 'monday', group = 'Highs & Lows MTF')

pmonhl_style = input.string('⎯⎯⎯', '', options = ['⎯⎯⎯', '----', '····'], inline = 'monday', group = 'Highs & Lows MTF')

pmonhl_css = input(color.yellow, '', inline = 'monday', group = 'Highs & Lows MTF')

showhighlightmonday = input(true, 'Highlight Monday Session', inline = '2', group = 'Highs & Lows MTF')
highlightmonday_css = input(#ace5dc, '', inline = '2', group = 'Highs & Lows MTF')
highlightmonday_trans = input.int(75, 'Transparency', options = [0,25,50,75], inline = '2', group = 'Highs & Lows MTF')

history = input(10, title = 'Max History', group = 'Highs & Lows MTF', tooltip = 'Maximum number of historical lines to draw')
//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
get_line_style(style) =>
    out = switch style
        '⎯⎯⎯'  => line.style_solid
        '----' => line.style_dashed
        '····' => line.style_dotted

method in_out(MONDAY aMonday, x1, x2, hy, ly, col, styl) =>
    aMonday.uid.unshift(x1), aMonday.uid.pop()
    aMonday.highline.unshift(line.new(x1, hy, x2, hy, color= col,style=get_line_style(styl),xloc=xloc.bar_time)), aMonday.highline.pop().delete()
    aMonday.lowline.unshift(line.new(x1, ly, x2, ly, color= col,style=get_line_style(styl),xloc=xloc.bar_time)), aMonday.lowline.pop().delete()

//function which is called by plot to establish day of the week is monday return true or false
isMonday() => dayofweek(time) == 2 ? 1 : 0

//HL Output function
hl() => [high, low]

//-----------------------------------------------------------------------------}
//Main
//-----------------------------------------------------------------------------{
var MONDAY aMonday = MONDAY.new(array.new < int >(), array.new < line >(),array.new < line >())

if barstate.isfirst
    for i= 0 to history-1
        aMonday.uid.unshift(0)
        aMonday.highline.unshift(line.new(na, na, na, na))
        aMonday.lowline.unshift(line.new(na, na, na, na))

// Get the start time and end time for the week
[weekStart, weekEnd] = request.security(syminfo.tickerid, 'W', [time, time_close], lookahead = barmerge.lookahead_on)

// Monday high/low
[pmonh, pmonl] = request.security(syminfo.tickerid, 'D', hl(), lookahead = barmerge.lookahead_on)

if show_pmonhl and isMonday() and aMonday.uid.indexof(weekStart) == -1
    aMonday.in_out(weekStart,weekEnd, pmonh, pmonl, pmonhl_css, pmonhl_style)

// Highlight Monday Session
bgcolor(showhighlightmonday and isMonday() ? color.new(highlightmonday_css, highlightmonday_trans) : color(na))
