Prior day and pre-market high low
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nkwdesmond

//@version=5
indicator('Price levels: Prior day, pre-market, after-hour high low; prior day open close; daily open', 'Price levels', overlay=true)

levelSettingOption = input.string('Individual level for each day', title='Display', options=['Individual level for each day', 'Most recent level on most recent day', 'Most recent level across the chart'], group='Price level')
levelSetting = levelSettingOption == 'Most recent level on most recent day' ? 'b' : levelSettingOption == 'Most recent level across the chart' ? 'c' : 'a'

pmHighLevel     = input.bool(true, title='Pre-market high', group='Price level', inline='pmHigh')
pmLowLevel      = input.bool(true, title='Pre-market low', group='Price level', inline='pmLow')
priorHighLevel  = input.bool(true, title='Prior day high', group='Price level', inline='priorHigh')
priorLowLevel   = input.bool(true, title='Prior day low', group='Price level', inline='priorLow')
priorOpenLevel  = input.bool(false, title='Prior day open', group='Price level', inline='priorOpen')
priorCloseLevel   = input.bool(false, title='Prior day close', group='Price level', inline='priorClose')
currentOpenLevel  = input.bool(false, title='Current day open', group='Price level', inline='currentOpen')
afterHighLevel  = input.bool(false, title='After-hour high', group='Price level', inline='afterHigh')
afterLowLevel  = input.bool(false, title='After-hour Low', group='Price level', inline='afterLow')

pmHighPrice     = input.bool(true, title='Pre-market high', group='Price axis')
pmLowPrice      = input.bool(true, title='Pre-market low', group='Price axis')
priorHighPrice  = input.bool(true, title='Prior day high', group='Price axis')
priorLowPrice   = input.bool(true, title='Prior day low', group='Price axis')
priorOpenPrice  = input.bool(false, title='Prior day open', group='Price axis')
priorClosePrice   = input.bool(false, title='Prior day close', group='Price axis')
currentOpenPrice  = input.bool(false, title='Current day open', group='Price axis')
afterHighPrice  = input.bool(false, title='After-hour high', group='Price axis')
afterLowPrice  = input.bool(false, title='After-hour Low', group='Price axis')

pmHighLabelBool     = input.bool(true, title='Pre-market high', group='Label', inline='pmHigh')
pmLowLabelBool      = input.bool(true, title='Pre-market low', group='Label', inline='pmLow')
priorHighLabelBool  = input.bool(true, title='Prior day high', group='Label', inline='priorHigh')
priorLowLabelBool   = input.bool(true, title='Prior day low', group='Label', inline='priorLow')
priorOpenLabelBool  = input.bool(false, title='Prior day open', group='Label', inline='priorOpen')
priorCloseLabelBool   = input.bool(false, title='Prior day close', group='Label', inline='priorClose')
currentOpenLabelBool  = input.bool(false, title='Current open', group='Label', inline='currentOpen')
afterHighLabelBool  = input.bool(false, title='After-hour high', group='Label', inline='afterHigh')
afterLowLabelBool  = input.bool(false, title='After-hour Low', group='Label', inline='afterLow')

off   = input.int(1, title='Label offset from last bar', group='Label')

pmHighLabelAlignOption       = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='pmHigh')
pmLowLabelAlignOption        = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='pmLow')
priorHighLabelAlignOption    = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='priorHigh')
priorLowLabelAlignOption     = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='priorLow')
priorOpenLabelAlignOption    = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='priorOpen')
priorCloseLabelAlignOption     = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='priorClose')
currentOpenLabelAlignOption    = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='currentOpen')
afterHighLabelAlignOption    = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='afterHigh')
afterLowLabelAlignOption    = input.string('align left', title='', options=['align left', 'align center', 'align right'], group='Label', inline='afterLow')

