Higher Time Frame {HTF} Candles [QuantVue]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantVue

//@version=5
indicator("Higher Time Frame {HTF} Candles [QuantVue]", overlay = true)

htf = input.timeframe('D', 'Higher Time Frame', tooltip = 'Must be greater than timeframe of current chart.')
showPrev = input.bool(true, 'Show Previous 5 Higher Time Frame Bars')
showWarn = input.bool(true, 'Display Warning Label if Current TF >= HTF')

var box body = na
var box upperWick = na
var box lowerWick = na
var startIndex = 0
var prevStarts = array.new<int>(6)

newTime = ta.change(time(htf))
[ohtf, hhtf, lhtf, chtf] = request.security(syminfo.tickerid, htf, [open, high, low, close])
bullish = chtf > ohtf

if newTime
    startIndex := bar_index
    if prevStarts.size() > 6
        prevStarts.pop()
        prevStarts.unshift(startIndex)
    else
        prevStarts.unshift(startIndex)

if newTime or barstate.islast
    (body[1]).delete(), (upperWick[1]).delete(), (lowerWick[1]).delete()
    boxColor = bullish ? color.green : color.red
    body := box.new(startIndex, bullish ? chtf : ohtf, bar_index, bullish ? ohtf : chtf, color.new(boxColor,50), bgcolor = color.new(boxColor,50))
    upperWick := box.new(startIndex, hhtf, bar_index, bullish ? chtf : ohtf, color.new(boxColor,90), bgcolor = color.new(boxColor,90))
    lowerWick := box.new(startIndex, bullish ? ohtf : chtf, bar_index, lhtf, color.new(boxColor,90), bgcolor = color.new(boxColor,90))

[ohtf1, hhtf1, lhtf1, chtf1] = request.security(syminfo.tickerid, htf, [open[1], high[1], low[1], close[1]])
[ohtf2, hhtf2, lhtf2, chtf2] = request.security(syminfo.tickerid, htf, [open[2], high[2], low[2], close[2]])
[ohtf3, hhtf3, lhtf3, chtf3] = request.security(syminfo.tickerid, htf, [open[3], high[3], low[3], close[3]])
[ohtf4, hhtf4, lhtf4, chtf4] = request.security(syminfo.tickerid, htf, [open[4], high[4], low[4], close[4]])
[ohtf5, hhtf5, lhtf5, chtf5] = request.security(syminfo.tickerid, htf, [open[5], high[5], low[5], close[5]])

prevOpens = array.from(ohtf1, ohtf2, ohtf3, ohtf4, ohtf5)
prevHighs = array.from(hhtf1, hhtf2, hhtf3, hhtf4, hhtf5)
prevLows = array.from(lhtf1, lhtf2, lhtf3, lhtf4, lhtf5)
prevCloses = array.from(chtf1, chtf2, chtf3, chtf4, chtf5)
oldStart = ta.valuewhen(newTime, bar_index, 0)

if barstate.islast and showPrev
    for i = 0 to 4
        bullBar = prevOpens.get(i) < prevCloses.get(i)
        box.new(prevStarts.get(i+1), bullBar? prevCloses.get(i) : prevOpens.get(i), prevStarts.get(i) - 1, bullBar ? prevOpens.get(i) : prevCloses.get(i), bullBar ? color.new(color.green,50) : color.new(color.red,50), bgcolor = bullBar ? color.new(color.green,50) : color.new(color.red,50))
        box.new(prevStarts.get(i+1), prevHighs.get(i), prevStarts.get(i) - 1, bullBar ? prevCloses.get(i) : prevOpens.get(i), bullBar ? color.new(color.green,90) : color.new(color.red,90), bgcolor = bullBar ? color.new(color.green,90) : color.new(color.red,90))
        box.new(prevStarts.get(i+1), prevLows.get(i), prevStarts.get(i) - 1, bullBar ? prevOpens.get(i) : prevCloses.get(i), bullBar ? color.new(color.green,90) : color.new(color.red,90), bgcolor = bullBar ? color.new(color.green,90) : color.new(color.red,90))

if barstate.islast and timeframe.in_seconds() >= timeframe.in_seconds(htf) and showWarn
    label.new(bar_index, high, 'Please select a higher time frame from the indicator settings\nor change current the chart to lower time frame to display\nhigher time frame candles.', color=color.aqua, textcolor = color.white, textalign = text.align_left)