本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("HTF Time Lines (1m)", shorttitle="HTF Lines", overlay=true, max_lines_count=500, max_boxes_count=500)
// ─────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────
grp1 = "1H Lines"
show1h = input.bool(true, "Show 1H lines", group=grp1)
col1h = input.color(color.new(color.gray, 40), "Color", group=grp1, inline="1h")
width1h = input.int(1, "Width", minval=1, maxval=10, group=grp1, inline="1h")
style1h = input.string("Dotted", "Style", options=["Solid", "Dashed", "Dotted"], group=grp1)
grp4 = "4H Lines"
show4h = input.bool(true, "Show 4H lines", group=grp4)
col4h = input.color(color.new(color.orange, 10), "Color", group=grp4, inline="4h")
width4h = input.int(2, "Width", minval=1, maxval=10, group=grp4, inline="4h")
style4h = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group=grp4)
grpF = "Upcoming (future) lines"
showFuture = input.bool(true, "Show upcoming lines", group=grpF)
futureHours = input.int(12, "Hours ahead", minval=1, maxval=72, group=grpF)
ZONE_TIP = "Default height for symbols not listed below. High zone: from high downward. Low zone: from low upward."
MAP_TIP = "Overrides the zone height for a matching symbol (matched against syminfo.ticker, e.g. XAUUSD, EURUSD, NAS100). Symbols not listed use the default height."
PIP_TIP = "Auto-detect assumes 10×mintick for 3/5-digit FX quotes. Override here for other instruments."
KEEP_TIP = "On: a consumed zone is cut where it ends instead of being deleted."
DIST_TIP = "If two untouched zones of the same kind are closer than this (in Height Unit), only the farther one is kept. 0 = off."
UNTOUCHED_TIP = "When a new 1H / 4H zone forms and the previous one was never reached by price, it stays as an untouched zone until price reaches it."
grpZ = "Zones (1H & 4H & Untouched)"
showZ1 = input.bool(true, "Show 1H zones", group=grpZ)
showZ4 = input.bool(true, "Show 4H zones", group=grpZ)
showU = input.bool(true, "Show untouched zones", group=grpZ, tooltip=UNTOUCHED_TIP)
zoneHeight = input.float(2, "Zone height (default / fallback)", minval=0, group=grpZ, tooltip=ZONE_TIP)
zoneUnit = input.string("Pips", "Height Unit", options=["Pips", "Ticks", "Price"], group=grpZ)
zoneMap = input.text_area("XAUUSD:150\nEURUSD:0.75\nGBPUSD:0.75\nUSDCAD:0.75\nUSDJPY:1\nNAS100:75\nUSDCHF:0.75\nUS30:75\nAUDUSD:0.5\nNZDUSD:0.75\nBRENT:0.1", "Per-Symbol Height Overrides (SYMBOL:HEIGHT, one per line)", group=grpZ, tooltip=MAP_TIP)
customPip = input.float(0, "Custom Pip Size (0 = auto-detect)", minval=0, group=grpZ, tooltip=PIP_TIP)
z1Bg = input.color(color.new(color.purple, 80), "1H fill", group=grpZ, inline="z1")
z1Bd = input.color(color.new(color.purple, 20), "1H border", group=grpZ, inline="z1")
z4Bg = input.color(color.new(color.green, 80), "4H fill", group=grpZ, inline="z4")
z4Bd = input.color(color.new(color.green, 20), "4H border", group=grpZ, inline="z4")
uBg = input.color(color.new(color.blue, 80), "Untouched fill", group=grpZ, inline="zu")
uBd = input.color(color.new(color.blue, 20), "Untouched border", group=grpZ, inline="zu")
zWidth = input.int(1, "1H / 4H border width", minval=0, maxval=10, group=grpZ)
zStyle = input.string("Solid", "1H / 4H border style", options=["Solid", "Dashed", "Dotted"], group=grpZ)
uWidth = input.int(1, "Untouched border width", minval=0, maxval=10, group=grpZ)
uStyle = input.string("Solid", "Untouched border style", options=["Solid", "Dashed", "Dotted"], group=grpZ)
z1Keep = input.bool(false, "Keep consumed 1H zones (cut)", group=grpZ, tooltip=KEEP_TIP)
z4Keep = input.bool(false, "Keep consumed 4H zones (cut)", group=grpZ, tooltip=KEEP_TIP)
uKeep = input.bool(false, "Keep consumed untouched zones (cut)", group=grpZ, tooltip=KEEP_TIP)
uMax = input.int(20, "Max untouched zones", minval=1, maxval=200, group=grpZ)
uDist = input.float(10, "Min distance between zones", minval=0, group=grpZ, tooltip=DIST_TIP)
uBreakDist = input.float(0, "Delete untouched zones within (distance) when a zone breaks", minval=0, group=grpZ, tooltip="When a 1H/4H zone is consumed by price, untouched blue zones within this distance (in Height Unit) are deleted and become invalid. 0 = off.")
consumeMode = input.string("Touches zone", "Zone is consumed when price", options=["Touches zone", "Reaches level"], group=grpZ, tooltip="Touches zone: price enters the zone. Reaches level: price reaches the High / Low itself.")
// ─────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────
MS_1H = 60 * 60 * 1000
MS_4H = 4 * MS_1H
f_style(s) =>
s == "Solid" ? line.style_solid : s == "Dashed" ? line.style_dashed : line.style_dotted
f_vline(t, c, w, s) =>
line.new(t, close, t, close + syminfo.mintick, xloc=xloc.bar_time, extend=extend.both, color=c, width=w, style=f_style(s))
is1m = timeframe.isminutes and timeframe.multiplier == 1
t1h = time("60")
t4h = time("240")
new1h = ta.change(t1h) != 0
new4h = ta.change(t4h) != 0
// ─────────────────────────────────────────────────────────────
// Historical lines
// ─────────────────────────────────────────────────────────────
if is1m
if show4h and new4h
f_vline(t4h, col4h, width4h, style4h)
if show1h and new1h and not (show4h and t1h == t4h)
f_vline(t1h, col1h, width1h, style1h)
// ─────────────────────────────────────────────────────────────
// Upcoming lines (redrawn on the last bar)
// ─────────────────────────────────────────────────────────────
var array<line> futureLines = array.new<line>()
if is1m and barstate.islast
while array.size(futureLines) > 0
line.delete(array.pop(futureLines))
if showFuture
for k = 1 to futureHours
ft = t1h + k * MS_1H
is4hT = (ft - t4h) % MS_4H == 0
if is4hT and show4h
array.push(futureLines, f_vline(ft, col4h, width4h, style4h))
else if show1h
array.push(futureLines, f_vline(ft, col1h, width1h, style1h))
// ─────────────────────────────────────────────────────────────
// Previous 1H / 4H High / Low zones
// ─────────────────────────────────────────────────────────────
type Zone
box b
float trig
float lvl
bool isHigh
bool hit = false
f_newZone(left, lvl, isHigh, height, bg, bd, w, st) =>
top = isHigh ? lvl : lvl + height
bottom = isHigh ? lvl - height : lvl
trig = consumeMode == "Touches zone" ? (isHigh ? bottom : top) : lvl
b = box.new(left, top, left + MS_1H, bottom, xloc=xloc.bar_time, extend=extend.right, bgcolor=bg, border_color=bd, border_width=w, border_style=f_style(st))
Zone.new(b=b, trig=trig, lvl=lvl, isHigh=isHigh)
f_cut(Zone z, keep, cutT) =>
if keep
box.set_extend(z.b, extend.none)
box.set_right(z.b, cutT)
else
box.delete(z.b)
f_rollover(array<Zone> zones, array<Zone> untouched, keep, cutT) =>
while array.size(zones) > 0
z = array.shift(zones)
if z.hit or not showU
f_cut(z, keep, cutT)
else
box.set_bgcolor(z.b, uBg)
box.set_border_color(z.b, uBd)
box.set_border_width(z.b, uWidth)
box.set_border_style(z.b, f_style(uStyle))
array.push(untouched, z)
f_cleanUntouched(array<Zone> zones, dist, maxN) =>
if dist > 0 and array.size(zones) > 1
for i = array.size(zones) - 1 to 0
zi = array.get(zones, i)
nearer = false
for j = 0 to array.size(zones) - 1
zj = array.get(zones, j)
if j != i and zj.isHigh == zi.isHigh and math.abs(zi.lvl - zj.lvl) <= dist
if (zi.isHigh ? zi.lvl < zj.lvl : zi.lvl > zj.lvl) or (zi.lvl == zj.lvl and i > j)
nearer := true
if nearer
array.remove(zones, i)
box.delete(zi.b)
while array.size(zones) > maxN
z = array.shift(zones)
box.delete(z.b)
f_invalidateUntouched(array<Zone> untouched, float refLvl, float dist) =>
if array.size(untouched) > 0
for i = array.size(untouched) - 1 to 0
z = array.get(untouched, i)
if math.abs(z.lvl - refLvl) <= dist
array.remove(untouched, i)
f_cut(z, uKeep, time_close)
var array<float> brokenLvls = array.new<float>()
f_checkHits(array<Zone> zones, removeOnHit, keep) =>
if array.size(zones) > 0
for i = array.size(zones) - 1 to 0
z = array.get(zones, i)
touched = z.isHigh ? high >= z.trig : low <= z.trig
if touched and not z.hit
z.hit := true
array.push(brokenLvls, z.lvl)
if removeOnHit
array.remove(zones, i)
f_cut(z, keep, time_close)
// ─────────────────────────────────────────────────────────────
// Zone calculation
// ─────────────────────────────────────────────────────────────
var float h1 = na
var float l1 = na
var float h4 = na
var float l4 = na
var array<Zone> zones1 = array.new<Zone>()
var array<Zone> zones4 = array.new<Zone>()
var array<Zone> zonesU = array.new<Zone>()
f_heightForSymbol(string map, string sym, float fallback) =>
rows = str.split(map, "\n")
result = fallback
for i = 0 to array.size(rows) - 1
row = str.trim(array.get(rows, i))
if row != ""
parts = str.split(row, ":")
if array.size(parts) == 2 and str.trim(array.get(parts, 0)) == sym
v = str.tonumber(str.trim(array.get(parts, 1)))
if not na(v)
result := v
result
pipSize = customPip > 0 ? customPip : syminfo.mintick * ((syminfo.mintick == 0.001 or syminfo.mintick == 0.00001) ? 10 : 1)
unitSize = zoneUnit == "Pips" ? pipSize : zoneUnit == "Ticks" ? syminfo.mintick : 1.0
z1Height = f_heightForSymbol(zoneMap, syminfo.ticker, zoneHeight) * unitSize
z4Height = z1Height
if is1m
// 1H
if new1h
pH = h1
pL = l1
h1 := high
l1 := low
if showZ1 and not na(pH)
f_rollover(zones1, zonesU, z1Keep, t1h)
array.push(zones1, f_newZone(t1h[1], pH, true, z1Height, z1Bg, z1Bd, zWidth, zStyle))
array.push(zones1, f_newZone(t1h[1], pL, false, z1Height, z1Bg, z1Bd, zWidth, zStyle))
else
h1 := math.max(nz(h1, high), high)
l1 := math.min(nz(l1, low), low)
// 4H
if new4h
pH = h4
pL = l4
h4 := high
l4 := low
if showZ4 and not na(pH)
f_rollover(zones4, zonesU, z4Keep, t4h)
array.push(zones4, f_newZone(t4h[1], pH, true, z4Height, z4Bg, z4Bd, zWidth, zStyle))
array.push(zones4, f_newZone(t4h[1], pL, false, z4Height, z4Bg, z4Bd, zWidth, zStyle))
else
h4 := math.max(nz(h4, high), high)
l4 := math.min(nz(l4, low), low)
array.clear(brokenLvls)
if new1h or new4h
f_cleanUntouched(zonesU, uDist * unitSize, uMax)
f_checkHits(zones1, false, z1Keep)
f_checkHits(zones4, false, z4Keep)
if array.size(brokenLvls) > 0 and uBreakDist > 0
for i = 0 to array.size(brokenLvls) - 1
bLvl = array.get(brokenLvls, i)
f_invalidateUntouched(zonesU, bLvl, uBreakDist * unitSize)
f_checkHits(zonesU, true, uKeep)
// ─────────────────────────────────────────────────────────────
// Timeframe warning
// ─────────────────────────────────────────────────────────────
var table warn = table.new(position.top_right, 1, 1)
if barstate.islast and not is1m
table.cell(warn, 0, 0, "HTF Time Lines: designed for the 1-minute timeframe only", text_color=color.white, bgcolor=color.new(color.red, 20))