Detect BOS in Five Candles with MTF - Alert [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
indicator("Detect BOS in Five Candles with MTF - Alert [MsF]", shorttitle = "Detect BOS /w MTF", overlay = true)

var GRP1 = "--- Time Frame Settings ---"
TF1 = input.timeframe("60", group = GRP1)
TF2 = input.timeframe("240", group = GRP1)
var GRP2 = "--- MTF Candles Settings ---"
show_HTF1_Candles = input.bool(true, title = "show_MTF1_Candles", group = GRP2)
show_HTF2_Candles = input.bool(true, title = "show_MTF2_Candles", group = GRP2)
show_Labels       = input.bool(true, title = "show_OHLC_Labels", group = GRP2)
show_RemainTime   = input.bool(true, title = "show_Remain_Time", group = GRP2)
chart_color_up    = input.color(color.lime, group = GRP2)
chart_color_even  = input.color(color.white, group = GRP2)
chart_color_down  = input.color(color.red, group = GRP2)
offset_HTF_Candles = input.int(0, title = "Candles OFFSET", group = GRP2)
var GRP21 = "--- High/Low Lines Settings ---"
show_HLLines_1 = input.bool(false, title = "", inline='1', group = GRP21)
lineColor_1    = input.color(defval=color.rgb( 255, 235,  59, 50), title='MTF1', inline='1', group = GRP21)
lineStyle_1    = input.string(title='Style', defval=line.style_dotted, options=[line.style_dotted, line.style_dashed, line.style_solid], inline = "1", group = GRP21)
lineWidth_1    = input.int(title='Width', defval=1, inline = "1", group = GRP21)
show_HLLines_2 = input.bool(false, title = "", inline='2', group = GRP21)
lineColor_2    = input.color(defval=color.rgb( 255, 255, 255, 70), title='MTF2', inline='2', group = GRP21)
lineStyle_2    = input.string(title='Style', defval=line.style_dashed, options=[line.style_dotted, line.style_dashed, line.style_solid], inline = "2", group = GRP21)
lineWidth_2    = input.int(title='Width', defval=1, inline = "2", group = GRP21)

var GRP3 = "--- MTF BOS Settings ---"
show_BOS_Lines    = input.bool(true, title = "show_BOS_Lines", group = GRP3)
show_BOS_Labels   = input.bool(true, title = "show_BOS_Labels", group = GRP3)
line_color_BOS    = input.color(color.blue, group = GRP3, inline = "BOS_line")
line_width_BOS    = input.int(defval = 1, minval = 1, title = "width", group = GRP3, inline = "BOS_line")

show_HTF1_BOSlines = show_BOS_Lines //input.bool(true)
show_HTF2_BOSlines = show_BOS_Lines //input.bool(true)

////////////////
// functions
// [
convert_mSecToTime(aMsec) =>
    _sTime = ""

    iMsec = aMsec
    if syminfo.type != "crypto"
        iMsec := aMsec - (19 * 1000 * 60 * 60) + (1 * 1000 * 60 * 60 * 24) // 帳尻合わせ：19時間引いて、1日足す

    iSec  = second(iMsec)
    iMin  = minute(iMsec)
    iHour = hour(iMsec)
    iDay  = math.floor(iMsec / 1000 / 60 / 60 / 24)

    if iDay > 0
        _sTime := _sTime + str.tostring(iDay) + "D " + str.format_time(iMsec, "HH:mm:ss", syminfo.timezone)
    else if iHour > 0
        _sTime := _sTime + str.format_time(iMsec, "HH:mm:ss", syminfo.timezone)
    else
        _sTime := _sTime + str.format_time(iMsec, "mm:ss", syminfo.timezone)

    _sTime
// ]
////////////////

[O_,H_,L_,C_, O_1,H_1,L_1,C_1, O_2,H_2,L_2,C_2, O_3,H_3,L_3,C_3, O_4,H_4,L_4,C_4] = request.security(syminfo.tickerid, TF1, [open[0], high[0], low[0], close[0], open[1], high[1], low[1], close[1], open[2], high[2], low[2], close[2], open[3], high[3], low[3], close[3], open[4], high[4], low[4], close[4]])
[DO_,DH_,DL_,DC_, DO_1,DH_1,DL_1,DC_1, DO_2,DH_2,DL_2,DC_2, DO_3,DH_3,DL_3,DC_3, DO_4,DH_4,DL_4,DC_4] = request.security(syminfo.tickerid, TF2, [open[0], high[0], low[0], close[0], open[1], high[1], low[1], close[1], open[2], high[2], low[2], close[2], open[3], high[3], low[3], close[3], open[4], high[4], low[4], close[4]])

var line line_H_L = na
var line line_H_L_1 = na
var line line_H_L_2 = na
var line line_H_L_3 = na
var line line_H_L_4 = na
var line line_O_C = na
var line line_O_C_1 = na
var line line_O_C_2 = na
var line line_O_C_3 = na
var line line_O_C_4 = na
var label label_O = na
var label label_H = na
var label label_L = na
var label label_C = na
var label label_LL = na
var label label_RT = na
var label label_BS_H = na
var label label_BS_L = na
var line line_HTF1_BOS_H1 = na // ひとつ前の足で引くライン
var line line_HTF1_BOS_H0 = na // 今の足で引くライン
var line line_HTF1_BOS_L1 = na // ひとつ前の足で引くライン
var line line_HTF1_BOS_L0 = na // 今の足で引くライン
var bool alert_HTF1_BOS_1 = false // アラート判定ひとつ前の足確定時
var bool alert_HTF1_BOS_0 = false // アラート判定今の足でBreak時
var line line_HTF1_H = na
var line line_HTF1_L = na

var line line_D_H_L = na
var line line_D_H_L_1 = na
var line line_D_H_L_2 = na
var line line_D_H_L_3 = na
var line line_D_H_L_4 = na
var line line_D_O_C = na
var line line_D_O_C_1 = na
var line line_D_O_C_2 = na
var line line_D_O_C_3 = na
var line line_D_O_C_4 = na
var label label_D_O = na
var label label_D_H = na
var label label_D_L = na
var label label_D_C = na
var label label_D_LL = na
var label label_D_RT = na
var label label_D_BS_H = na
var label label_D_BS_L = na
var line line_HTF2_BOS_H1 = na // ひとつ前の足で引くライン
var line line_HTF2_BOS_H0 = na // 今の足で引くライン
var line line_HTF2_BOS_L1 = na // ひとつ前の足で引くライン
var line line_HTF2_BOS_L0 = na // 今の足で引くライン
var bool alert_HTF2_BOS_1 = false // アラート判定ひとつ前の足確定時
var bool alert_HTF2_BOS_0 = false // アラート判定今の足でBreak時
var line line_HTF2_H = na
var line line_HTF2_L = na

// MTFキャンドルの描画スタート位置＝オフセットを加味したバーインデックス
htf_candles_startingPos = bar_index + offset_HTF_Candles

if show_HTF1_Candles
    line.delete(line_H_L)
    line.delete(line_H_L_1)
    line.delete(line_H_L_2)
    line.delete(line_H_L_3)
    line.delete(line_H_L_4)
    line.delete(line_O_C)
    line.delete(line_O_C_1)
    line.delete(line_O_C_2)
    line.delete(line_O_C_3)
    line.delete(line_O_C_4)
    label.delete(label_O)
    label.delete(label_H)
    label.delete(label_L)
    label.delete(label_C)
    label.delete(label_LL)
    label.delete(label_RT)
    line_H_L := line.new(htf_candles_startingPos + 32, O_, htf_candles_startingPos + 32, C_, color=(O_<C_?chart_color_up:O_>C_?chart_color_down:chart_color_even), width=9)
    line_H_L_1 := line.new(htf_candles_startingPos + 29, O_1, htf_candles_startingPos + 29, C_1, color=(O_1<C_1?chart_color_up:O_1>C_1?chart_color_down:chart_color_even), width=9)
    line_H_L_2 := line.new(htf_candles_startingPos + 26, O_2, htf_candles_startingPos + 26, C_2, color=(O_2<C_2?chart_color_up:O_2>C_2?chart_color_down:chart_color_even), width=9)
    line_H_L_3 := line.new(htf_candles_startingPos + 23, O_3, htf_candles_startingPos + 23, C_3, color=(O_3<C_3?chart_color_up:O_3>C_3?chart_color_down:chart_color_even), width=9)
    line_H_L_4 := line.new(htf_candles_startingPos + 20, O_4, htf_candles_startingPos + 20, C_4, color=(O_4<C_4?chart_color_up:O_4>C_4?chart_color_down:chart_color_even), width=9)
    
    line_O_C := line.new(htf_candles_startingPos + 32, H_, htf_candles_startingPos + 32, L_, color=(O_<C_?chart_color_up:O_>C_?chart_color_down:chart_color_even))
    line_O_C_1 := line.new(htf_candles_startingPos + 29, H_1, htf_candles_startingPos + 29, L_1, color=(O_1<C_1?chart_color_up:O_1>C_1?chart_color_down:chart_color_even))
    line_O_C_2 := line.new(htf_candles_startingPos + 26, H_2, htf_candles_startingPos + 26, L_2, color=(O_2<C_2?chart_color_up:O_2>C_2?chart_color_down:chart_color_even))
    line_O_C_3 := line.new(htf_candles_startingPos + 23, H_3, htf_candles_startingPos + 23, L_3, color=(O_3<C_3?chart_color_up:O_3>C_3?chart_color_down:chart_color_even))
    line_O_C_4 := line.new(htf_candles_startingPos + 20, H_4, htf_candles_startingPos + 20, L_4, color=(O_4<C_4?chart_color_up:O_4>C_4?chart_color_down:chart_color_even))
    
    if show_HTF1_BOSlines
        line.delete(line_HTF1_BOS_H0)
        line.delete(line_HTF1_BOS_H1)
        line.delete(line_HTF1_BOS_L0)
        line.delete(line_HTF1_BOS_L1)
        label.delete(label_BS_H)
        label.delete(label_BS_L)
        alert_HTF1_BOS_0 := false
        alert_HTF1_BOS_1 := false
        // 5個のローソク番号：[4][3][2][1][0]
        // [4][3][2]のローソクから判定する
        if L_4>L_3 and L_3<L_2
            if H_2<C_1
                // [2]と[1]でBOS発生してたらBOS引いてアラート設定
                line_HTF1_BOS_H1 := line.new(htf_candles_startingPos + 26, H_2, htf_candles_startingPos + 32, H_2, color=line_color_BOS, width=line_width_BOS)
                alert_HTF1_BOS_1 := true
                // BOS label
                if show_BOS_Labels
                    label.delete(label_BS_H)
                    label_BS_H := label.new(htf_candles_startingPos + 48, H_2, text = "BOS: " + str.tostring(H_2), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))
            else
                // [2]と[1]でBOS発生してない場合はBOS候補の線（隣のBarまで延長）のみ引く
                line_HTF1_BOS_H1 := line.new(htf_candles_startingPos + 26, H_2, htf_candles_startingPos + 35, H_2, color=line_color_BOS, width=line_width_BOS)
                if H_2<C_
                    // [2]のBOS候補の線を[0]で抜いたらアラート設定
                    alert_HTF1_BOS_0 := true
                // BOS label
                if show_BOS_Labels
                    label.delete(label_BS_H)
                    label_BS_H := label.new(htf_candles_startingPos + 48, H_2, text = "BOS: " + str.tostring(H_2), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))
        if H_4<H_3 and H_3>H_2
            if L_2>C_1
                // [2]と[1]でBOS発生してたらBOS引いてアラート設定
                line_HTF1_BOS_L1 := line.new(htf_candles_startingPos + 26, L_2, htf_candles_startingPos + 32, L_2, color=line_color_BOS, width=line_width_BOS)
                alert_HTF1_BOS_1 := true
                // BOS label
                if show_BOS_Labels
                    label.delete(label_BS_L)
                    label_BS_L := label.new(htf_candles_startingPos + 48, L_2, text = "BOS: " + str.tostring(L_2), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))
            else
                // [2]と[1]でBOS発生してない場合はBOS候補の線（隣のBarまで延長）のみ引く
                line_HTF1_BOS_L1 := line.new(htf_candles_startingPos + 26, L_2, htf_candles_startingPos + 35, L_2, color=line_color_BOS, width=line_width_BOS)
                if L_2>C_
                    // [2]のBOS候補の線を[0]で抜いたらアラート設定
                    alert_HTF1_BOS_0 := true
                // BOS label
                if show_BOS_Labels
                    label.delete(label_BS_L)
                    label_BS_L := label.new(htf_candles_startingPos + 48, L_2, text = "BOS: " + str.tostring(L_2), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))

        // [3][2][1]のローソクから判定する
        if L_3>L_2 and L_2<L_1
            line_HTF1_BOS_H0 := line.new(htf_candles_startingPos + 29, H_1, htf_candles_startingPos + 35, H_1, color=line_color_BOS, width=line_width_BOS)
            if H_1<C_
                // [1]のBOS候補の線を[0]で抜いたらアラート設定
                alert_HTF1_BOS_0 := true
            // BOS label
            if show_BOS_Labels
                label.delete(label_BS_H)
                label_BS_H := label.new(htf_candles_startingPos + 48, H_1, text = "BOS: " + str.tostring(H_1), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))
        if H_3<H_2 and H_2>H_1
            line_HTF1_BOS_L0 := line.new(htf_candles_startingPos + 29, L_1, htf_candles_startingPos + 35, L_1, color=line_color_BOS, width=line_width_BOS)
            if L_1>C_
                // [1]のBOS候補の線を[0]で抜いたらアラート設定
                alert_HTF1_BOS_0 := true
            // BOS label
            if show_BOS_Labels
                label.delete(label_BS_L)
                label_BS_L := label.new(htf_candles_startingPos + 48, L_1, text = "BOS: " + str.tostring(L_1), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))

    if show_Labels
        label_O := label.new(htf_candles_startingPos + 48, O_, text = "O : " + str.tostring(O_), textcolor=(O_<C_?chart_color_up:O_>C_?chart_color_down:chart_color_even), style=label.style_label_left, color = color.new(chart_color_even, 70))
        label_H := label.new(htf_candles_startingPos + 33, H_, text = "H : " + str.tostring(H_), textcolor=(O_<C_?chart_color_up:O_>C_?chart_color_down:chart_color_even), style=label.style_label_left, color = color.new(chart_color_even, 70))
        label_L := label.new(htf_candles_startingPos + 48, L_, text = "L : " + str.tostring(L_), textcolor=(O_<C_?chart_color_up:O_>C_?chart_color_down:chart_color_even), style=label.style_label_left, color = color.new(chart_color_even, 70))
        label_C := label.new(htf_candles_startingPos + 33, C_, text = "C : " + str.tostring(C_), textcolor=(O_<C_?chart_color_up:O_>C_?chart_color_down:chart_color_even), style=label.style_label_left, color = color.new(chart_color_even, 70))
