本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Harmonic Hours", overlay=true, max_labels_count=500, max_lines_count=500)
// ---------- Inputs ----------
g1 = "Intersection"
base_utc = input.int(0, "Base hour (UTC)", minval=0, maxval=23, tooltip="0 UTC = 03:00 TR", group=g1)
min_cross = input.int(2, "Min intersections (shading)", minval=1, maxval=8, group=g1)
show_label = input.bool(true, "Show label", group=g1)
g2 = "Pre-window"
show_pre = input.bool(true, "Shade pre-window", group=g2)
pre_min = input.int(6, "Target intersections (>=)", minval=2, maxval=8, group=g2)
pre_hours = input.int(2, "Hours before", minval=1, maxval=4, group=g2)
g3 = "Liquidity lines"
show_liq = input.bool(true, "Liquidity lines", group=g3)
liq_min = input.int(6, "Min intersections (>=)", minval=2, maxval=8, group=g3)
max_age = input.int(300, "Max age (bars)", minval=10, group=g3)
g4 = "Open lines"
show_o4 = input.bool(true, "4h open", group=g4)
show_o8 = input.bool(false, "8h open", group=g4)
show_o12 = input.bool(true, "12h open", group=g4)
show_o1d = input.bool(true, "1D open", group=g4)
g5 = "Statistics"
show_stats = input.bool(false, "Statistics table", group=g5)
// ---------- Helpers ----------
tf_sec = timeframe.in_seconds()
tf_bad = tf_sec > 3600 or 3600 % tf_sec != 0
f_count(int e) =>
int n = 1
for d in array.from(2, 3, 4, 6, 8, 12, 24)
if e % d == 0
n += 1
n
f_pre(int e) =>
bool r = false
for k = 1 to pre_hours
if f_count((e + k) % 24) >= pre_min
r := true
r
// ---------- Hour / intersection ----------
hr = hour(time, "UTC")
elapsed = (hr - base_utc + 24) % 24
newHour = bar_index == 0 or hr != hour(time[1], "UTC")
count = (newHour and not tf_bad) ? f_count(elapsed) : 0
hit = count >= min_cross
inPre = show_pre and not tf_bad and f_pre(elapsed)
preStart = inPre and not inPre[1]
// ---------- Hourly candle tracking ----------
var float hO = na
var float hH = na
var float hL = na
var int hT = na
var int hE = na
var bool warm = false
var sumR = array.new_float(24, 0.0)
var cntR = array.new_int(24, 0)
var line[] hiLines = array.new_line()
var line[] loLines = array.new_line()
if newHour
if warm and not na(hH)
// statistics of the finished hour
rng = (hH - hL) / hO * 100
array.set(sumR, hE, array.get(sumR, hE) + rng)
array.set(cntR, hE, array.get(cntR, hE) + 1)
// draw liquidity lines if the hour had high intersections
if show_liq and f_count(hE) >= liq_min
array.push(hiLines, line.new(hT, hH, bar_index, hH, color=color.new(color.red, 25), style=line.style_dashed))
array.push(loLines, line.new(hT, hL, bar_index, hL, color=color.new(color.green, 25), style=line.style_dashed))
warm := bar_index > 0
hO := open
hH := high
hL := low
hT := bar_index
hE := elapsed
else
hH := math.max(hH, high)
hL := math.min(hL, low)
// ---------- Extend lines / sweep detection ----------
bool sweepUp = false
bool sweepDn = false
if array.size(hiLines) > 0
for i = array.size(hiLines) - 1 to 0
l = array.get(hiLines, i)
if high > line.get_y1(l)
line.set_x2(l, bar_index)
line.set_color(l, color.new(color.gray, 60))
line.set_style(l, line.style_dotted)
array.remove(hiLines, i)
sweepUp := true
else if bar_index - line.get_x1(l) > max_age
array.remove(hiLines, i)
else
line.set_x2(l, bar_index)
if array.size(loLines) > 0
for i = array.size(loLines) - 1 to 0
l = array.get(loLines, i)
if low < line.get_y1(l)
line.set_x2(l, bar_index)
line.set_color(l, color.new(color.gray, 60))
line.set_style(l, line.style_dotted)
array.remove(loLines, i)
sweepDn := true
else if bar_index - line.get_x1(l) > max_age
array.remove(loLines, i)
else
line.set_x2(l, bar_index)
// ---------- Visuals ----------
// 8 intersections = darkest, 1 = lightest
transp = hit ? math.max(85 - count * 9, 15) : 100
bgcolor(hit ? color.new(color.blue, transp) : inPre ? color.new(color.orange, 88) : na)
if show_label and hit
label.new(bar_index, low, str.tostring(count) + "x", style=label.style_label_up, color=color.new(color.blue, 40), textcolor=color.white, size=size.tiny)
plotshape(sweepUp, "Upper sweep", shape.xcross, location.abovebar, color.red, size=size.tiny)
plotshape(sweepDn, "Lower sweep", shape.xcross, location.belowbar, color.green, size=size.tiny)
// ---------- Open lines ----------
o4 = request.security(syminfo.tickerid, "240", open, lookahead=barmerge.lookahead_on)
o8 = request.security(syminfo.tickerid, "480", open, lookahead=barmerge.lookahead_on)
o12 = request.security(syminfo.tickerid, "720", open, lookahead=barmerge.lookahead_on)
o1d = request.security(syminfo.tickerid, "D", open, lookahead=barmerge.lookahead_on)
plot(show_o4 and tf_sec < 14400 ? o4 : na, "4h open", color.new(color.aqua, 40), 1, plot.style_linebr)
plot(show_o8 and tf_sec < 28800 ? o8 : na, "8h open", color.new(color.purple, 40), 1, plot.style_linebr)
plot(show_o12 and tf_sec < 43200 ? o12 : na, "12h open", color.new(color.yellow, 30), 1, plot.style_linebr)
plot(show_o1d and tf_sec < 86400 ? o1d : na, "1D open", color.new(color.white, 30), 2, plot.style_linebr)
// ---------- Table ----------
var tbl = table.new(position.top_right, 5, 25, border_width=1)
if barstate.islast
if tf_bad
table.cell(tbl, 0, 0, "Set chart to 1h or lower", bgcolor=color.red, text_color=color.white)
else if show_stats
heads = array.from("TR Hour", "Int.", "Avg %", "Ratio", "n")
for c = 0 to 4
table.cell(tbl, c, 0, array.get(heads, c), bgcolor=color.gray, text_color=color.white)
overall = array.sum(sumR) / math.max(array.sum(cntR), 1)
for e = 0 to 23
n = array.get(cntR, e)
avg = n > 0 ? array.get(sumR, e) / n : 0.0
ratio = overall > 0 ? avg / overall : 0.0
bg = ratio > 1.3 ? color.new(color.red, 40) : ratio > 1.1 ? color.new(color.orange, 50) : color.new(color.gray, 85)
trh = (e + base_utc + 3) % 24
table.cell(tbl, 0, e + 1, str.format("{0,number,00}:00", trh), bgcolor=bg)
table.cell(tbl, 1, e + 1, str.tostring(f_count(e)) + "x", bgcolor=bg)
table.cell(tbl, 2, e + 1, str.tostring(avg, "#.##"), bgcolor=bg)
table.cell(tbl, 3, e + 1, str.tostring(ratio, "#.##"), bgcolor=bg)
table.cell(tbl, 4, e + 1, str.tostring(n), bgcolor=bg)
// ---------- Alerts ----------
alertcondition(hit and count >= pre_min, "High intersection", "Strong candle intersection")
alertcondition(preStart, "Pre-window started", "Pre-window before high intersection has started")
alertcondition(sweepUp, "Upper liquidity swept", "Upper liquidity taken")
alertcondition(sweepDn, "Lower liquidity swept", "Lower liquidity taken")