本页源码来自 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("Sweep Odds [Nambou]", "Sweep Odds", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 500)
gL = "Liquidity levels"
len = input.int(8, "Swing size: bars on each side", minval = 2, maxval = 30, group = gL)
maxN = input.int(4, "Swing levels tracked on each side", minval = 1, maxval = 15, group = gL)
usePD = input.bool(true, "Also track the previous day's high and low", group = gL, tooltip = "Intraday charts only.")
gS = "Scoring"
kAtr = input.float(1.0, "A sweep counts as reversed after a move back of this many ATR", minval = 0.25, maxval = 5.0, step = 0.25, group = gS)
maxBar = input.int(20, "within this many bars", minval = 3, maxval = 200, group = gS, tooltip = "The move back is measured from the close of the sweep bar. A sweep fails when a later bar closes beyond the sweep's wick before that. If neither happens in time, it faded. ATR is the average true range of the last 14 bars.")
wickPct = input.int(50, "A strong rejection has a wick of at least this % of the bar", minval = 10, maxval = 90, group = gS, tooltip = "The wick beyond the close and open, measured against the whole bar from high to low. Odds are kept separately for strong and weak rejections.")
minN = input.int(8, "Show odds once a level type has this many scored sweeps", minval = 1, maxval = 100, group = gS)
gD = "Display"
cHi = input.color(#F23645, "Highs", group = gD, inline = "c")
cLo = input.color(#089981, "Lows", group = gD, inline = "c")
cPD = input.color(#29B6F6, "Previous day", group = gD, inline = "c")
cWin = input.color(#26A69A, "Reversed", group = gD, inline = "r")
cLoss = input.color(#EF5350, "Failed", group = gD, inline = "r")
cFade = input.color(#787B86, "Faded", group = gD, inline = "r")
showTbl = input.bool(true, "Show the odds table", group = gD)
tblPos = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = gD)
doAlerts = input.bool(true, "Send an alert on every sweep, with its odds", group = gD, tooltip = "Create an alert on the indicator with \"Any alert() function call\".")
type Level
float p
int b
line ln
type Sweep
int side
int kind
float target
float ext
int bar
label lb
label dot
line tl
string odds
type Probe
int side
float target
float ext
int bar
var array<Level> his = array.new<Level>()
var array<Level> los = array.new<Level>()
var array<Sweep> live = array.new<Sweep>()
var array<Probe> probes = array.new<Probe>()
var array<int> cnt = array.new<int>(8, 0)
var array<int> win = array.new<int>(8, 0)
var array<int> bCnt = array.from(0, 0)
var array<int> bWin = array.from(0, 0)
kindName(int k) => k == 0 ? "Swing high" : k == 1 ? "Swing low" : k == 2 ? "Previous day high" : "Previous day low"
pct(int w, int n) => n > 0 ? str.tostring(100.0 * w / n, "#") + "%" : "–"
atr = ta.atr(14)
ph = ta.pivothigh(high, len, len)
pl = ta.pivotlow(low, len, len)
intraday = timeframe.in_seconds() < 86400
newDay = timeframe.change("D")
var float dHi = na
var float dLo = na
var int dHiB = na
var int dLoB = na
var float pdh = na
var float pdl = na
var int pdhB = na
var int pdlB = na
var bool pdhLive = false
var bool pdlLive = false
if newDay
pdh := dHi
pdl := dLo
pdhB := dHiB
pdlB := dLoB
pdhLive := usePD and intraday and not na(pdh)
pdlLive := usePD and intraday and not na(pdl)
dHi := high
dLo := low
dHiB := bar_index
dLoB := bar_index
else
if na(dHi) or high > dHi
dHi := high
dHiB := bar_index
if na(dLo) or low < dLo
dLo := low
dLoB := bar_index
resolve(float target, float ext, int side, int bar) =>
int r = 0
bool failed = side == 1 ? close > ext : close < ext
bool hit = side == 1 ? low <= target : high >= target
if failed
r := -1
else if hit
r := 1
else if bar_index - bar >= maxBar
r := 2
r
if barstate.isconfirmed and not na(atr)
if probes.size() > 0
for i = probes.size() - 1 to 0
Probe q = probes.get(i)
int r = resolve(q.target, q.ext, q.side, q.bar)
if r != 0
int s = q.side == 1 ? 0 : 1
bCnt.set(s, bCnt.get(s) + 1)
if r == 1
bWin.set(s, bWin.get(s) + 1)
probes.remove(i)
probes.push(Probe.new(1, close - kAtr * atr, high, bar_index))
probes.push(Probe.new(-1, close + kAtr * atr, low, bar_index))
if live.size() > 0
for i = live.size() - 1 to 0
Sweep w = live.get(i)
if bar_index > w.bar
int r = resolve(w.target, w.ext, w.side, w.bar)
if r != 0
cnt.set(w.kind, cnt.get(w.kind) + 1)
if r == 1
win.set(w.kind, win.get(w.kind) + 1)
w.dot.set_color(color.new(r == 1 ? cWin : r == -1 ? cLoss : cFade, w.kind % 2 == 0 ? 0 : 55))
w.lb.delete()
w.tl.delete()
if r == 1
line.new(w.bar, w.ext, bar_index, w.target, color = color.new(cWin, 30), width = 2, style = line.style_arrow_right)
live.remove(i)
int hiKind = -1
int loKind = -1
if pdhLive and high > pdh
if close < pdh
hiKind := 2
pdhLive := false
if pdlLive and low < pdl
if close > pdl
loKind := 3
pdlLive := false
if his.size() > 0
for i = his.size() - 1 to 0
Level v = his.get(i)
if high > v.p
if close < v.p and hiKind < 0
hiKind := 0
v.ln.delete()
his.remove(i)
if los.size() > 0
for i = los.size() - 1 to 0
Level v = los.get(i)
if low < v.p
if close > v.p and loKind < 0
loKind := 1
v.ln.delete()
los.remove(i)
for side in array.from(1, -1)
int k = side == 1 ? hiKind : loKind
if k >= 0
float rng = high - low
float wick = side == 1 ? high - math.max(open, close) : math.min(open, close) - low
bool strong = rng > 0 and wick / rng * 100.0 >= wickPct
k := k * 2 + (strong ? 0 : 1)
int n = cnt.get(k)
string odds = n >= minN ? "↩ " + pct(win.get(k), n) : "↩ new"
float tgt = side == 1 ? close - kAtr * atr : close + kAtr * atr
float ext = side == 1 ? high : low
color sc = side == 1 ? cHi : cLo
label dot = label.new(bar_index, ext, "", style = label.style_circle, color = color.new(sc, strong ? 0 : 55), size = size.tiny)
label lb = label.new(bar_index, ext, odds, style = side == 1 ? label.style_label_down : label.style_label_up, color = color(na), textcolor = sc, size = size.large, text_formatting = text.format_bold)
line tl = line.new(bar_index, tgt, bar_index + maxBar, tgt, color = color.new(sc, 40), style = line.style_dotted)
live.push(Sweep.new(side, k, tgt, ext, bar_index, lb, dot, tl, odds))
if doAlerts
int bs = side == 1 ? 0 : 1
alert(syminfo.ticker + ": " + kindName(int(math.floor(k / 2))) + " swept, " + (strong ? "strong" : "weak") + " rejection. Reversal odds " + pct(win.get(k), n) + " of " + str.tostring(n) + " past sweeps, any bar " + pct(bWin.get(bs), bCnt.get(bs)) + ".", alert.freq_once_per_bar_close)
if not na(ph)
his.push(Level.new(ph, bar_index - len, line.new(bar_index - len, ph, bar_index, ph, color = color.new(cHi, 15), width = 2)))
if his.size() > maxN
his.shift().ln.delete()
if not na(pl)
los.push(Level.new(pl, bar_index - len, line.new(bar_index - len, pl, bar_index, pl, color = color.new(cLo, 15), width = 2)))
if los.size() > maxN
los.shift().ln.delete()
var table tb = na
var line pdhLn = na
var line pdlLn = na
var array<label> lvLbs = array.new<label>()
if barstate.islast
for l in lvLbs
l.delete()
lvLbs.clear()
pdhLn.delete()
pdlLn.delete()
array<float> ps = array.new<float>()
array<string> ts = array.new<string>()
array<color> cs = array.new<color>()
for v in his
v.ln.set_x2(bar_index + 3)
ps.push(v.p), ts.push("Swing high " + str.tostring(v.p, format.mintick)), cs.push(cHi)
for v in los
v.ln.set_x2(bar_index + 3)
ps.push(v.p), ts.push("Swing low " + str.tostring(v.p, format.mintick)), cs.push(cLo)
if pdhLive
pdhLn := line.new(pdhB, pdh, bar_index + 3, pdh, color = cPD, width = 2)
ps.push(pdh), ts.push("PDH " + str.tostring(pdh, format.mintick)), cs.push(cPD)
if pdlLive
pdlLn := line.new(pdlB, pdl, bar_index + 3, pdl, color = cPD, width = 2)
ps.push(pdl), ts.push("PDL " + str.tostring(pdl, format.mintick)), cs.push(cPD)
if ps.size() > 0
array<int> idx = ps.sort_indices()
float gap = atr * 0.7
float last = na
for i in idx
float y = ps.get(i)
if not na(last) and y - last < gap
y := last + gap
last := y
lvLbs.push(label.new(bar_index + 3, y, ts.get(i), style = label.style_label_left, color = color(na), textcolor = cs.get(i), size = size.normal, text_formatting = text.format_bold))
if not na(tb)
tb.delete()
if showTbl
pos = tblPos == "Top left" ? position.top_left : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Bottom left" ? position.bottom_left : position.top_right
tb := table.new(pos, 4, 7, bgcolor = color.new(#0B0E14, 15), border_width = 0, frame_width = 0)
color hd = color.new(#D1D4DC, 0)
color sub = color.new(#787B86, 0)
tb.cell(0, 0, "SWEEP ODDS", text_color = hd, text_size = size.large, text_halign = text.align_left, text_formatting = text.format_bold)
tb.cell(1, 0, "strong", text_color = sub, text_size = size.normal)
tb.cell(2, 0, "weak", text_color = sub, text_size = size.normal)
tb.cell(3, 0, "any bar", text_color = sub, text_size = size.normal)
for k = 0 to 3
bool hiSide = k == 0 or k == 2
if k < 2 or (usePD and intraday)
int bs = hiSide ? 0 : 1
tb.cell(0, k + 1, kindName(k), text_color = k >= 2 ? cPD : hiSide ? cHi : cLo, text_size = size.normal, text_halign = text.align_left)
tb.cell(1, k + 1, pct(win.get(k * 2), cnt.get(k * 2)) + " (" + str.tostring(cnt.get(k * 2)) + ")", text_color = hd, text_size = size.normal, text_formatting = text.format_bold)
tb.cell(2, k + 1, pct(win.get(k * 2 + 1), cnt.get(k * 2 + 1)) + " (" + str.tostring(cnt.get(k * 2 + 1)) + ")", text_color = hd, text_size = size.normal)
tb.cell(3, k + 1, pct(bWin.get(bs), bCnt.get(bs)), text_color = sub, text_size = size.normal)
tb.cell(0, 5, "reversed = " + str.tostring(kAtr) + " ATR back within " + str.tostring(maxBar) + " bars", text_color = sub, text_size = size.small, text_halign = text.align_left)
tb.merge_cells(0, 5, 3, 5)