bar view
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nishantsaxena

//@version=5
indicator("bar view",overlay = true,max_boxes_count = 500, max_labels_count = 500, max_bars_back = 4000)
//Input options
lookbackInput1 = input.int(15, minval = 0, maxval = 60, title =  "bar grouping period", step = 5)
boxBorderSize = input.int(2, title="Box border size", minval=0)

upBoxColor      = input.color(color.new(color.green, 85), title="Up box colour")
upBorderColor   = input.color(color.green, title="Up border colour")
downBoxColor    = input.color(color.new(color.red, 85), title="Down box colour")
downBorderColor = input.color(color.red, title="Down border colour")

joinBoxes = input.bool(false, title="Join boxes")

// Create variables
var dayHighPrice = 0.0
var dayLowPrice  = 0.0
var prevDayClose = 0.0
var box dailyBox = na

// See if a new calendar day started on the intra-day time frame
newDayStart = (minute % lookbackInput1) == 0 and 
     timeframe.isintraday

// If a new day starts, set the high and low to that bar's data. Else
// during the day track the highest high and lowest low.
if newDayStart
    dayHighPrice := high
    dayLowPrice  := low
    prevDayClose := close[1]
else
    dayHighPrice := math.max(dayHighPrice, high)
    dayLowPrice  := math.min(dayLowPrice, low)

// When a new day start, create a new box for that day.
// Else, during the day, update that day's box.
if newDayStart
    dailyBox := box.new(left=bar_index, top=dayHighPrice,
         right=bar_index + 1, bottom=dayLowPrice,
         border_width=boxBorderSize)

    box.set_bgcolor(dailyBox, color.rgb(0,0,0,100))
    box.set_border_color(dailyBox, color.rgb(0,0,0,100))
    // If we don't want the boxes to join, the previous box shouldn't
    // end on the same bar as the new box starts.
    if not joinBoxes
        box.set_right(dailyBox[1], bar_index[1])
else
    box.set_top(dailyBox, dayHighPrice)
    box.set_rightbottom(dailyBox, right=bar_index + 1, bottom=dayLowPrice)

    // If the current bar closed higher than yesterday's close, make
    // the box green (and paint it red otherwise)
    if close > prevDayClose
        box.set_bgcolor(dailyBox, upBoxColor)
        box.set_border_color(dailyBox, upBorderColor)
    else
        box.set_bgcolor(dailyBox, downBoxColor)
        box.set_border_color(dailyBox, downBorderColor)