本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ─────────────────────────────────────────────────────────────────────────────
// Sweep [pandich] v1.0 (31.10.2026)
// Liquidity-sweep reversal with a wick-based stop, trade zones and an honest
// scoreboard (expectancy in R after costs).
//
// Attribution: the sweep condition is taken from "Setup Scanner [GBB]" v1.1
// by GoodBadBitcoin (open source) and modified: standalone, stop behind the
// sweep wick instead of a fixed ATR multiple, minimum / maximum stop filter,
// single-trade engine, scoreboard in R with a cost input. Everything else is
// new code.
//
// Setup: price takes out the lowest low / highest high of the last N bars with
// a wick, closes back inside the range on a bar of the reversal colour, and the
// close sits in the far half of the bar. Entry at the signal bar's close
// (confirmed bar only, no repaint).
//
// NOT a proven edge. Sweep-type setups were tested in this project before
// (Liquidity Sweep PF 0.53-0.80, Shoh Ake Bank E -0.07R). Read the scoreboard:
// below ~30 trades it is noise; "→TP1" for a random entry is about 50%.
// ─────────────────────────────────────────────────────────────────────────────
indicator("Sweep [pandich]", "SWEEP-p", overlay = true,
max_boxes_count = 200, max_lines_count = 300, max_labels_count = 200)
// ═════════════════════════════════════════════════════════════════════════════
// INPUTS
// ═════════════════════════════════════════════════════════════════════════════
var string GS = "Sweep"
int sweepLen = input.int(20, "Sweep lookback (bars)", minval = 5, group = GS,
tooltip = "A sweep must take out the extreme of this many previous bars.")
float volMult = input.float(1.0, "Volume must exceed x SMA20", minval = 0, step = 0.1, group = GS,
tooltip = "0 disables the filter. Volume on CFD/FX is tick volume.")
int atrLen = input.int(14, "ATR length", minval = 2, group = GS)
var string GT = "Trade plan"
float wickBuf = input.float(0.2, "Stop buffer behind wick (ATR)", minval = 0, step = 0.05, group = GT,
tooltip = "Stop = sweep extreme minus (plus for shorts) this many ATR.")
float minStop = input.float(0.5, "Min stop distance (ATR)", minval = 0.1, step = 0.1, group = GT,
tooltip = "If the wick stop is closer than this, the stop is pushed out to it.")
float maxStop = input.float(3.0, "Max stop distance (ATR, 0 = off)", minval = 0, step = 0.1, group = GT,
tooltip = "Signals whose wick stop is wider than this are skipped (counted as blocked).")
float r1 = input.float(1.0, "TP1 (R)", minval = 0.2, step = 0.1, group = GT)
float r2 = input.float(2.0, "TP2 (R)", minval = 0.2, step = 0.1, group = GT)
float r3 = input.float(3.0, "TP3 (R)", minval = 0.2, step = 0.1, group = GT)
int maxBars = input.int(60, "Give up after N bars", minval = 5, group = GT)
int reentry = input.int(1, "Re-entry cooldown (bars)", minval = 0, group = GT)
float costPx = input.float(0.30, "Cost per trade (price units)", minval = 0, step = 0.05, group = GT,
tooltip = "Spread + commission per round trip. XAUUSD ≈ 0.30. Used only in the scoreboard.")
var string GD = "Display"
bool showZones = input.bool(true, "Draw trade zones", group = GD)
int zoneLen = input.int(6, "Project zone N bars ahead", minval = 1, group = GD)
int keepN = input.int(8, "Keep last N drawings", minval = 1, maxval = 40, group = GD)
string tblPos = input.string("Top right", "Panel position",
options = ["Top right", "Top left", "Bottom right", "Bottom left", "Middle right"], group = GD)
bool showPanel = input.bool(true, "Show panel", group = GD)
int minSamp = input.int(30, "Grey out below N trades", minval = 1, group = GD)
color cLong = input.color(#00E5A8, "Long", group = GD, inline = "c")
color cShort = input.color(#FF4D6D, "Short", group = GD, inline = "c")
color cBrand = input.color(#B388FF, "Brand", group = GD, inline = "c")
// ═════════════════════════════════════════════════════════════════════════════
// SERIES (all ta.* run on every bar)
// ═════════════════════════════════════════════════════════════════════════════
float atr = ta.atr(atrLen)
float volMa = ta.sma(volume, 20)
float swLo = ta.lowest(low, sweepLen)[1]
float swHi = ta.highest(high, sweepLen)[1]
bool ready = barstate.isconfirmed and not na(atr) and atr > 0
bool volOk = volMult <= 0 or na(volume) or na(volMa) or volume >= volMa * volMult
bool bullBar = close > open
bool bearBar = close < open
bool sweepL = ready and not na(swLo) and low < swLo and close > swLo and bullBar and (close - low) > (high - close) and volOk
bool sweepS = ready and not na(swHi) and high > swHi and close < swHi and bearBar and (high - close) > (close - low) and volOk
// ═════════════════════════════════════════════════════════════════════════════
// TRADE ENGINE
// ═════════════════════════════════════════════════════════════════════════════
type Trade
int dir
float entry
float stop
float t1
float t2
float t3
float risk
int openBar
bool hit1
bool hit2
bool done
box bRisk
box bRwd
line lEnt
line lT1
line lT2
line lT3
label lTag
label lOut
var array<Trade> trades = array.new<Trade>()
// counters: 0 fired, 1 →TP1, 2 →TP3, 3 stop, 4 expired, 5 skipped (too wide)
var array<int> cnt = array.new_int(6, 0)
// sums of net R per trade: 0 = close everything at TP1, 1 = hold everything to TP3
var array<float> sumR = array.new_float(2, 0.0)
bump(int i) =>
array.set(cnt, i, array.get(cnt, i) + 1)
openCount() =>
int n = 0
for t in trades
if not t.done
n += 1
n
killDrawings(Trade t) =>
box.delete(t.bRisk)
box.delete(t.bRwd)
line.delete(t.lEnt)
line.delete(t.lT1)
line.delete(t.lT2)
line.delete(t.lT3)
label.delete(t.lTag)
label.delete(t.lOut)
drawTrade(Trade t) =>
int l = t.openBar
int r = t.openBar + zoneLen
if showZones
t.bRisk := box.new(l, math.max(t.entry, t.stop), r, math.min(t.entry, t.stop),
border_color = color.new(cShort, 10), bgcolor = color.new(cShort, 86), border_width = 1)
t.bRwd := box.new(l, math.max(t.entry, t.t3), r, math.min(t.entry, t.t3),
border_color = color.new(cLong, 100), bgcolor = color.new(cLong, 92))
t.lEnt := line.new(l, t.entry, r, t.entry, color = color.new(cBrand, 0), width = 2)
t.lT1 := line.new(l, t.t1, r, t.t1, color = cLong, style = line.style_dashed)
t.lT2 := line.new(l, t.t2, r, t.t2, color = color.new(cLong, 25), style = line.style_dashed)
t.lT3 := line.new(l, t.t3, r, t.t3, color = color.new(cLong, 45), style = line.style_dashed)
float off = atr * 0.9
t.lTag := label.new(bar_index, t.dir == 1 ? low - off : high + off, t.dir == 1 ? "▲ SWEEP" : "▼ SWEEP",
style = t.dir == 1 ? label.style_label_up : label.style_label_down,
color = t.dir == 1 ? cLong : cShort, textcolor = #0B0F14, size = size.tiny)
extendTrade(Trade t) =>
if showZones and not na(t.bRisk)
int r = bar_index + zoneLen
box.set_right(t.bRisk, r)
box.set_right(t.bRwd, r)
line.set_x2(t.lEnt, r)
line.set_x2(t.lT1, r)
line.set_x2(t.lT2, r)
line.set_x2(t.lT3, r)
closeTrade(Trade t, string why) =>
t.done := true
if t.hit1
bump(1)
if why == "TP3"
bump(2)
else if why == "stop"
bump(3)
else
bump(4)
// realised R per scenario, before cost
float markR = (t.dir == 1 ? close - t.entry : t.entry - close) / t.risk
float rTp1 = t.hit1 ? r1 : why == "stop" ? -1.0 : markR
float rTp3 = why == "TP3" ? r3 : why == "stop" ? -1.0 : markR
float costR = costPx / t.risk
array.set(sumR, 0, array.get(sumR, 0) + rTp1 - costR)
array.set(sumR, 1, array.get(sumR, 1) + rTp3 - costR)
if showZones and not na(t.bRisk)
box.set_right(t.bRisk, bar_index)
box.set_right(t.bRwd, bar_index)
line.set_x2(t.lEnt, bar_index)
line.set_x2(t.lT1, bar_index)
line.set_x2(t.lT2, bar_index)
line.set_x2(t.lT3, bar_index)
box.set_bgcolor(t.bRisk, color.new(cShort, 94))
box.set_bgcolor(t.bRwd, color.new(cLong, 96))
box.set_border_color(t.bRisk, color.new(cShort, 70))
float lvl = why == "stop" ? t.stop : why == "TP3" ? t.t3 : close
float sgn = why == "TP3" ? (t.dir == 1 ? 1.0 : -1.0) : (t.dir == 1 ? -1.0 : 1.0)
color col = why == "TP3" ? cLong : why == "stop" ? cShort : cBrand
t.lOut := label.new(bar_index, lvl + sgn * atr * 0.5, why,
style = label.style_none, textcolor = color.new(col, 20), size = size.tiny)
// ── advance open trades (a trade is first checked on the bar AFTER entry) ────
var int lastCloseBar = -100000
if ready and array.size(trades) > 0
for i = array.size(trades) - 1 to 0
Trade t = array.get(trades, i)
if not t.done and bar_index > t.openBar
bool hitStop = t.dir == 1 ? low <= t.stop : high >= t.stop
bool hit1 = t.dir == 1 ? high >= t.t1 : low <= t.t1
bool hit2 = t.dir == 1 ? high >= t.t2 : low <= t.t2
bool hit3 = t.dir == 1 ? high >= t.t3 : low <= t.t3
if hitStop // conservative: stop wins a same-bar tie
closeTrade(t, "stop")
else
if hit1 and not t.hit1
t.hit1 := true
line.set_style(t.lT1, line.style_solid)
line.set_width(t.lT1, 2)
if hit2 and not t.hit2
t.hit2 := true
line.set_style(t.lT2, line.style_solid)
line.set_width(t.lT2, 2)
if hit3
line.set_style(t.lT3, line.style_solid)
line.set_width(t.lT3, 2)
closeTrade(t, "TP3")
else if bar_index - t.openBar >= maxBars
closeTrade(t, "expired")
if t.done
lastCloseBar := bar_index
else
extendTrade(t)
// ── open a new trade ─────────────────────────────────────────────────────────
bool took = false
int tookDir = 0
if (sweepL or sweepS) and openCount() == 0 and bar_index - lastCloseBar >= reentry
int dir = sweepL ? 1 : -1
float wick = dir == 1 ? low : high
float stop = dir == 1 ? wick - wickBuf * atr : wick + wickBuf * atr
float risk = math.abs(close - stop)
if risk < minStop * atr
risk := minStop * atr
stop := dir == 1 ? close - risk : close + risk
if maxStop > 0 and risk > maxStop * atr
bump(5)
else
Trade t = Trade.new(dir = dir, entry = close, stop = stop,
t1 = close + dir * risk * r1, t2 = close + dir * risk * r2, t3 = close + dir * risk * r3,
risk = risk, openBar = bar_index, hit1 = false, hit2 = false, done = false)
drawTrade(t)
array.push(trades, t)
bump(0)
took := true
tookDir := dir
alert("SWEEP " + (dir == 1 ? "LONG" : "SHORT") + " @ " + str.tostring(close, format.mintick) +
" SL " + str.tostring(stop, format.mintick), alert.freq_once_per_bar_close)
// ── prune old drawings ───────────────────────────────────────────────────────
if barstate.isconfirmed
while array.size(trades) > keepN
Trade old = array.first(trades)
if old.done
killDrawings(old)
array.shift(trades)
else
break
// ═════════════════════════════════════════════════════════════════════════════
// PANEL
// ═════════════════════════════════════════════════════════════════════════════
tablePosOf() =>
switch tblPos
"Top left" => position.top_left
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
"Middle right" => position.middle_right
=> position.top_right
fmtR(float sum, int n) =>
string res = "—"
if n > 0
float e = sum / n
res := (e >= 0 ? "+" : "") + str.tostring(e, "#0.00") + "R"
res
pct(int a, int n) =>
n > 0 ? str.tostring(math.round(100.0 * a / n)) + "%" : "—"
varip bool merged = false
var table pnl = table.new(tablePosOf(), 2, 9, bgcolor = color.new(#0B0F14, 15),
border_width = 1, border_color = color.new(color.gray, 80), frame_color = color.new(cBrand, 40), frame_width = 1)
if barstate.islast and showPanel
int n = array.get(cnt, 0)
bool enough = (n - openCount()) >= minSamp
color tx = enough ? #C6CDD8 : color.new(#8A94A3, 20)
color hdr = color.new(cBrand, 70)
int nd = n - openCount()
float e1 = nd > 0 ? array.get(sumR, 0) / nd : na
float e3 = nd > 0 ? array.get(sumR, 1) / nd : na
table.cell(pnl, 0, 0, "SWEEP [pandich]", bgcolor = hdr, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(pnl, 1, 0, syminfo.ticker + " · " + timeframe.period, bgcolor = hdr, text_color = color.white, text_size = size.small)
table.cell(pnl, 0, 1, "trades", text_color = tx, text_size = size.small, text_halign = text.align_left)
table.cell(pnl, 1, 1, str.tostring(nd) + (array.get(cnt, 5) > 0 ? " (skipped " + str.tostring(array.get(cnt, 5)) + ")" : ""), text_color = tx, text_size = size.small)
table.cell(pnl, 0, 2, "→TP1", text_color = tx, text_size = size.small, text_halign = text.align_left)
table.cell(pnl, 1, 2, pct(array.get(cnt, 1), nd), text_color = tx, text_size = size.small)
table.cell(pnl, 0, 3, "→TP3", text_color = tx, text_size = size.small, text_halign = text.align_left)
table.cell(pnl, 1, 3, pct(array.get(cnt, 2), nd), text_color = tx, text_size = size.small)
table.cell(pnl, 0, 4, "stopped", text_color = tx, text_size = size.small, text_halign = text.align_left)
table.cell(pnl, 1, 4, pct(array.get(cnt, 3), nd), text_color = tx, text_size = size.small)
table.cell(pnl, 0, 5, "E @TP1 (net)", text_color = tx, text_size = size.small, text_halign = text.align_left)
table.cell(pnl, 1, 5, fmtR(array.get(sumR, 0), nd), text_color = not enough or na(e1) ? tx : e1 > 0 ? cLong : cShort, text_size = size.small)
table.cell(pnl, 0, 6, "E @TP3 (net)", text_color = tx, text_size = size.small, text_halign = text.align_left)
table.cell(pnl, 1, 6, fmtR(array.get(sumR, 1), nd), text_color = not enough or na(e3) ? tx : e3 > 0 ? cLong : cShort, text_size = size.small)
table.cell(pnl, 0, 7, openCount() > 0 ? "trade open" : "flat", text_color = openCount() > 0 ? color.white : color.new(#8A94A3, 20), text_size = size.small, text_halign = text.align_left)
table.cell(pnl, 1, 7, "", text_size = size.small)
table.cell(pnl, 0, 8, "E in R after cost " + str.tostring(costPx) + "; n<" + str.tostring(minSamp) + " = noise", text_color = color.new(#8A94A3, 20), text_size = size.tiny, text_halign = text.align_left)
table.cell(pnl, 1, 8, "", text_size = size.tiny)
if not merged
table.merge_cells(pnl, 0, 8, 1, 8)
merged := true
// ═════════════════════════════════════════════════════════════════════════════
// MARKERS AND ALERTS
// ═════════════════════════════════════════════════════════════════════════════
plotshape(took and tookDir == 1, "Long", shape.triangleup, location.belowbar, cLong, size = size.tiny)
plotshape(took and tookDir == -1, "Short", shape.triangledown, location.abovebar, cShort, size = size.tiny)
alertcondition(sweepL, "Sweep long", "Sweep [pandich]: long")
alertcondition(sweepS, "Sweep short", "Sweep [pandich]: short")