本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("PWHL/PDHL", shorttitle="PWHL/PDHL", overlay=true, max_lines_count=500, max_labels_count=500)
// ─── Type: one level (line + label + state) ───────────────
type Lvl
line ln
label lb
float price
bool isHigh
bool active
// ─── Inputs ───────────────────────────────────────────────
grpW = "Previous Week (PWH / PWL)"
grpWH = "PWH (High)"
grpWL = "PWL (Low)"
grpD = "Previous Day (PDH / PDL)"
grpDH = "PDH (High)"
grpDL = "PDL (Low)"
grpG = "General"
// Week
showW = input.bool(true, "Show PWH / PWL", group=grpW)
tfW = input.timeframe("W", "Reference timeframe", group=grpW, tooltip="W = week. If this timeframe is lower than the chart timeframe, the week part is disabled automatically.")
nbW = input.int(1, "Number of candles displayed", minval=1, maxval=50, group=grpW, tooltip="1 = last closed candle only")
breakW = input.string("Wick", "Break by", options=["Wick", "Close"], group=grpW, tooltip="Wick: the line stops as soon as price touches or goes beyond the level. Close: it stops only if a candle closes beyond the level.")
colorWH = input.color(color.black, "Color", group=grpWH)
styleWH = input.string("Dotted", "Line style", options=["Solid", "Dotted", "Dashed"], group=grpWH)
widthWH = input.int(2, "Width", minval=1, maxval=10, group=grpWH)
colorWL = input.color(color.black, "Color", group=grpWL)
styleWL = input.string("Dotted", "Line style", options=["Solid", "Dotted", "Dashed"], group=grpWL)
widthWL = input.int(2, "Width", minval=1, maxval=10, group=grpWL)
// Day
showD = input.bool(true, "Show PDH / PDL", group=grpD)
tfD = input.timeframe("D", "Reference timeframe", group=grpD, tooltip="D = day. If this timeframe is lower than the chart timeframe, the day part is disabled automatically.")
nbD = input.int(1, "Number of candles displayed", minval=1, maxval=50, group=grpD, tooltip="1 = last closed candle only")
breakD = input.string("Wick", "Break by", options=["Wick", "Close"], group=grpD, tooltip="Wick: the line stops as soon as price touches or goes beyond the level. Close: it stops only if a candle closes beyond the level.")
colorDH = input.color(color.black, "Color", group=grpDH)
styleDH = input.string("Dotted", "Line style", options=["Solid", "Dotted", "Dashed"], group=grpDH)
widthDH = input.int(2, "Width", minval=1, maxval=10, group=grpDH)
colorDL = input.color(color.black, "Color", group=grpDL)
styleDL = input.string("Dotted", "Line style", options=["Solid", "Dotted", "Dashed"], group=grpDL)
widthDL = input.int(2, "Width", minval=1, maxval=10, group=grpDL)
// General
showLabels = input.bool(true, "Show labels", group=grpG)
fontSize = input.string("Normal", "Font size", options=["Tiny", "Normal", "Large"], group=grpG)
// ─── Functions ────────────────────────────────────────────
f_style(s) =>
s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed
f_size(s) =>
s == "Tiny" ? size.tiny : s == "Large" ? size.large : size.normal
// Handles one set of levels (week or day). Each call keeps its own state.
f_levels(bool enabled, bool newHtf, int nb, string brk, color cH, string sH, int wH, color cL, string sL, int wL, string tH, string tL, bool showLb, string fs) =>
var float curH = na
var float curL = na
var int idxH = na
var int idxL = na
var array<Lvl> lvls = array.new<Lvl>()
if enabled
if bar_index == 0
curH := high
curL := low
idxH := bar_index
idxL := bar_index
else if newHtf
// The previous HTF candle just closed: draw its levels
if not na(curH)
lnH = line.new(idxH, curH, bar_index, curH, extend=extend.none, color=cH, style=f_style(sH), width=wH)
lnL = line.new(idxL, curL, bar_index, curL, extend=extend.none, color=cL, style=f_style(sL), width=wL)
// Labels on the right, at the end of the line: High above, Low below
lbH = showLb ? label.new(bar_index, curH, tH, style=label.style_label_lower_right, color=color.new(cH, 100), textcolor=cH, size=f_size(fs)) : na
lbL = showLb ? label.new(bar_index, curL, tL, style=label.style_label_upper_right, color=color.new(cL, 100), textcolor=cL, size=f_size(fs)) : na
array.push(lvls, Lvl.new(lnH, lbH, curH, true, true))
array.push(lvls, Lvl.new(lnL, lbL, curL, false, true))
// Keep only the last N candles
while array.size(lvls) > nb * 2
old = array.shift(lvls)
line.delete(old.ln)
label.delete(old.lb)
// New HTF candle: start over
curH := high
curL := low
idxH := bar_index
idxL := bar_index
else
// Same HTF candle: update the high/low and the bar where they formed
if high > curH
curH := high
idxH := bar_index
if low < curL
curL := low
idxL := bar_index
// The line and its label follow the open candle, and stop when price breaks the level
for lv in lvls
if lv.active
crossed = lv.isHigh ? (brk == "Wick" ? high > lv.price : close > lv.price) : (brk == "Wick" ? low < lv.price : close < lv.price)
line.set_x2(lv.ln, bar_index)
if not na(lv.lb)
label.set_x(lv.lb, bar_index)
if crossed
lv.active := false
// ─── New HTF candle detection ─────────────────────────────
okW = showW and timeframe.in_seconds(tfW) >= timeframe.in_seconds(timeframe.period)
okD = showD and timeframe.in_seconds(tfD) >= timeframe.in_seconds(timeframe.period)
timeW = time(tfW)
timeD = time(tfD)
newW = timeW != timeW[1]
newD = timeD != timeD[1]
// ─── Calls: one for the week, one for the day ─────────────
f_levels(okW, newW, nbW, breakW, colorWH, styleWH, widthWH, colorWL, styleWL, widthWL, "PWH", "PWL", showLabels, fontSize)
f_levels(okD, newD, nbD, breakD, colorDH, styleDH, widthDH, colorDL, styleDL, widthDL, "PDH", "PDL", showLabels, fontSize)