Majors HOD/LOD
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Level8Trading

//@version=5
indicator("Majors HOD/LOD", overlay = true)

show_labels = input.bool(false, title = "Show Labels")

timeSpan = time(timeframe.period, "0930-1600:1234567", "America/New_York")
timeStart = time(timeframe.period, "0930-0931:1234567", "America/New_York")
timeEnd = time(timeframe.period, "1559-1600:1234567", "America/New_York")

var float qqqHod = 0.0
var float qqqLod = 0.0
qqqHigh = request.security("NASDAQ:QQQ", timeframe.period, high)
qqqCurrent = request.security("NASDAQ:QQQ", timeframe.period, close)
qqqLow = request.security("NASDAQ:QQQ", timeframe.period, low)

var float xlfHod = 0.0
var float xlfLod = 0.0
xlfHigh = request.security("AMEX:XLF", timeframe.period, high)
xlfCurrent = request.security("AMEX:XLF", timeframe.period, close)
xlfLow = request.security("AMEX:XLF", timeframe.period, low)

var float xlvHod = 0.0
var float xlvLod = 0.0
xlvHigh = request.security("AMEX:XLV", timeframe.period, high)
xlvCurrent = request.security("AMEX:XLV", timeframe.period, close)
xlvLow = request.security("AMEX:XLV", timeframe.period, low)


if timeframe.isintraday 
    isNewDay = timeStart and not timeEnd

    if isNewDay
        qqqHod := qqqHigh
        qqqLod := qqqLow
        xlfHod := xlfHigh
        xlfLod := xlfLow
        xlvHod := xlvHigh
        xlvLod := xlvLow

    bool qqqAtHigh = qqqCurrent > qqqHod ? true : false
    bool qqqAtLow = qqqCurrent < qqqLod ? true : false
    qqqHod := qqqHigh > qqqHod ? qqqHigh : qqqHod
    qqqLod := qqqLow < qqqLod ? qqqLow : qqqLod

    bool xlfAtHigh = xlfCurrent > xlfHod ? true : false
    bool xlfAtLow = xlfCurrent < xlfLod ? true : false
    xlfHod := xlfHigh > xlfHod ? xlfHigh : xlfHod
    xlfLod := xlfLow < xlfLod ? xlfLow : xlfLod

    bool xlvAtHigh = xlvCurrent > xlvHod ? true : false
    bool xlvAtLow = xlvCurrent < xlvLod ? true : false
    xlvHod := xlvHigh > xlvHod ? xlvHigh : xlvHod
    xlvLod := xlvLow < xlvLod ? xlvLow : xlvLod

    bool allAtHigh = false
    if qqqAtHigh and xlfAtHigh and xlvAtHigh
        if show_labels
            label.new(bar_index, high, yloc = yloc.abovebar, size = size.tiny, color = color.new(color.teal, 50), style = label.style_label_down)
        allAtHigh := true

    bool allAtLow = false
    if qqqAtLow and xlfAtLow and xlvAtLow
        if show_labels
            label.new(bar_index, low, yloc = yloc.belowbar, size = size.tiny, color = color.new(color.red, 50), style = label.style_label_up)
        allAtLow := true

    displayTable = table.new(position.top_right, 1, 2, bgcolor = color(na))
    table.cell(displayTable, 0, 0, text = " ")

    if barstate.islast  
        table.cell(displayTable, 0, 1, text = allAtHigh ? "Majors @ HOD 🡅" : allAtLow ? "Majors @ LOD 🡇" : "Mixed 🡅🡇", bgcolor = allAtHigh ? color.new(color.teal, 50) : allAtLow ? color.new(color.red, 50) : color.new(color.blue, 50), text_color = color.white)

        if allAtHigh
            alert("Majors @ HOD")

        if allAtLow
            alert("Majors @ LOD")