本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nambou_Signals
//@version=6
indicator("Nambou Equal Highs & Lows Odds", "EQH/EQL Odds", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 1000)
gL = "Levels"
pivLen = input.int(3, "Swing length", minval = 2, maxval = 20, group = gL, tooltip = "A swing high is the highest high of this many bars on each side. It is confirmed that many bars later.")
tolAtr = input.float(0.15, "Equal within this many ATR", minval = 0.01, maxval = 1.0, step = 0.01, group = gL, tooltip = "Two untaken swing highs (or lows) this close together form an equal high (low). The pool's level is the more extreme of them.")
minGap = input.int(5, "Fewest bars between the two swings", minval = 1, maxval = 100, group = gL)
maxAge = input.int(300, "Forget levels older than this many bars", minval = 50, maxval = 900, group = gL)
gO = "Odds"
horizon = input.int(24, "Odds of being taken within this many bars", minval = 4, maxval = 200, group = gO, tooltip = "Every few bars, each untaken swing level (equal or not) is recorded with its distance from the close in ATR. The odds next to a level are how often levels at the same distance were taken within this many bars on this chart.")
revAtr = input.float(1.0, "After a sweep, a reversal is a move of this many ATR", minval = 0.2, maxval = 5.0, step = 0.1, group = gO, tooltip = "Measured from the close of the bar that took the level, before price runs the same distance beyond the level.")
revWait = input.int(20, "Bars a reversal has to happen in", minval = 2, maxval = 200, group = gO)
minObs = input.int(30, "Fewest past cases behind a number", minval = 10, maxval = 500, group = gO)
gD = "Display"
nearest = input.int(3, "Odds shown for the nearest pools on each side", minval = 1, maxval = 10, group = gD, tooltip = "The other untaken pools are still drawn, without a number.")
maxDrawn = input.int(30, "Swept pools kept on the chart", minval = 0, maxval = 150, group = gD)
showTable = input.bool(true, "Show the table", group = gD)
tablePosIn = input.string("Top right", "Table position", options = ["Top right", "Bottom right", "Top center", "Bottom center", "Top left", "Bottom left"], group = gD)
txtSizeIn = input.string("Large", "Chart text size", options = ["Tiny", "Small", "Normal", "Large"], group = gD)
tblSizeIn = input.string("Normal", "Table text size", options = ["Tiny", "Small", "Normal", "Large"], group = gD)
cHi = input.color(#F23645, "Equal highs", group = gD)
cLo = input.color(#089981, "Equal lows", group = gD)
cMiss = input.color(#9598A1, "Swept, no reversal", group = gD)
cTxt = chart.fg_color
gA = "Alerts"
aNew = input.bool(true, "Alert when an equal high or low forms", group = gA)
aSweep = input.bool(true, "Alert when one is swept", group = gA)
sizeOf(string s) => s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Large" ? size.large : size.normal
txtSize = sizeOf(txtSizeIn)
tagSize = txtSizeIn == "Large" ? size.normal : txtSizeIn == "Normal" ? size.small : size.tiny
tblSize = sizeOf(tblSizeIn)
type Pool
int side
float lvl
int born
int last
int touches = 1
bool eq = false
line ln
line ext
label lb
label tag
string tips
type Rec
int side
float lvl
int start
int band
bool eq
type Sweep
int side
float lvl
float ref
float atr
int start
bool eq
Pool pool
var pools = array.new<Pool>()
var recs = array.new<Rec>()
var sweeps = array.new<Sweep>()
var done = array.new<Pool>()
// distance bands: taken, cases
var cellHit = array.new_int(8, 0)
var cellN = array.new_int(8, 0)
// at 0.5-1.5 ATR: [equal, ordinary] taken and cases; after the sweep: reversed and cases
var takeHit = array.new_int(2, 0)
var takeN = array.new_int(2, 0)
var revHit = array.new_int(2, 0)
var revN = array.new_int(2, 0)
band(float x) => x < 0.25 ? 0 : x < 0.5 ? 1 : x < 1.0 ? 2 : x < 1.5 ? 3 : x < 2.0 ? 4 : x < 3.0 ? 5 : x < 5.0 ? 6 : 7
odds(int b) => cellN.get(b) >= minObs ? cellHit.get(b) * 100.0 / cellN.get(b) : na
pctText(float v) => na(v) ? "—" : str.tostring(math.round(v)) + "%"
name(int side, int touches) => (side > 0 ? "EQH" : "EQL") + (touches > 2 ? " ×" + str.tostring(touches) : "")
float atrNow = ta.atr(14)
wipe(Pool p) =>
line.delete(p.ln)
line.delete(p.ext)
label.delete(p.lb)
label.delete(p.tag)
place(Pool p) =>
line.set_xy1(p.ln, p.born, p.lvl)
line.set_xy2(p.ln, p.last, p.lvl)
line.set_y1(p.ext, p.lvl)
line.set_y2(p.ext, p.lvl)
line.set_x1(p.ext, p.last)
label.set_xy(p.lb, int(math.round((p.born + p.last) / 2.0)), p.lvl)
label.set_text(p.lb, name(p.side, p.touches))
// recorded distances: taken within the horizon or not
if recs.size() > 0
for i = recs.size() - 1 to 0
Rec r = recs.get(i)
bool hit = r.side > 0 ? high > r.lvl : low < r.lvl
if hit or bar_index - r.start >= horizon
cellN.set(r.band, cellN.get(r.band) + 1)
cellHit.set(r.band, cellHit.get(r.band) + (hit ? 1 : 0))
if r.band == 2 or r.band == 3
int k = r.eq ? 0 : 1
takeN.set(k, takeN.get(k) + 1)
takeHit.set(k, takeHit.get(k) + (hit ? 1 : 0))
recs.remove(i)
// sweeps: reversal or run
if sweeps.size() > 0
for i = sweeps.size() - 1 to 0
Sweep s = sweeps.get(i)
bool ran = s.side > 0 ? high > s.lvl + revAtr * s.atr : low < s.lvl - revAtr * s.atr
bool rev = s.side > 0 ? s.ref - low >= revAtr * s.atr : high - s.ref >= revAtr * s.atr
bool up = bar_index - s.start >= revWait
if ran or rev or up
bool ok = rev and not ran
int k = s.eq ? 0 : 1
revN.set(k, revN.get(k) + 1)
revHit.set(k, revHit.get(k) + (ok ? 1 : 0))
if not na(s.pool)
Pool p = s.pool
label.set_text(p.lb, name(p.side, p.touches) + (ok ? " ✓" : ran ? " ✗" : " ·"))
label.set_tooltip(p.lb, name(p.side, p.touches) + " at " + str.tostring(p.lvl, format.mintick) + " swept, then " + (ok ? "reversed " + str.tostring(revAtr) + " ATR" : ran ? "kept going " + str.tostring(revAtr) + " ATR" : "neither within " + str.tostring(revWait) + " bars"))
if not ok
label.set_textcolor(p.lb, cMiss)
line.set_color(p.ln, cMiss)
line.set_color(p.ext, color.new(cMiss, 30))
sweeps.remove(i)
// takes of untaken levels
if pools.size() > 0
for i = pools.size() - 1 to 0
Pool p = pools.get(i)
bool taken = p.side > 0 ? high > p.lvl : low < p.lvl
bool old = bar_index - p.born > maxAge
if taken
Pool tgt = na
if p.eq
tgt := p
line.set_x2(p.ext, bar_index)
label.delete(p.tag)
p.tag := na
done.push(p)
if done.size() > maxDrawn
wipe(done.shift())
if aSweep and barstate.isconfirmed
alert("EQH/EQL Odds: " + syminfo.ticker + " " + name(p.side, p.touches) + " swept at " + str.tostring(p.lvl, format.mintick), alert.freq_once_per_bar_close)
if not na(atrNow)
sweeps.push(Sweep.new(p.side, p.lvl, close, atrNow, bar_index, p.eq, tgt))
pools.remove(i)
else if old
wipe(p)
pools.remove(i)
// new swings: a new level, or an equal high / low with an earlier one
float ph = ta.pivothigh(high, pivLen, pivLen)
float pl = ta.pivotlow(low, pivLen, pivLen)
addSwing(int side, float v) =>
int x = bar_index - pivLen
int match = -1
if pools.size() > 0 and not na(atrNow)
for i = 0 to pools.size() - 1
Pool p = pools.get(i)
if p.side == side and x - p.born >= minGap and math.abs(p.lvl - v) <= tolAtr * atrNow
match := i
if match >= 0
Pool p = pools.get(match)
p.lvl := side > 0 ? math.max(p.lvl, v) : math.min(p.lvl, v)
p.touches += 1
p.last := x
color c = side > 0 ? cHi : cLo
if not p.eq
p.eq := true
p.ln := line.new(p.born, p.lvl, x, p.lvl, color = c, width = 2)
p.ext := line.new(x, p.lvl, bar_index, p.lvl, color = color.new(c, 20), width = 2, style = line.style_dotted)
p.lb := label.new(x, p.lvl, "", style = side > 0 ? label.style_label_down : label.style_label_up, color = color(na), textcolor = c, size = tagSize)
if aNew and barstate.isconfirmed
alert("EQH/EQL Odds: " + syminfo.ticker + (side > 0 ? " equal highs" : " equal lows") + " at " + str.tostring(p.lvl, format.mintick), alert.freq_once_per_bar_close)
place(p)
else
pools.push(Pool.new(side, v, x, x))
if not na(ph)
addSwing(1, ph)
if not na(pl)
addSwing(-1, pl)
// record each untaken level's distance every few bars
if bar_index % 4 == 0 and not na(atrNow) and atrNow > 0 and pools.size() > 0
for p in pools
float d = (p.lvl - close) / atrNow * p.side
if d >= 0
recs.push(Rec.new(p.side, p.lvl, bar_index, band(d), p.eq))
// live pools: extend the line, odds on the nearest (pools too close together share one number)
if barstate.islast and pools.size() > 0
float wHi = high
float wLo = low
int wj = 0
while wj < 999 and time[wj] >= chart.left_visible_bar_time
wHi := math.max(wHi, high[wj])
wLo := math.min(wLo, low[wj])
wj += 1
float gap = (wHi - wLo) * (txtSizeIn == "Large" ? 0.045 : 0.035)
for p in pools
if p.eq
line.set_x2(p.ext, bar_index + 3)
label.delete(p.tag)
p.tag := na
for side in array.from(1, -1)
array<float> ds = array.new_float()
for p in pools
if p.eq and p.side == side
ds.push(math.abs(p.lvl - close))
ds.sort()
int shown = 0
array<Pool> placed = array.new<Pool>()
for k = 0 to ds.size() - 1
if ds.size() == 0 or shown >= nearest
break
for p in pools
if p.eq and p.side == side and na(p.tag) and math.abs(p.lvl - close) == ds.get(k)
float d = ds.get(k)
float v = na(atrNow) ? na : odds(band(d / atrNow))
string tip = name(side, p.touches) + " at " + str.tostring(p.lvl, format.mintick) + ": " + pctText(v) + " odds it is taken within " + str.tostring(horizon) + " bars (" + str.tostring(d / atrNow, "#.##") + " ATR away)"
Pool near = na
for q in placed
if math.abs(q.lvl - p.lvl) < gap
near := q
if not na(near)
near.tips := near.tips + "\n" + tip
label.set_tooltip(near.tag, near.tips)
p.tag := label.new(bar_index, p.lvl, "", style = label.style_none)
else
p.tag := label.new(bar_index + 3, p.lvl, pctText(v), style = label.style_label_left, color = color(na), textcolor = side > 0 ? cHi : cLo, size = txtSize, tooltip = tip)
p.tips := tip
placed.push(p)
shown += 1
break
tPos = switch tablePosIn
"Top center" => position.top_center
"Bottom center" => position.bottom_center
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
"Top left" => position.top_left
=> position.top_right
var table tbl = table.new(tPos, 3, 6, border_width = 0, frame_width = 0)
tblBg = color.new(chart.bg_color, 0)
cell(int c, int r, string v, color tc, bool bold = false) =>
table.cell(tbl, c, r, v, text_color = tc, text_halign = c == 0 ? text.align_left : text.align_right, text_size = tblSize, bgcolor = tblBg, text_formatting = bold ? text.format_bold : text.format_none)
rate(array<int> hit, array<int> n, int k) => n.get(k) < minObs ? na : hit.get(k) * 100.0 / n.get(k)
diffText(array<int> hit, array<int> n) =>
float a = rate(hit, n, 0)
float b = rate(hit, n, 1)
na(a) or na(b) ? "—" : (a - b >= 0 ? "+" : "−") + str.tostring(math.abs(math.round(a - b))) + " pts"
if barstate.islast and showTable
table.clear(tbl, 0, 0, 2, 5)
color dim = color.new(cTxt, 45)
table.cell(tbl, 0, 0, "Equal highs & lows: are they liquidity?", text_color = cTxt, text_halign = text.align_left, text_size = tblSize, bgcolor = tblBg, text_formatting = text.format_bold, tooltip = "taken = traded through within " + str.tostring(horizon) + " bars, starting 0.5 to 1.5 ATR away. reversed = moved " + str.tostring(revAtr) + " ATR back within " + str.tostring(revWait) + " bars after the sweep, before running " + str.tostring(revAtr) + " ATR beyond the level. Ordinary swings = untaken swing highs and lows that are not equal.")
table.merge_cells(tbl, 0, 0, 2, 0)
cell(0, 1, "", dim)
cell(1, 1, "taken", dim)
cell(2, 1, "reversed", dim)
cell(0, 2, "Equal levels", cTxt)
cell(1, 2, pctText(rate(takeHit, takeN, 0)), cTxt, true)
cell(2, 2, pctText(rate(revHit, revN, 0)), cTxt, true)
cell(0, 3, "Ordinary swings", cTxt)
cell(1, 3, pctText(rate(takeHit, takeN, 1)), cTxt)
cell(2, 3, pctText(rate(revHit, revN, 1)), cTxt)
cell(0, 4, "Difference", dim)
cell(1, 4, diffText(takeHit, takeN), dim)
cell(2, 4, diffText(revHit, revN), dim)