//        [line_H_L,line_H_L_1,line_H_L_2,line_H_L_3,line_H_L_4, line_O_C,line_O_C_1,line_O_C_2,line_O_C_3,line_O_C_4, label_O, label_H, label_L, label_C, label_LL, label_RT]

    iHighest = H_
    if iHighest < H_1
        iHighest := H_1
    if iHighest < H_2
        iHighest := H_2
    if iHighest < H_3
        iHighest := H_3
    if iHighest < H_4
        iHighest := H_4

    iLowest = L_
    if iLowest > L_1
        iLowest := L_1
    if iLowest > L_2
        iLowest := L_2
    if iLowest > L_3
        iLowest := L_3
    if iLowest > L_4
        iLowest := L_4

    if show_RemainTime
        label_RT := label.new(htf_candles_startingPos + 40, iHighest, text = convert_mSecToTime(time_close(TF1)-timenow), textcolor=(O_<C_?chart_color_up:O_>C_?chart_color_down:chart_color_even), style=label.style_label_right, color = na, size=size.large)
    label_LL := label.new(htf_candles_startingPos + 10, (H_ + L_)/2, text = "TF: " + str.tostring(TF1), textcolor=(O_<C_?chart_color_up:O_>C_?chart_color_down:chart_color_even), style=label.style_label_right, color = na, size=size.large)     

    if show_HLLines_1
        line.delete(line_HTF1_H)
        line.delete(line_HTF1_L)
        line_HTF1_H := line.new(htf_candles_startingPos, iHighest, htf_candles_startingPos+1, iHighest, extend=extend.both, color=lineColor_1, style=lineStyle_1, width=lineWidth_1)
        line_HTF1_L := line.new(htf_candles_startingPos, iLowest, htf_candles_startingPos+1, iLowest, extend=extend.both, color=lineColor_1, style=lineStyle_1, width=lineWidth_1)

