// SPDX-License-Identifier: MIT
// Copyright (c) 2026 WickAtlas. MIT-licensed; provided "AS IS", without warranty.
// Version 1.0.0 | Released 2026-08-13 | Platform: TradingView Pine Script v6
// Defaults: previous completed daily and weekly high, low and close. No orders or external access.
//@version=6
indicator("WickAtlas Previous Period Levels", shorttitle = "WA Previous D/W", overlay = true)

string groupDisplay = "Levels"
bool showDailyLevels = input.bool(true, "Show previous day", group = groupDisplay, tooltip = "Plots the high, low and close of the previous completed TradingView daily bar.")
bool showWeeklyLevels = input.bool(true, "Show previous week", group = groupDisplay, tooltip = "Plots the high, low and close of the previous completed TradingView weekly bar.")

if showDailyLevels and timeframe.in_seconds() > timeframe.in_seconds("1D")
    runtime.error("Previous daily levels require a daily or lower chart timeframe.")
if showWeeklyLevels and timeframe.in_seconds() > timeframe.in_seconds("1W")
    runtime.error("Previous weekly levels require a weekly or lower chart timeframe.")

// Offset every requested expression by one completed source bar and pair it with lookahead_on.
// This is TradingView's documented pattern for confirmed, non-repainting higher-timeframe values.
[previousDayHigh, previousDayLow, previousDayClose] = request.security(symbol = syminfo.tickerid, timeframe = "1D", expression = [high[1], low[1], close[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[previousWeekHigh, previousWeekLow, previousWeekClose] = request.security(symbol = syminfo.tickerid, timeframe = "1W", expression = [high[1], low[1], close[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)

plot(showDailyLevels ? previousDayHigh : na, "Previous day high", color = color.new(color.teal, 15), linewidth = 1, style = plot.style_stepline)
plot(showDailyLevels ? previousDayLow : na, "Previous day low", color = color.new(color.orange, 15), linewidth = 1, style = plot.style_stepline)
plot(showDailyLevels ? previousDayClose : na, "Previous day close", color = color.new(color.gray, 25), linewidth = 1, style = plot.style_stepline)
plot(showWeeklyLevels ? previousWeekHigh : na, "Previous week high", color = color.teal, linewidth = 2, style = plot.style_stepline)
plot(showWeeklyLevels ? previousWeekLow : na, "Previous week low", color = color.orange, linewidth = 2, style = plot.style_stepline)
plot(showWeeklyLevels ? previousWeekClose : na, "Previous week close", color = color.gray, linewidth = 1, style = plot.style_stepline)

// Period boundaries follow TradingView's data feed and exchange calendar, which can differ from an MT5 broker's D1/W1 boundaries.

