HDBhagat multi time frame box analysis
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HDBhagat

//@version=5
indicator("HDBhagat multi time frame", overlay = true,max_boxes_count=5000)

timeframeInput = input.timeframe("1W")

type bar
    float o = open
    float h = high
    float l = low
    float c = close
    int   t = time

drawBox(bar b, right) =>
    bar s = bar.new()
    color boxColor = b.c >= b.o ? color.rgb(82, 61, 220) : color.rgb(247, 63, 63)
    box.new(b.t, b.h, right, b.l, boxColor, xloc = xloc.bar_time,border_style=line.style_dashed,border_width=2, bgcolor = color.new(boxColor, 99))

updateBox(box boxId, bar b) =>
    color boxColor = b.c >= b.o ? color.rgb(72, 54, 239, 72) : color.rgb(245, 31, 31, 77)
    box.set_border_color(boxId, boxColor)
    box.set_bgcolor(boxId, color.new(boxColor, 99))
    box.set_top(boxId, b.h)
    box.set_bottom(boxId, b.l)
    box.set_right(boxId, time)

secBar = request.security(syminfo.tickerid, timeframeInput, bar.new())

if not na(secBar)
    // To avoid a runtime error, only process data when an object exists.
    if not barstate.islast
        if timeframe.change(timeframeInput)
            // On historical bars, draw a new box in the past when the HTF closes.
            drawBox(secBar, time[1])
    else
        var box lastBox = na
        if na(lastBox) or timeframe.change(timeframeInput)
            // On the last bar, only draw a new current box the first time we get there or when HTF changes.
            lastBox := drawBox(secBar, time)
        else
            // On other chart updates, use setters to modify the current box.
            updateBox(lastBox, secBar)



//@version=5
//indicator("Multi Time Period Chart", overlay = true)

timeframeInput2 = input.timeframe("1D")

type barb
    float ow = open
    float hw = high
    float lw = low
    float cw = close
    int   tw = time

drawBox2(barb b2, right2) =>
    barb s2 = barb.new()
    color boxColor2 = b2.cw >= b2.ow ? color.rgb(94, 101, 249) : color.rgb(244, 96, 96)
    box.new(b2.tw, b2.hw, right2, b2.lw, boxColor2, xloc = xloc.bar_time,border_style=line.style_dotted,border_width=1, bgcolor = color.new(boxColor2, 99))

updateBox2(box boxId2, barb b2) =>
    color boxColor2 = b2.cw >= b2.ow ? color.rgb(76, 109, 255, 67) : color.rgb(255, 72, 72, 54)
    box.set_border_color(boxId2, boxColor2)
    box.set_bgcolor(boxId2, color.new(boxColor2, 99))
    box.set_top(boxId2, b2.hw)
    box.set_bottom(boxId2, b2.lw)
    box.set_right(boxId2, time)

secBar2 = request.security(syminfo.tickerid, timeframeInput2, barb.new())

if not na(secBar2)
    // To avoid a runtime error, only process data when an object exists.
    if not barstate.islast
        if timeframe.change(timeframeInput2)
            // On historical bars, draw a new box in the past when the HTF closes.
            drawBox2(secBar2, time[1])
    else
        var box lastBox2 = na
        if na(lastBox2) or timeframe.change(timeframeInput2)
            // On the last bar, only draw a new current box the first time we get there or when HTF changes.
            lastBox2 := drawBox2(secBar2, time)
        else
            // On other chart updates, use setters to modify the current box.
            updateBox2(lastBox2, secBar2)






//@version=5
//indicator("Multi Time Period Chart", overlay = true)

timeframeInput12 = input.timeframe("240")

type barb2
    float od = open
    float hd = high
    float ld = low
    float cd = close
    int   td = time

drawBox1(barb2 b12, right12) =>
    barb2 s12 = barb2.new()
    color boxColor12 = b12.cd >= b12.od ? color.rgb(131, 105, 210) : color.rgb(243, 79, 79)
    box.new(b12.td, b12.hd, right12, b12.ld, boxColor12, xloc = xloc.bar_time, border_style=line.style_solid,border_width=2, bgcolor = color.new(boxColor12, 99))

updateBox1(box boxId12, barb2 b12) =>
    color boxColor12 = b12.cd >= b12.od ? color.rgb(98, 98, 231, 70) : color.rgb(250, 34, 34, 72)
    box.set_border_color(boxId12, boxColor12)
    box.set_bgcolor(boxId12, color.new(boxColor12, 99))
    box.set_top(boxId12, b12.hd)
    box.set_bottom(boxId12, b12.ld)
    box.set_right(boxId12, time)

secBar12 = request.security(syminfo.tickerid, timeframeInput12, barb2.new())

if not na(secBar12)
    // To avoid a runtime error, only process data when an object exists.
    if not barstate.islast
        if timeframe.change(timeframeInput12)
            // On historical bars, draw a new box in the past when the HTF closes.
            drawBox1(secBar12, time[1])
    else
        var box lastBox12 = na
        if na(lastBox12) or timeframe.change(timeframeInput12)
            // On the last bar, only draw a new current box the first time we get there or when HTF changes.
            lastBox12 := drawBox1(secBar12, time)
        else
            // On other chart updates, use setters to modify the current box.
            updateBox1(lastBox12, secBar12)






//@version=5
//indicator("Multi Time Period Chart", overlay = true)

timeframeInput123 = input.timeframe("60")

type barb3
    float o4 = open
    float h4 = high
    float l4 = low
    float c4 = close
    int   t4 = time

drawBox3(barb3 b123, right123) =>
    barb3 s123 = barb3.new()
    color boxColor123 = b123.c4 >= b123.o4 ? color.rgb(88, 96, 255) : color.rgb(255, 48, 48)
    box.new(b123.t4, b123.h4, right123, b123.l4, boxColor123, xloc = xloc.bar_time,border_style=line.style_solid,border_width=1, bgcolor = color.new(boxColor123, 99))

updateBox3(box boxId123, barb3 b123) =>
    color boxColor123 = b123.c4 >= b123.o4 ? color.rgb(76, 101, 175) : color.rgb(250, 41, 41)
    box.set_border_color(boxId123, boxColor123)
    box.set_bgcolor(boxId123, color.new(boxColor123, 99))
    box.set_top(boxId123, b123.h4)
    box.set_bottom(boxId123, b123.l4)
    box.set_right(boxId123, time)

secBar123 = request.security(syminfo.tickerid, timeframeInput123, barb3.new())

if not na(secBar123)
    // To avoid a runtime error, only process data when an object exists.
    if not barstate.islast
        if timeframe.change(timeframeInput123)
            // On historical bars, draw a new box in the past when the HTF closes.
            drawBox3(secBar123, time[1])
    else
        var box lastBox123 = na
        if na(lastBox123) or timeframe.change(timeframeInput123)
            // On the last bar, only draw a new current box the first time we get there or when HTF changes.
            lastBox123 := drawBox3(secBar123, time)
        else
            // On other chart updates, use setters to modify the current box.
            updateBox3(lastBox123, secBar123)