pmHighLabelAlign = pmHighLabelAlignOption == 'align left' ? text.align_left : pmHighLabelAlignOption == 'align center' ? text.align_center : text.align_right
pmLowLabelAlign = pmLowLabelAlignOption == 'align left' ? text.align_left : pmLowLabelAlignOption == 'align center' ? text.align_center : text.align_right
priorHighLabelAlign = priorHighLabelAlignOption == 'align left' ? text.align_left : priorHighLabelAlignOption == 'align center' ? text.align_center : text.align_right
priorLowLabelAlign = priorLowLabelAlignOption == 'align left' ? text.align_left : priorLowLabelAlignOption == 'align center' ? text.align_center : text.align_right
priorOpenLabelAlign = priorOpenLabelAlignOption == 'align left' ? text.align_left : priorOpenLabelAlignOption == 'align center' ? text.align_center : text.align_right
priorCloseLabelAlign = priorCloseLabelAlignOption == 'align left' ? text.align_left : priorCloseLabelAlignOption == 'align center' ? text.align_center : text.align_right
currentOpenLabelAlign = currentOpenLabelAlignOption == 'align left' ? text.align_left : currentOpenLabelAlignOption == 'align center' ? text.align_center : text.align_right
afterHighLabelAlign = afterHighLabelAlignOption == 'align left' ? text.align_left : afterHighLabelAlignOption == 'align center' ? text.align_center : text.align_right
afterLowLabelAlign = afterLowLabelAlignOption == 'align left' ? text.align_left : afterLowLabelAlignOption == 'align center' ? text.align_center : text.align_right

pmHighLabelText       = input.string('PM high', title='Text',  group='Label', inline='pmHigh')
pmLowLabelText        = input.string('PM low', title='Text',  group='Label', inline='pmLow')
priorHighLabelText    = input.string('Prior day high', title='Text',  group='Label', inline='priorHigh')
priorLowLabelText     = input.string('Prior day low', title='Text',  group='Label', inline='priorLow')
priorOpenLabelText    = input.string('Prior day open', title='Text',  group='Label', inline='priorOpen')
priorCloseLabelText     = input.string('Prior day close', title='Text',  group='Label', inline='priorClose')
currentOpenLabelText    = input.string('Open', title='Text',  group='Label', inline='currentOpen')
afterHighLabelText    = input.string('AM high', title='Text',  group='Label', inline='afterHigh')
afterLowLabelText    = input.string('AM low', title='Text',  group='Label', inline='afterLow')

