本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
// Previous HTF Highs & Lows + Session Highs & Lows
// - PDH/PDL, PWH/PWL, previous 4H high/low: only the LAST completed period is drawn.
// - Asia / London / New York session high/low: the most recent session (optionally the running one).
// - Each line starts on the exact candle whose wick made the high/low and runs to the right.
// - Master switches for all HTF levels and all sessions, plus individual High/Low switches per level.
// - Optional: remove a level once it is mitigated (price trades or closes through it). Off by default.
// - Labels that sit close together are merged into one label (e.g. "4H L / LDN L").
// - Auto-hide: a level is hidden when the chart timeframe is HIGHER than the level's timeframe
// (Daily chart: PDH/PDL + PWH/PWL, no 4H. Weekly chart: PWH/PWL only). Sessions show below 4H.
// - Sessions are set in each market's own local time (Tokyo / London / New York), so summer/winter time
// is handled automatically whatever timezone your chart uses.
// Day/week boundaries follow the symbol's trading session (same as TradingView's D and W candles).
indicator("Previous HTF & Session Highs/Lows", "Prev HTF/Session H/L", overlay = true, max_lines_count = 40, max_labels_count = 40)
// ───────── Higher-timeframe levels
gHTF = "Higher-timeframe levels"
showHTF = input.bool(true, "Show all higher-timeframe levels", group = gHTF)
g4 = "Previous 4H high / low"
show4H = input.bool(true, "High", group = g4, inline = "v4")
show4L = input.bool(true, "Low", group = g4, inline = "v4")
c4H = input.color(color.black, "High colour", group = g4, inline = "c4")
c4L = input.color(color.black, "Low colour", group = g4, inline = "c4")
w4 = input.int(1, "Width", minval = 1, maxval = 5, group = g4, inline = "s4")
s4 = input.string("Dotted", "Style", options = ["Solid", "Dotted", "Dashed"], group = g4, inline = "s4")
gD = "Previous day high / low (PDH / PDL)"
showDH = input.bool(true, "High", group = gD, inline = "vD")
showDL = input.bool(true, "Low", group = gD, inline = "vD")
cDH = input.color(color.black, "High colour", group = gD, inline = "cD")
cDL = input.color(color.black, "Low colour", group = gD, inline = "cD")
wD = input.int(1, "Width", minval = 1, maxval = 5, group = gD, inline = "sD")
sD = input.string("Dotted", "Style", options = ["Solid", "Dotted", "Dashed"], group = gD, inline = "sD")
gW = "Previous week high / low (PWH / PWL)"
showWH = input.bool(true, "High", group = gW, inline = "vW")
showWL = input.bool(true, "Low", group = gW, inline = "vW")
cWH = input.color(color.black, "High colour", group = gW, inline = "cW")
cWL = input.color(color.black, "Low colour", group = gW, inline = "cW")
wW = input.int(1, "Width", minval = 1, maxval = 5, group = gW, inline = "sW")
sW = input.string("Dotted", "Style", options = ["Solid", "Dotted", "Dashed"], group = gW, inline = "sW")
// ───────── Sessions
gS = "Sessions"
showSess = input.bool(true, "Show all session highs / lows", group = gS)
liveSess = input.bool(true, "Show the running session's developing high / low", group = gS)
gA = "Asia session high / low"
showAH = input.bool(true, "High", group = gA, inline = "vA")
showAL = input.bool(true, "Low", group = gA, inline = "vA")
sessA = input.session("0900-1500", "Session", group = gA, inline = "tA")
tzA = input.string("Asia/Tokyo", "", options = ["Asia/Tokyo", "Asia/Singapore", "Asia/Hong_Kong", "Australia/Sydney", "Europe/London", "Europe/Berlin", "America/New_York", "America/Chicago", "Asia/Karachi", "UTC"], group = gA, inline = "tA")
cAH = input.color(color.black, "High colour", group = gA, inline = "cA")
cAL = input.color(color.black, "Low colour", group = gA, inline = "cA")
wA = input.int(1, "Width", minval = 1, maxval = 5, group = gA, inline = "sA")
sA = input.string("Dotted", "Style", options = ["Solid", "Dotted", "Dashed"], group = gA, inline = "sA")
gL = "London session high / low"
showLH = input.bool(true, "High", group = gL, inline = "vL")
showLL = input.bool(true, "Low", group = gL, inline = "vL")
sessL = input.session("0800-1600", "Session", group = gL, inline = "tL")
tzL = input.string("Europe/London", "", options = ["Asia/Tokyo", "Asia/Singapore", "Asia/Hong_Kong", "Australia/Sydney", "Europe/London", "Europe/Berlin", "America/New_York", "America/Chicago", "Asia/Karachi", "UTC"], group = gL, inline = "tL")
cLH = input.color(color.black, "High colour", group = gL, inline = "cL")
cLL = input.color(color.black, "Low colour", group = gL, inline = "cL")
wL = input.int(1, "Width", minval = 1, maxval = 5, group = gL, inline = "sL")
sL = input.string("Dotted", "Style", options = ["Solid", "Dotted", "Dashed"], group = gL, inline = "sL")
gN = "New York session high / low"
showNH = input.bool(true, "High", group = gN, inline = "vN")
showNL = input.bool(true, "Low", group = gN, inline = "vN")
sessN = input.session("0800-1700", "Session", group = gN, inline = "tN")
tzN = input.string("America/New_York", "", options = ["Asia/Tokyo", "Asia/Singapore", "Asia/Hong_Kong", "Australia/Sydney", "Europe/London", "Europe/Berlin", "America/New_York", "America/Chicago", "Asia/Karachi", "UTC"], group = gN, inline = "tN")
cNH = input.color(color.black, "High colour", group = gN, inline = "cN")
cNL = input.color(color.black, "Low colour", group = gN, inline = "cN")
wN = input.int(1, "Width", minval = 1, maxval = 5, group = gN, inline = "sN")
sN = input.string("Dotted", "Style", options = ["Solid", "Dotted", "Dashed"], group = gN, inline = "sN")
// ───────── Mitigation
gM = "Mitigation"
hideMit = input.bool(false, "Remove a level once it is mitigated", group = gM)
mitBy = input.string("Wick", "Mitigated when price", options = ["Wick", "Close"], group = gM, tooltip = "Wick: any candle trades through the level. Close: a candle closes beyond it.")
// ───────── General
gG = "General"
autoHide = input.bool(true, "Hide levels below the chart timeframe (no 4H on Daily, no Daily on Weekly)", group = gG)
extRight = input.bool(false, "Extend lines to the right edge", group = gG)
extBars = input.int(10, "Otherwise: bars past the last candle", minval = 0, maxval = 200, group = gG)
showLbl = input.bool(true, "Labels", group = gG, inline = "lb")
showPx = input.bool(false, "Price in label", group = gG, inline = "lb")
lblSize = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = gG)
mergeAtr = input.float(0.5, "Merge labels closer than (x ATR)", minval = 0.0, step = 0.05, group = gG, tooltip = "Levels this close share one label, e.g. 4H L / LDN L. 0 = never merge.")
// ───────── helpers
f_style(string s) =>
s == "Dotted" ? line.style_dotted : s == "Dashed" ? line.style_dashed : line.style_solid
f_size(string s) =>
s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : size.small
f_allowed(string tf) =>
not autoHide or timeframe.in_seconds() <= timeframe.in_seconds(tf)
// price that counts as "through" the level
upX = mitBy == "Close" ? close : high
dnX = mitBy == "Close" ? close : low
// Higher-timeframe tracker: running high/low of each period with the wick candle's time.
// When a new period starts the finished one becomes "previous"; from then on we watch for mitigation.
f_track(string tf) =>
var float curH = na
var float curL = na
var int curHt = na
var int curLt = na
var float pH = na
var float pL = na
var int pHt = na
var int pLt = na
var bool mH = false
var bool mL = false
if timeframe.change(tf)
pH := curH
pL := curL
pHt := curHt
pLt := curLt
mH := false
mL := false
curH := high
curL := low
curHt := time
curLt := time
else
if na(curH) or high > curH
curH := high
curHt := time
if na(curL) or low < curL
curL := low
curLt := time
if not na(pH) and upX > pH
mH := true
if not na(pL) and dnX < pL
mL := true
[pH, pHt, pL, pLt, mH, mL]
[t4H, t4Ht, t4L, t4Lt, m4H, m4L] = f_track("240")
[tDH, tDHt, tDL, tDLt, mDH, mDL] = f_track("D")
[tWH, tWHt, tWL, tWLt, mWH, mWL] = f_track("W")
// Reference values from the real higher-timeframe candle (last closed one, no repainting).
[r4H, r4L] = request.security(syminfo.tickerid, "240", [high[1], low[1]], lookahead = barmerge.lookahead_on)
[rDH, rDL] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead = barmerge.lookahead_on)
[rWH, rWL] = request.security(syminfo.tickerid, "W", [high[1], low[1]], lookahead = barmerge.lookahead_on)
var int firstTime = time
// Session tracker. Live = the running session's developing high/low (never counted as mitigated).
// Completed = the last finished session, watched for mitigation from the bar after it ended.
f_session(string sess, string tz) =>
bool inS = not na(time(timeframe.period, sess, tz))
var float curH = na
var float curL = na
var int curHt = na
var int curLt = na
var float pH = na
var float pL = na
var int pHt = na
var int pLt = na
var bool mH = false
var bool mL = false
if inS and not inS[1]
curH := high
curL := low
curHt := time
curLt := time
else if inS
if high > curH
curH := high
curHt := time
if low < curL
curL := low
curLt := time
if not inS and inS[1] and not na(curH)
pH := curH
pL := curL
pHt := curHt
pLt := curLt
mH := false
mL := false
if not na(pH) and upX > pH
mH := true
if not na(pL) and dnX < pL
mL := true
bool live = inS and liveSess and not na(curH)
[live ? curH : pH, live ? curHt : pHt, live ? curL : pL, live ? curLt : pLt, live ? false : mH, live ? false : mL]
[aH, aHt, aL, aLt, mAH, mAL] = f_session(sessA, tzA)
[lH, lHt, lL, lLt, mLH, mLL] = f_session(sessL, tzL)
[nH, nHt, nL, nLt, mNH, mNL] = f_session(sessN, tzN)
sessAllowed = not autoHide or timeframe.in_seconds() < timeframe.in_seconds("240")
// If the chart does not hold the whole previous period (very low timeframes), use the HTF candle's value
// and start the line at the oldest loaded candle.
f_pickHigh(float tracked, int t, float ref) =>
bool partial = na(tracked) or (not na(ref) and tracked < ref - syminfo.mintick)
[partial ? ref : tracked, partial ? firstTime : t]
f_pickLow(float tracked, int t, float ref) =>
bool partial = na(tracked) or (not na(ref) and tracked > ref + syminfo.mintick)
[partial ? ref : tracked, partial ? firstTime : t]
atrNow = ta.atr(14)
// ───────── collect the levels, then draw lines and merged labels
var array<line> lines = array.new<line>()
var array<label> labels = array.new<label>()
var array<float> lvY = array.new<float>()
var array<int> lvT = array.new<int>()
var array<color> lvC = array.new<color>()
var array<int> lvW = array.new<int>()
var array<string> lvS = array.new<string>()
var array<string> lvN = array.new<string>()
f_add(bool on, bool mitigated, float y, int t, color c, int w, string st, string name) =>
if on and not na(y) and not na(t) and not (hideMit and mitigated)
array.push(lvY, y)
array.push(lvT, t)
array.push(lvC, c)
array.push(lvW, w)
array.push(lvS, st)
array.push(lvN, name)
f_label(int x, float y, string txt, color col) =>
string t = showPx ? txt + " " + str.tostring(y, format.mintick) : txt
array.push(labels, label.new(x, y, t, xloc = xloc.bar_time, style = label.style_label_left, color = color.new(color.black, 100), textcolor = col, size = f_size(lblSize)))
if barstate.islast
for ln in lines
line.delete(ln)
for lb in labels
label.delete(lb)
array.clear(lines)
array.clear(labels)
array.clear(lvY)
array.clear(lvT)
array.clear(lvC)
array.clear(lvW)
array.clear(lvS)
array.clear(lvN)
if showHTF and f_allowed("W")
[yH, xH] = f_pickHigh(tWH, tWHt, rWH)
[yL, xL] = f_pickLow(tWL, tWLt, rWL)
f_add(showWH, mWH, yH, xH, cWH, wW, sW, "PWH")
f_add(showWL, mWL, yL, xL, cWL, wW, sW, "PWL")
if showHTF and f_allowed("D")
[yH, xH] = f_pickHigh(tDH, tDHt, rDH)
[yL, xL] = f_pickLow(tDL, tDLt, rDL)
f_add(showDH, mDH, yH, xH, cDH, wD, sD, "PDH")
f_add(showDL, mDL, yL, xL, cDL, wD, sD, "PDL")
if showHTF and f_allowed("240")
[yH, xH] = f_pickHigh(t4H, t4Ht, r4H)
[yL, xL] = f_pickLow(t4L, t4Lt, r4L)
f_add(show4H, m4H, yH, xH, c4H, w4, s4, "4H H")
f_add(show4L, m4L, yL, xL, c4L, w4, s4, "4H L")
if showSess and sessAllowed
f_add(showAH, mAH, aH, aHt, cAH, wA, sA, "Asia H")
f_add(showAL, mAL, aL, aLt, cAL, wA, sA, "Asia L")
f_add(showLH, mLH, lH, lHt, cLH, wL, sL, "LDN H")
f_add(showLL, mLL, lL, lLt, cLL, wL, sL, "LDN L")
f_add(showNH, mNH, nH, nHt, cNH, wN, sN, "NY H")
f_add(showNL, mNL, nL, nLt, cNL, wN, sN, "NY L")
int xEnd = time + extBars * timeframe.in_seconds() * 1000
int n = array.size(lvY)
if n > 0
for i = 0 to n - 1
float y = array.get(lvY, i)
array.push(lines, line.new(array.get(lvT, i), y, xEnd, y, xloc = xloc.bar_time, color = array.get(lvC, i), width = array.get(lvW, i), style = f_style(array.get(lvS, i)), extend = extRight ? extend.right : extend.none))
// labels: walk the levels from lowest to highest price and merge the ones closer than the tolerance
if showLbl and n > 0
array<int> idx = array.sort_indices(lvY, order.ascending)
float tol = mergeAtr * atrNow
string txt = ""
float ySum = 0.0
int cnt = 0
color col = na
float yPrev = na
for k = 0 to n - 1
int i = array.get(idx, k)
float y = array.get(lvY, i)
if cnt > 0 and (tol <= 0 or y - yPrev > tol)
f_label(xEnd, ySum / cnt, txt, col)
txt := ""
ySum := 0.0
cnt := 0
txt := cnt == 0 ? array.get(lvN, i) : txt + " / " + array.get(lvN, i)
col := cnt == 0 ? array.get(lvC, i) : col
ySum += y
cnt += 1
yPrev := y
if cnt > 0
f_label(xEnd, ySum / cnt, txt, col)