OHLC MTF
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © GhostTraderx1

//@version=5
indicator("Previous OHLC", "OHLC MTF", true)

group1 = "General Settings"
string tf = input.timeframe("D", "Time Frame", group=group1)
int offset = input.int(1, "Offset", group=group1)

group2 = "Display Settings"
bool showLast = input.bool(false, "Show last OHLC levels only", group=group2)
bool extendLines = input.bool(false, "Extend OHLC levels", group=group2)
color bgcolor = input.color(color.new(color.silver, 80), "Background fill color", group=group2)

group3 = "Previous Open Level"
string openStyle = input.string(line.style_dashed, "Style", options=[line.style_solid, line.style_dashed, line.style_dotted], group=group3)
color openColor = input.color(color.blue, "Previous High", group=group3)
int openWidth = input.int(1, "Thickness", group=group3)

group4 = "Previous High Level"
string highStyle = input.string(line.style_solid, "Style", options=[line.style_solid, line.style_dashed, line.style_dotted], group=group4)
color highColor = input.color(color.green, "Previous High", group=group4)
int highWidth = input.int(1, "Thickness", group=group4)

group5 = "Previous Low Level"
string lowStyle = input.string(line.style_solid, "Style", options=[line.style_solid, line.style_dashed, line.style_dotted], group=group5)
color lowColor = input.color(color.red, "Previous High", group=group5)
int lowWidth = input.int(1, "Thickness", group=group5)

group6 = "Previous Close Level"
string closeStyle = input.string(line.style_dashed, "Style", options=[line.style_solid, line.style_dashed, line.style_dotted], group=group6)
color closeColor = input.color(color.blue, "Previous High", group=group6)
int closeWidth = input.int(1, "Thickness", group=group6)

bool newSession = ta.change(time(tf))

op = request.security(syminfo.tickerid, tf, open[offset - 1])
hi = request.security(syminfo.tickerid, tf, high[offset - 1])
lo = request.security(syminfo.tickerid, tf, low[offset - 1])
cl = request.security(syminfo.tickerid, tf, close[offset - 1])

x = request.security(syminfo.tickerid, tf, time)

var line openLine = na
var line highLine = na
var line lowLine = na
var line closeLine = na

var linefill ohlc = na

displayLabels(x1, x2, x3, x4) =>
    openLabel = label.new(line.get_x2(x1), line.get_y2(x1), "Prev. Open", xloc.bar_time, color=openColor, style=label.style_label_left, textcolor=color.white, size=size.tiny)
    highLabel = label.new(line.get_x2(x2), line.get_y2(x2), "Prev. High", xloc.bar_time, color=highColor, style=label.style_label_left, textcolor=color.white, size=size.tiny)
    lowLabel = label.new(line.get_x2(x3), line.get_y2(x3), "Prev. Low", xloc.bar_time, color=lowColor, style=label.style_label_left, textcolor=color.white, size=size.tiny)
    closeLabel = label.new(line.get_x2(x4), line.get_y2(x4), "Prev. Close", xloc.bar_time, color=closeColor, style=label.style_label_left, textcolor=color.white, size=size.tiny)

    label.delete(openLabel[1])
    label.delete(highLabel[1])
    label.delete(lowLabel[1])
    label.delete(closeLabel[1])
    
displayLines(x1, x2) =>
    opLine = line.new(x1, op[1], x2, op[1], xloc.bar_time, color=openColor, style=openStyle, width=openWidth)
    hiLine = line.new(x1, hi[1], x2, hi[1], xloc.bar_time, color=highColor, style=highStyle, width=highWidth)
    loLine = line.new(x1, lo[1], x2, lo[1], xloc.bar_time, color=lowColor, style=lowStyle, width=lowWidth)
    clLine = line.new(x1, cl[1], x2, cl[1], xloc.bar_time, color=closeColor, style=closeStyle, width=closeWidth)
    
    line.delete(opLine[1])
    line.delete(hiLine[1])
    line.delete(loLine[1])
    line.delete(clLine[1])
    
    [opLine, hiLine, loLine, clLine]

if newSession and not showLast
    openLine := line.new(x[1], op, time, op, xloc.bar_time, color=openColor, style=openStyle, width=openWidth)
    highLine := line.new(x[1], hi, time, hi, xloc.bar_time, color=highColor, style=highStyle, width=highWidth)
    lowLine := line.new(x[1], lo, time, lo, xloc.bar_time, color=lowColor, style=lowStyle, width=lowWidth)
    closeLine := line.new(x[1], cl, time, cl, xloc.bar_time, color=closeColor, style=closeStyle, width=closeWidth)
    
    ohlc := linefill.new(highLine, lowLine, bgcolor)
    
    if not extendLines
        displayLabels(openLine, highLine, lowLine, closeLine)

if showLast
    [openLine2, highLine2, lowLine2, closeLine2] = displayLines(x[1], x)
    
    if not extendLines 
        displayLabels(openLine2, highLine2, lowLine2, closeLine2)
    
    linefill.new(highLine2, lowLine2, bgcolor)

if extendLines
    [openLine3, highLine3, lowLine3, closeLine3] = displayLines(x, time)
    displayLabels(openLine3, highLine3, lowLine3, closeLine3)
    
    linefill.new(highLine3, lowLine3, bgcolor)

    