if show_HTF2_Candles
    line.delete(line_D_H_L)
    line.delete(line_D_H_L_1)
    line.delete(line_D_H_L_2)
    line.delete(line_D_H_L_3)
    line.delete(line_D_H_L_4)
    line.delete(line_D_O_C)
    line.delete(line_D_O_C_1)
    line.delete(line_D_O_C_2)
    line.delete(line_D_O_C_3)
    line.delete(line_D_O_C_4)
    label.delete(label_D_O)
    label.delete(label_D_H)
    label.delete(label_D_L)
    label.delete(label_D_C)
    label.delete(label_D_LL)
    label.delete(label_D_RT)
    line_D_H_L := line.new(htf_candles_startingPos + 92, DO_, htf_candles_startingPos + 92, DC_, color=(DO_<DC_?chart_color_up:DO_>DC_?chart_color_down:chart_color_even), width=9)
    line_D_H_L_1 := line.new(htf_candles_startingPos + 89, DO_1, htf_candles_startingPos + 89, DC_1, color=(DO_1<DC_1?chart_color_up:DO_1>DC_1?chart_color_down:chart_color_even), width=9)
    line_D_H_L_2 := line.new(htf_candles_startingPos + 86, DO_2, htf_candles_startingPos + 86, DC_2, color=(DO_2<DC_2?chart_color_up:DO_2>DC_2?chart_color_down:chart_color_even), width=9)
    line_D_H_L_3 := line.new(htf_candles_startingPos + 83, DO_3, htf_candles_startingPos + 83, DC_3, color=(DO_3<DC_3?chart_color_up:DO_3>DC_3?chart_color_down:chart_color_even), width=9)
    line_D_H_L_4 := line.new(htf_candles_startingPos + 80, DO_4, htf_candles_startingPos + 80, DC_4, color=(DO_4<DC_4?chart_color_up:DO_4>DC_4?chart_color_down:chart_color_even), width=9)
    
    line_D_O_C := line.new(htf_candles_startingPos + 92, DH_, htf_candles_startingPos + 92, DL_, color=(O_<DC_?chart_color_up:DO_>DC_?chart_color_down:chart_color_even))
    line_D_O_C_1 := line.new(htf_candles_startingPos + 89, DH_1, htf_candles_startingPos + 89, DL_1, color=(DO_1<DC_1?chart_color_up:DO_1>DC_1?chart_color_down:chart_color_even))
    line_D_O_C_2 := line.new(htf_candles_startingPos + 86, DH_2, htf_candles_startingPos + 86, DL_2, color=(DO_2<DC_2?chart_color_up:DO_2>DC_2?chart_color_down:chart_color_even))
    line_D_O_C_3 := line.new(htf_candles_startingPos + 83, DH_3, htf_candles_startingPos + 83, DL_3, color=(DO_3<DC_3?chart_color_up:DO_3>DC_3?chart_color_down:chart_color_even))
    line_D_O_C_4 := line.new(htf_candles_startingPos + 80, DH_4, htf_candles_startingPos + 80, DL_4, color=(DO_4<DC_4?chart_color_up:DO_4>DC_4?chart_color_down:chart_color_even))
    
    if show_HTF2_BOSlines
        line.delete(line_HTF2_BOS_H0)
        line.delete(line_HTF2_BOS_H1)
        line.delete(line_HTF2_BOS_L0)
        line.delete(line_HTF2_BOS_L1)
        label.delete(label_D_BS_H)
        label.delete(label_D_BS_L)
        alert_HTF2_BOS_0 := false
        alert_HTF2_BOS_1 := false
        // 5個のローソク番号：[4][3][2][1][0]
        // [4][3][2]のローソクから判定する
        if DL_4>DL_3 and DL_3<DL_2
            if DH_2<DC_1
                // [2]と[1]でBOS発生してたらBOS引いてアラート設定
                line_HTF2_BOS_H1 := line.new(htf_candles_startingPos + 86, DH_2, htf_candles_startingPos + 92, DH_2, color=line_color_BOS, width=line_width_BOS)
                alert_HTF2_BOS_1 := true
                // BOS label
                if show_BOS_Labels
                    label.delete(label_D_BS_H)
                    label_D_BS_H := label.new(htf_candles_startingPos + 108, DH_2, text = "BOS: " + str.tostring(DH_2), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))
            else
                // [2]と[1]でBOS発生してない場合はBOS候補の線（隣のBarまで延長）のみ引く
                line_HTF2_BOS_H1 := line.new(htf_candles_startingPos + 86, DH_2, htf_candles_startingPos + 95, DH_2, color=line_color_BOS, width=line_width_BOS)
                if DH_2<DC_
                    // [2]のBOS候補の線を[0]で抜いたらアラート設定
                    alert_HTF2_BOS_0 := true
                // BOS label
                if show_BOS_Labels
                    label.delete(label_D_BS_H)
                    label_D_BS_H := label.new(htf_candles_startingPos + 108, DH_2, text = "BOS: " + str.tostring(DH_2), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))
        if DH_4<DH_3 and DH_3>DH_2
            if DL_2>DC_1
                // [2]と[1]でBOS発生してたらBOS引いてアラート設定
                line_HTF2_BOS_L1 := line.new(htf_candles_startingPos + 86, DL_2, htf_candles_startingPos + 92, DL_2, color=line_color_BOS, width=line_width_BOS)
                alert_HTF2_BOS_1 := true
                // BOS label
                if show_BOS_Labels
                    label.delete(label_D_BS_L)
                    label_D_BS_L := label.new(htf_candles_startingPos + 108, DL_2, text = "BOS: " + str.tostring(DL_2), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))
            else
                // [2]と[1]でBOS発生してない場合はBOS候補の線（隣のBarまで延長）のみ引く
                line_HTF2_BOS_L1 := line.new(htf_candles_startingPos + 86, DL_2, htf_candles_startingPos + 95, DL_2, color=line_color_BOS, width=line_width_BOS)
                if DL_2>DC_
                    // [2]のBOS候補の線を[0]で抜いたらアラート設定
                    alert_HTF2_BOS_0 := true
                // BOS label
                if show_BOS_Labels
                    label.delete(label_D_BS_L)
                    label_D_BS_L := label.new(htf_candles_startingPos + 108, DL_2, text = "BOS: " + str.tostring(DL_2), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))

        // [3][2][1]のローソクから判定する
        if DL_3>DL_2 and DL_2<DL_1
            line_HTF2_BOS_H0 := line.new(htf_candles_startingPos + 89, DH_1, htf_candles_startingPos + 95, DH_1, color=line_color_BOS, width=line_width_BOS)
            if DH_1<DC_
                // [1]のBOS候補の線を[0]で抜いたらアラート設定
                alert_HTF2_BOS_0 := true
            // BOS label
            if show_BOS_Labels
                label.delete(label_D_BS_H)
                label_D_BS_H := label.new(htf_candles_startingPos + 108, DH_1, text = "BOS: " + str.tostring(DH_1), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))
        if DH_3<DH_2 and DH_2>DH_1
            line_HTF2_BOS_L0 := line.new(htf_candles_startingPos + 89, DL_1, htf_candles_startingPos + 95, DL_1, color=line_color_BOS, width=line_width_BOS)
            if DL_1>DC_
                // [1]のBOS候補の線を[0]で抜いたらアラート設定
                alert_HTF2_BOS_0 := true
            // BOS label
            if show_BOS_Labels
                label.delete(label_D_BS_L)
                label_D_BS_L := label.new(htf_candles_startingPos + 108, DL_1, text = "BOS: " + str.tostring(DL_1), textcolor=line_color_BOS, style=label.style_label_left, color = color.new(chart_color_even, 90))

    if show_Labels
        label_D_O := label.new(htf_candles_startingPos + 108, DO_, text = "O : " + str.tostring(DO_), textcolor=(DO_<DC_?chart_color_up:DO_>DC_?chart_color_down:chart_color_even), style=label.style_label_left, color = color.new(chart_color_even, 70))
        label_D_H := label.new(htf_candles_startingPos + 93, DH_, text = "H : " + str.tostring(DH_), textcolor=(DO_<DC_?chart_color_up:DO_>DC_?chart_color_down:chart_color_even), style=label.style_label_left, color = color.new(chart_color_even, 70))
        label_D_L := label.new(htf_candles_startingPos + 108, DL_, text = "L : " + str.tostring(DL_), textcolor=(DO_<DC_?chart_color_up:DO_>DC_?chart_color_down:chart_color_even), style=label.style_label_left, color = color.new(chart_color_even, 70))
        label_D_C := label.new(htf_candles_startingPos + 93, DC_, text = "C : " + str.tostring(DC_), textcolor=(DO_<DC_?chart_color_up:DO_>DC_?chart_color_down:chart_color_even), style=label.style_label_left, color = color.new(chart_color_even, 70))
