Previous OHLC Levels [TradeMaster Lite]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Copyright (c) 2023 trademasterindicator. All rights reserved.
// The Pinescript source code ("Previous OHLC Levels [TradeMaster Lite]") is exclusively licensed to trademasterindicator TradingView account. No other use is permitted without written consent from trademasterindicator.
// By accessing or using the script, you agree to the following terms:
// Grant of License: You are granted a non-transferable license to use the script for personal, non-commercial purposes related to technical analysis and trading on TradingView.
// Ownership and Intellectual Property: trademasterindicator retains all ownership and intellectual property rights to the script. You may not modify, reverse engineer, or distribute the script.
// Disclaimer of Warranty: The source code is provided as-is, without warranties. trademasterindicator shall not be liable for any damages arising from its use.
// Termination: This license is valid until terminated.
//@version=5 

indicator('Previous OHLC Levels [TradeMaster Lite]', 'Previous OHLC [TradeMaster Lite]', overlay = true)

i_showPlot    = input.bool     (true       , 'Historical data'       , group = 'calculation', inline = 'display', display = display.none)
i_showCurrent = input.bool     (true       , 'Current data'          , group = 'calculation', inline = 'display', display = display.none)
i_pOHLC_tf    = input.timeframe('W'        , ''                      , group = 'calculation', inline = 'display', display = display.all , tooltip = "These levels can provide critical information about the previous trading period, whether it's a day, hour, week, or any other timeframe depending on your setting.\n\n● Previous Open: shows the opening price of the previous period. It's the price at which the market first started trading in that period.\n● Previous High: represents the highest price reached during the previous period. It can act as a resistance level for the current period.\n● Previous Low: indicates the lowest price hit during the previous period. It can serve as a support level in the current period.\n● Previous Close: the last price at which the asset traded during the previous period. It's often considered the most accurate reflection of the market sentiment at the end of that period.\n\nThese values provide a summary of the previous trading period's price action, giving you a baseline for comparing current price movements. They can help in understanding the market's direction and identifying potential support and resistance levels.")
i_offsetRight = input.int      (20         , 'Offset drawings right' , group = 'style'                          , display = display.none, tooltip = 'This setting lets you align drawings slightly to the right of their original position. It can be useful to prevent overlapping with price action or other indicators on the chart.')
i_textSize    = input.string   (size.small , 'Text size on the Chart', group = 'style'                          , display = display.none, options = [size.tiny, size.small, size.normal, size.large, size.huge])
i_clrPOHLC    = input.color    (#3bd8ffb3, 'Color'                 , group = 'style')

get_pOHLC(tf) => request.security(syminfo.tickerid, tf, array.from(high[1], low[1], open[1], close[1]), barmerge.gaps_off, barmerge.lookahead_on)
convertTimeframe(string tf) =>
    var tf_int = str.tonumber(tf)
    tf_int % 60 == 0 ? str.format("{0}h", tf_int / 60) : str.format("{0}{1}", tf, na(tf_int) or str.endswith(tf, 'D') or str.endswith(tf, 'W') or str.endswith(tf, 'M') ? '' : 'm')

var pOHLCs = array.new<float>(4)
var lines  = array.new<line>()
var labels = array.new<label>()
txts       = array.from('high', 'low', 'open', 'close')
pOHLCs    := timeframe.change(i_pOHLC_tf) or barstate.isfirst ? get_pOHLC(i_pOHLC_tf) : pOHLCs

if i_showCurrent
    for [i,e] in pOHLCs
        if barstate.isfirst
            lines .push(line.new (na,na,na,na, xloc.bar_time, extend.none, i_clrPOHLC, i < 2 ? line.style_dotted : line.style_solid))
            labels.push(label.new(na,na,na   , xloc.bar_time, yloc.price , #ffffff00, label.style_label_left, i_clrPOHLC, i_textSize, text_font_family = font.family_monospace))

        if barstate.islast
            ln  = lines .get(i)
            lbl = labels.get(i)
            ln.set_xy1     (time, e)
            ln.set_xy2     (time + i_offsetRight * timeframe.in_seconds() * 1000, e)
            lbl.set_xy     (time + i_offsetRight * timeframe.in_seconds() * 1000, e)
            lbl.set_text   (str.format('prev.{0} {1}', i_pOHLC_tf, txts.get(i)))
            lbl.set_tooltip(str.tostring(e))

plot_high  = plot(i_showPlot ? pOHLCs.get(0) : na, 'prev. high' , na)
plot_low   = plot(i_showPlot ? pOHLCs.get(1) : na, 'prev. low'  , na)
plot_open  = plot(i_showPlot ? pOHLCs.get(2) : na, 'prev. open' , na)
plot_close = plot(i_showPlot ? pOHLCs.get(3) : na, 'prev. close', na)

fill(plot_high, plot_low  , pOHLCs.get(0) != pOHLCs.get(0)[1] ? na : color.new(i_clrPOHLC,90))
fill(plot_open, plot_close, pOHLCs.get(2) != pOHLCs.get(2)[1] ? na : color.new(i_clrPOHLC,80))