pmHighLabelTextColour    = input(title='', defval=#000000, group='Label', inline='pmHigh')
pmLowLabelTextColour     = input(title='', defval=#000000, group='Label', inline='pmLow')
priorHighLabelTextColour = input(title='', defval=#000000, group='Label', inline='priorHigh')
priorLowLabelTextColour  = input(title='', defval=#000000, group='Label', inline='priorLow')
priorOpenLabelTextColour = input(title='', defval=#000000, group='Label', inline='priorOpen')
priorCloseLabelTextColour  = input(title='', defval=#000000, group='Label', inline='priorClose')
currentOpenLabelTextColour = input(title='', defval=#000000, group='Label', inline='currentOpen')
afterHighLabelTextColour = input(title='', defval=#000000, group='Label', inline='afterHigh')
afterLowLabelTextColour = input(title='', defval=#ffffff, group='Label', inline='afterLow')

secondBool  = input.bool(true, title='Seconds', group='Price axis visibility', inline='second')
minuteBool  = input.bool(true, title='Minutes', group='Price axis visibility', inline='minute')
hourBool    = input.bool(false, title='Hours', group='Price axis visibility', inline='hour')
dayBool     = input.bool(false, title='Days', group='Price axis visibility', inline='day')
weekBool    = input.bool(false, title='Weeks', group='Price axis visibility', inline='week')
monthBool   = input.bool(false, title='Months', group='Price axis visibility', inline='month')

noPeriodBool = input.bool(false, title='No alert period', group='No alert period', inline='period', tooltip='Time period where alerts will not be triggered')
noPeriod = input.session('0930-0935', title='', group='No alert period', inline='period')
t = time(timeframe.period, noPeriod)
alertbool = not (noPeriodBool and t)

secondMin   = input.int(1, '', 1, 59, group='Price axis visibility', inline='second')
secondMax   = input.int(59, '-', 1, 59, group='Price axis visibility', inline='second')
minuteMin   = input.int(1, '', 1, 59, group='Price axis visibility', inline='minute')
minuteMax   = input.int(5, '-', 1, 59, group='Price axis visibility', inline='minute')
hourMin     = input.int(1, '', 1, 24, group='Price axis visibility', inline='hour')
hourMax     = input.int(24, '-', 1, 24, group='Price axis visibility', inline='hour')
dayMin      = input.int(1, '', 1, 366, group='Price axis visibility', inline='day')
dayMax      = input.int(366, '-', 1, 366, group='Price axis visibility', inline='day')
weekMin     = input.int(1, '', 1, 52, group='Price axis visibility', inline='week')
weekMax     = input.int(52, '-', 1, 52, group='Price axis visibility', inline='week')
monthMin    = input.int(1, '', 1, 12, group='Price axis visibility', inline='month')
monthMax    = input.int(12, '-', 1, 12, group='Price axis visibility', inline='month')

timeframeNumberstr = str.match(timeframe.period, '[0-9]*')
timeframeNumber = str.tonumber(timeframeNumberstr)
timeframeLetter = str.match(timeframe.period, '[A-Z]')

bool timeframePlot = false

if secondBool and timeframeLetter=='S' and secondMin<=timeframeNumber and secondMax>=timeframeNumber
    timeframePlot := true
if minuteBool and timeframeLetter=='' and minuteMin<=timeframeNumber and minuteMax>=timeframeNumber
    timeframePlot := true
if hourBool and timeframeLetter=='' and hourMin*60<=timeframeNumber and hourMax*60>=timeframeNumber
    timeframePlot := true
if dayBool and timeframeLetter=='D' and dayMin<=timeframeNumber and dayMax>=timeframeNumber
    timeframePlot := true
if weekBool and timeframeLetter=='W' and weekMin<=timeframeNumber and weekMax>=timeframeNumber
    timeframePlot := true
if monthBool and timeframeLetter=='M' and monthMin<=timeframeNumber and monthMax>=timeframeNumber
    timeframePlot := true

pmHighColour    = input(title='', defval=#81c583, group='Price level', inline='pmHigh')
pmLowColour     = input(title='', defval=#ee9899, group='Price level', inline='pmLow')
priorHighColour = input(title='', defval=#00ff00, group='Price level', inline='priorHigh')
priorLowColour  = input(title='', defval=#ff80ff, group='Price level', inline='priorLow')
priorOpenColour = input(title='', defval=#ffb54e, group='Price level', inline='priorOpen')
priorCloseColour  = input(title='', defval=#00ffff, group='Price level', inline='priorClose')
currentOpenColour = input(title='', defval=#ffff00, group='Price level', inline='currentOpen')
afterHighColour = input(title='', defval=#80ffbb, group='Price level', inline='afterHigh')
afterLowColour = input(title='', defval=#8080ff, group='Price level', inline='afterLow')

pmHighLineWidth     = input(1, 'Thickness', group='Price level', inline='pmHigh')
pmLowLineWidth      = input(1, 'Thickness', group='Price level', inline='pmLow')
priorHighLineWidth  = input(1, 'Thickness', group='Price level', inline='priorHigh')
priorLowLineWidth   = input(1, 'Thickness', group='Price level', inline='priorLow')
priorOpenLineWidth  = input(1, 'Thickness', group='Price level', inline='priorOpen')
priorCloseLineWidth   = input(1, 'Thickness', group='Price level', inline='priorClose')
currentOpenLineWidth  = input(1, 'Thickness', group='Price level', inline='currentOpen')
afterHighLineWidth  = input(1, 'Thickness', group='Price level', inline='afterHigh')
afterLowLineWidth  = input(1, 'Thickness', group='Price level', inline='afterLow')

pmHighStyleOption       = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='pmHigh')
pmLowStyleOption        = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='pmLow')
priorHighStyleOption    = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='priorHigh')
priorLowStyleOption     = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='priorLow')
priorOpenStyleOption    = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='priorOpen')
priorCloseStyleOption     = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='priorClose')
currentOpenStyleOption    = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='currentOpen')
afterHighStyleOption    = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='afterHigh')
afterLowStyleOption    = input.string('solid (─)', title='', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], group='Price level', inline='afterLow')

pmHighLineStyle = pmHighStyleOption == 'dotted (┈)' ? line.style_dotted : pmHighStyleOption == 'dashed (╌)' ? line.style_dashed : pmHighStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     pmHighStyleOption == 'arrow right (→)' ? line.style_arrow_right : pmHighStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
pmLowLineStyle = pmLowStyleOption == 'dotted (┈)' ? line.style_dotted : pmLowStyleOption == 'dashed (╌)' ? line.style_dashed : pmLowStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     pmLowStyleOption == 'arrow right (→)' ? line.style_arrow_right : pmLowStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
priorHighLineStyle = priorHighStyleOption == 'dotted (┈)' ? line.style_dotted : priorHighStyleOption == 'dashed (╌)' ? line.style_dashed : priorHighStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     priorHighStyleOption == 'arrow right (→)' ? line.style_arrow_right : priorHighStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
priorLowLineStyle = priorLowStyleOption == 'dotted (┈)' ? line.style_dotted : priorLowStyleOption == 'dashed (╌)' ? line.style_dashed : priorLowStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     priorLowStyleOption == 'arrow right (→)' ? line.style_arrow_right : priorLowStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
priorOpenLineStyle = priorOpenStyleOption == 'dotted (┈)' ? line.style_dotted : priorOpenStyleOption == 'dashed (╌)' ? line.style_dashed : priorOpenStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     priorOpenStyleOption == 'arrow right (→)' ? line.style_arrow_right : priorOpenStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
priorCloseLineStyle = priorCloseStyleOption == 'dotted (┈)' ? line.style_dotted : priorCloseStyleOption == 'dashed (╌)' ? line.style_dashed : priorCloseStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     priorCloseStyleOption == 'arrow right (→)' ? line.style_arrow_right : priorCloseStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
currentOpenLineStyle = currentOpenStyleOption == 'dotted (┈)' ? line.style_dotted : currentOpenStyleOption == 'dashed (╌)' ? line.style_dashed : currentOpenStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     currentOpenStyleOption == 'arrow right (→)' ? line.style_arrow_right : currentOpenStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
afterHighLineStyle = afterHighStyleOption == 'dotted (┈)' ? line.style_dotted : afterHighStyleOption == 'dashed (╌)' ? line.style_dashed : afterHighStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     afterHighStyleOption == 'arrow right (→)' ? line.style_arrow_right : afterHighStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid
afterLowLineStyle = afterLowStyleOption == 'dotted (┈)' ? line.style_dotted : afterLowStyleOption == 'dashed (╌)' ? line.style_dashed : afterLowStyleOption == 'arrow left (←)' ? line.style_arrow_left :
     afterLowStyleOption == 'arrow right (→)' ? line.style_arrow_right : afterLowStyleOption == 'arrows both (↔)' ? line.style_arrow_both : line.style_solid

var line pmHighLine = na
var line pmLowLine = na
var line priorHighLine = na
var line priorLowLine = na
var line priorOpenLine = na
var line priorCloseLine = na
var line currentOpenLine = na
var line afterHighLine = na
var line afterLowLine = na
var float pmHigh = na
var float pmLow = na
var float priorHigh = na
var float priorLow = na
var float priorOpen = na
var float priorClose = na
var float currentHigh = na
var float currentLow = na
var float currentOpen = na
var float currentClose = na
var float afterHigh = na
var float afterLow = na
var label pmHighLabel = na
var label pmLowLabel = na
var label priorHighLabel = na
var label priorLowLabel = na
var label priorOpenLabel = na
var label priorCloseLabel = na
var label currentOpenLabel = na
var label afterHighLabel = na
var label afterLowLabel = na

if session.isfirstbar and session.ispremarket // initialize pmHigh and pmLow for new day
    pmHigh := high
    pmLow := low

// var float temp = na
// array <float> test = request.security_lower_tf(syminfo.tickerid,'1',high)
// if array.size(test)>0 and time('1','0400-0859','UTC-4') // time condition does not work because request.security_lower_tf will take all prices in the completed candle
//     temp := array.max(test)
if high > pmHigh and session.ispremarket
    pmHigh := high
    line.set_y1(pmHighLine, pmHigh) // shift pmHigh in real-time
    line.set_y2(pmHighLine, pmHigh)
    label.set_y(pmHighLabel, pmHigh)

if low < pmLow and session.ispremarket
    pmLow := low
    line.set_y1(pmLowLine, pmLow) // shift pmLow in real-time
    line.set_y2(pmLowLine, pmLow)
    label.set_y(pmLowLabel, pmLow)

currentOpen := request.security(syminfo.tickerid,'D',open,lookahead=barmerge.lookahead_on)
if not session.isfirstbar_regular
    currentOpen := currentOpen[1]
if session.isfirstbar_regular
    // currentHigh := high
    // currentLow := low
    // currentOpen := open
    line.set_extend(currentOpenLine, extend.none) // currentOpen line should only be drawn starting from first regular bar
    if levelSetting!='a'
        line.delete(currentOpenLine)
    label.delete(currentOpenLabel)
    if timeframe.isintraday 
        if currentOpenLevel
            currentOpenLine := line.new(bar_index, currentOpen, bar_index, currentOpen, color=currentOpenColour, style=currentOpenLineStyle, width=currentOpenLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if currentOpenLabelBool
            currentOpenLabel := label.new(bar_index + off, currentOpen, text=currentOpenLabelText, color=currentOpenColour, style=label.style_label_left, textcolor=currentOpenLabelTextColour, size=size.normal, textalign=currentOpenLabelAlign)

if high > currentHigh and session.ismarket
    currentHigh := high // update currentHigh

if low < currentLow and session.ismarket
    currentLow := low // update currentLow

if session.ismarket
    currentClose := close // update currentClose

if session.ispostmarket and session.ispostmarket[1] == false // initialize after-hour prices for the day
    afterHigh := high
    afterLow := low
    
if session.ispostmarket and high > afterHigh
    afterHigh := high
    line.set_y1(afterHighLine, afterHigh) // shift afterHigh in real-time
    line.set_y2(afterHighLine, afterHigh)
    label.set_y(afterHighLabel, afterLow)
    
if session.ispostmarket and low < afterLow
    afterLow := low
    line.set_y1(afterLowLine, afterLow) // shift afterHigh in real-time
    line.set_y2(afterLowLine, afterLow)
    label.set_y(afterLowLabel, afterLow)

if session.ispostmarket and session.ispostmarket[1] == false
    line.set_extend(afterHighLine, extend.none) // afterhourlines should only be drawn starting from first after-hour bar
    line.set_extend(afterLowLine, extend.none)
    if levelSetting!='a'
        line.delete(afterHighLine)
        line.delete(afterLowLine)
    label.delete(afterLowLabel)
    label.delete(afterHighLabel)
    if timeframe.isintraday
        if afterHighLevel
            afterHighLine := line.new(bar_index, afterHigh, bar_index, afterHigh, color=afterHighColour, style=afterHighLineStyle, width=afterHighLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if afterHighLabelBool
            afterHighLabel := label.new(bar_index + off, afterHigh, text=afterHighLabelText, color=afterHighColour, style=label.style_label_left, textcolor=afterHighLabelTextColour, size=size.normal, textalign=afterHighLabelAlign)
        if afterLowLevel
            afterLowLine := line.new(bar_index, afterLow, bar_index, afterLow, color=afterLowColour, style=afterLowLineStyle, width=afterLowLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if afterLowLabelBool
            afterLowLabel := label.new(bar_index + off, afterLow, text=afterLowLabelText, color=afterLowColour, style=label.style_label_left, textcolor=afterLowLabelTextColour, size=size.normal, textalign=afterLowLabelAlign)

// priorHigh := request.security(syminfo.tickerid,'D',session.isfirstbar?(syminfo.session==session.extended?high:high[1]):priorHigh,lookahead=barmerge.lookahead_on)
priorHigh := request.security(syminfo.tickerid,'D',(syminfo.session==session.extended?high:high[1]),lookahead=barmerge.lookahead_on) // session.isfirstbar does not work here
priorLow := request.security(syminfo.tickerid,'D',(syminfo.session==session.extended?low:low[1]),lookahead=barmerge.lookahead_on)
priorOpen := request.security(syminfo.tickerid,'D',(syminfo.session==session.extended?open:open[1]),lookahead=barmerge.lookahead_on)
priorClose := request.security(syminfo.tickerid,'D',(syminfo.session==session.extended?close:close[1]),lookahead=barmerge.lookahead_on)
if not session.isfirstbar
    priorHigh := priorHigh[1]
    priorLow := priorLow[1]
    priorOpen := priorOpen[1]
    priorClose := priorClose[1]
if session.isfirstbar
    line.set_extend(pmHighLine, extend.none) // so that lines from prior days will not extend right to price axis 
    line.set_extend(pmLowLine, extend.none)
    line.set_extend(priorHighLine, extend.none)
    line.set_extend(priorLowLine, extend.none)
    line.set_extend(priorOpenLine, extend.none)
    line.set_extend(priorCloseLine, extend.none)
    // priorHigh := currentHigh[1] // since this is first bar, the previous value will be that of prior day
    // priorLow := currentLow[1]
    // priorOpen := currentOpen[1]
    // priorClose := currentClose[1]
    if levelSetting!='a' // delete the old lines if option 'a' is not selected
        line.delete(pmHighLine)
        line.delete(pmLowLine)
        line.delete(priorHighLine)
        line.delete(priorLowLine)
        line.delete(priorOpenLine)
        line.delete(priorCloseLine)
    label.delete(pmHighLabel)
    label.delete(pmLowLabel)
    label.delete(priorHighLabel)
    label.delete(priorLowLabel)
    label.delete(priorOpenLabel)
    label.delete(priorCloseLabel)
    if timeframe.isintraday 
        if pmHighLevel
            pmHighLine := line.new(bar_index, pmHigh, bar_index, pmHigh, color=pmHighColour, style=pmHighLineStyle, width=pmHighLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if pmHighLabelBool
            pmHighLabel := label.new(bar_index + off, pmHigh, text=pmHighLabelText, color=pmHighColour, style=label.style_label_left, textcolor=pmHighLabelTextColour, size=size.normal, textalign=pmHighLabelAlign)
        if pmLowLevel
            pmLowLine := line.new(bar_index, pmLow, bar_index, pmLow, color=pmLowColour, style=pmLowLineStyle, width=pmLowLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if pmLowLabelBool
            pmLowLabel := label.new(bar_index + off, pmLow, text=pmLowLabelText, color=pmLowColour, style=label.style_label_left, textcolor=pmLowLabelTextColour, size=size.normal, textalign=pmLowLabelAlign)
        if priorHighLevel
            priorHighLine := line.new(bar_index, priorHigh, bar_index, priorHigh, color=priorHighColour, style=priorHighLineStyle, width=priorHighLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if priorHighLabelBool
            priorHighLabel := label.new(bar_index + off, priorHigh, text=priorHighLabelText, color=priorHighColour, style=label.style_label_left, textcolor=priorHighLabelTextColour, size=size.normal, textalign=priorHighLabelAlign)
        if priorLowLevel
            priorLowLine := line.new(bar_index, priorLow, bar_index, priorLow, color=priorLowColour, style=priorLowLineStyle, width=priorLowLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if priorLowLabelBool
            priorLowLabel := label.new(bar_index + off, priorLow, text=priorLowLabelText, color=priorLowColour, style=label.style_label_left, textcolor=priorLowLabelTextColour, size=size.normal, textalign=priorLowLabelAlign)
        if priorOpenLevel
            priorOpenLine := line.new(bar_index, priorOpen, bar_index, priorOpen, color=priorOpenColour, style=priorOpenLineStyle, width=priorOpenLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if priorOpenLabelBool
            priorOpenLabel := label.new(bar_index + off, priorOpen, text=priorOpenLabelText, color=priorOpenColour, style=label.style_label_left, textcolor=priorOpenLabelTextColour, size=size.normal, textalign=priorOpenLabelAlign)
        if priorCloseLevel
            priorCloseLine := line.new(bar_index, priorClose, bar_index, priorClose, color=priorCloseColour, style=priorCloseLineStyle, width=priorCloseLineWidth, extend=levelSetting=='c' ? extend.both : extend.right)
        if priorCloseLabelBool
            priorCloseLabel := label.new(bar_index + off, priorClose, text=priorCloseLabelText, color=priorCloseColour, style=label.style_label_left, textcolor=priorCloseLabelTextColour, size=size.normal, textalign=priorCloseLabelAlign)
    
line.set_x2(pmHighLine, bar_index) 
line.set_x2(pmLowLine, bar_index)
line.set_x2(priorHighLine, bar_index)
line.set_x2(priorLowLine, bar_index)
line.set_x2(priorOpenLine, bar_index)
line.set_x2(priorCloseLine, bar_index)
if not session.ispremarket
    line.set_x2(currentOpenLine, bar_index) // currentOpen should not be drawn during premarket
line.set_x2(afterHighLine, bar_index)
line.set_x2(afterLowLine, bar_index)

label.set_x(pmHighLabel, bar_index + off) 
label.set_x(pmLowLabel, bar_index + off)
label.set_x(priorHighLabel, bar_index + off)
label.set_x(priorLowLabel, bar_index + off)
label.set_x(priorOpenLabel, bar_index + off)
label.set_x(priorCloseLabel, bar_index + off)
label.set_x(currentOpenLabel, bar_index + off)
label.set_x(afterHighLabel, bar_index + off)
label.set_x(afterLowLabel, bar_index + off)

plot(timeframe.isintraday and pmHighPrice and timeframePlot ? pmHigh : na, color=pmHighColour, editable=false, display=display.price_scale) // plot the value on price axis without the line
plot(timeframe.isintraday and pmLowPrice and timeframePlot? pmLow : na, color=pmLowColour, editable=false, display=display.price_scale)
plot(timeframe.isintraday and priorHighPrice and timeframePlot ? priorHigh : na, color=priorHighColour, editable=false, display=display.price_scale)
plot(timeframe.isintraday and priorLowPrice and timeframePlot ? priorLow : na, color=priorLowColour, editable=false, display=display.price_scale)
plot(timeframe.isintraday and priorOpenPrice and timeframePlot ? priorOpen : na, color=priorOpenColour, editable=false, display=display.price_scale)
plot(timeframe.isintraday and priorClosePrice and timeframePlot ? priorClose : na, color=priorCloseColour, editable=false, display=display.price_scale)
plot(timeframe.isintraday and currentOpenPrice and timeframePlot ? currentOpen : na, color=currentOpenColour, editable=false, display=display.price_scale)
plot(timeframe.isintraday and afterHighPrice and timeframePlot ? afterHigh : na, color=afterHighColour, editable=false, display=display.price_scale)
plot(timeframe.isintraday and afterLowPrice and timeframePlot ? afterLow : na, color=afterLowColour, editable=false, display=display.price_scale)

// plotshape(timeframe.isintraday and afterLowLabelBool and timeframePlot ? afterLow : na, style=shape.labeldown, location=location.absolute, color=afterLowColour, offset=6, text="afterLow", textcolor=#ffffff, editable=false, show_last=1, display=display.pane)

alertcondition(alertbool and session.ismarket and close>=pmHigh and close[1]<pmHigh, title='Cross abv pm high (do not use Once Per Bar Close)', message='Price crossed above pre-market high!')
alertcondition(alertbool and session.ismarket and close<=pmLow and close[1]>pmLow, title='Cross blw pm low (do not use Once Per Bar Close)', message='Price crossed below pre-market low!')

alertcondition(alertbool and session.ismarket and close>=pmHigh and open<pmHigh, title='Close abv pm high (use Once Per Bar Close)', message='Price closed above pre-market high!')
alertcondition(alertbool and session.ismarket and close<=pmLow and open>pmLow, title='Close blw pm low (use Once Per Bar Close)', message='Price closed below pre-market low!')

// label.new(bar_index, pmHigh, text=str.tostring(pmHigh))
// plot(pmHigh, style=plot.style_linebr, color=#81c583, linewidth=1) 
// plot(pmLow, style=plot.style_linebr, color=#ee9899, linewidth=1) 
// plot(high, style=plot.style_linebr, color=#ffff00, linewidth=1)
// plot(priorLow, style=plot.style_linebr, color=#ff0000, linewidth=1)