//        [line_D_H_L,line_D_H_L_1,line_D_H_L_2,line_D_H_L_3,line_D_H_L_4, line_D_O_C,line_D_O_C_1,line_D_O_C_2,line_D_O_C_3,line_D_O_C_4, label_D_O, label_D_H, label_D_L, label_D_C, label_D_LL, label_D_RT]

    iHighest = DH_
    if iHighest < DH_1
        iHighest := DH_1
    if iHighest < DH_2
        iHighest := DH_2
    if iHighest < DH_3
        iHighest := DH_3
    if iHighest < DH_4
        iHighest := DH_4

    iLowest = DL_
    if iLowest > DL_1
        iLowest := DL_1
    if iLowest > DL_2
        iLowest := DL_2
    if iLowest > DL_3
        iLowest := DL_3
    if iLowest > DL_4
        iLowest := DL_4

    if show_RemainTime
        label_D_RT := label.new(htf_candles_startingPos + 100, iHighest, text = convert_mSecToTime(time_close(TF2)-timenow), textcolor=(DO_<DC_?chart_color_up:DO_>DC_?chart_color_down:chart_color_even), style=label.style_label_right, color = na, size=size.large)
    label_D_LL := label.new(htf_candles_startingPos + 70, (DH_ + DL_)/2, text = "TF: " + str.tostring(TF2), textcolor=(DO_<DC_?chart_color_up:DO_>DC_?chart_color_down:chart_color_even), style=label.style_label_right, color = na, size=size.large)     

    if show_HLLines_2
        line.delete(line_HTF2_H)
        line.delete(line_HTF2_L)
        line_HTF2_H := line.new(htf_candles_startingPos, iHighest, htf_candles_startingPos+1, iHighest, extend=extend.both, color=lineColor_2, style=lineStyle_2, width=lineWidth_2)
        line_HTF2_L := line.new(htf_candles_startingPos, iLowest, htf_candles_startingPos+1, iLowest, extend=extend.both, color=lineColor_2, style=lineStyle_2, width=lineWidth_2)

alertcondition(alert_HTF1_BOS_0 , title='HTF1 detected BOS - Liquidity Sweep', message='Detected BOS Alert!! - Liquidity Sweep : Judged by the rightmore bar [HTF1]')
alertcondition(alert_HTF1_BOS_1 , title='HTF1 detected BOS - Candle Close', message='Detected BOS Alert!! - Candle Close : Judged by 2nd bar from the right [HTF1]')
alertcondition(alert_HTF2_BOS_0 , title='HTF2 detected BOS - Liquidity Sweep', message='Detected BOS Alert!! - Liquidity Sweep : Judged by the rightmore bar [HTF2]')
alertcondition(alert_HTF2_BOS_1 , title='HTF2 detected BOS - Candle Close', message='Detected BOS Alert!! - Candle Close : Judged by 2nd bar from the right [HTF2]')
