本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// Sweep & Trend Strategy (single pane)
// Derived from: Smart Money Bull/Bear Trap Detector (BigBeluga, CC BY-NC-SA 4.0) and Dynamic RSI Trend (NJ_Algo, MPL 2.0).
// Because of the BigBeluga source, share under CC BY-NC-SA 4.0, non-commercial.
//@version=6
strategy("Sweep & Trend", "SWT", overlay = true, initial_capital = 10000,
default_qty_type = strategy.percent_of_equity, default_qty_value = 10,
commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 2,
pyramiding = 0, max_bars_back = 500, max_boxes_count = 500, max_labels_count = 500)
// ─── Inputs ──────────────────────────────────────────────────────────────
string G1 = "Regime (Dynamic RSI Trend)"
int rsiLen = input.int(14, "RSI length", minval = 2, group = G1)
int rsiSmooth = input.int(20, "RSI smoothing", minval = 1, group = G1)
int atrSmooth = input.int(50, "Volatility smoothing", minval = 1, group = G1)
int thr1 = input.int(54, "Threshold 1", group = G1)
int thr2 = input.int(45, "Threshold 2", group = G1)
string G2 = "Sweep trigger"
int pLegs = input.int(5, "Pivot strength (bars each side)", minval = 2, group = G2)
int zLen = input.int(40, "Zone life (bars)", minval = 10, group = G2)
int zSegs = input.int(6, "Gradient segments", minval = 2, maxval = 10, group = G2)
float vThresh = input.float(1.2, "High volume multiplier", minval = 0.5, step = 0.1, group = G2)
string minRank = input.string("C", "Minimum rank to trade", options = ["C", "B", "A"], group = G2)
bool flipEntry = input.bool(true, "Also enter when regime flips", group = G2)
string G3 = "Risk"
float rr = input.float(2.0, "Reward : risk", minval = 0.5, step = 0.1, group = G3)
float stopBuf = input.float(0.25, "Stop buffer (ATR)", minval = 0.0, step = 0.05, group = G3)
float maxStop = input.float(3.0, "Max stop distance (ATR)", minval = 0.5, step = 0.1, group = G3)
bool exitFlip = input.bool(true, "Exit when regime flips", group = G3)
bool longOn = input.bool(true, "Longs", group = G3)
bool shortOn = input.bool(true, "Shorts", group = G3)
string G4 = "Visuals"
int lineLen = input.int(21, "Trend line length", minval = 2, group = G4)
float glowW = input.float(1.5, "Gradient reach (ATR)", minval = 0.2, step = 0.1, group = G4)
bool dispCandles = input.bool(true, "Color candles by regime", group = G4)
bool showZones = input.bool(true, "Show pivot zones", group = G4)
bool showDash = input.bool(true, "Show dashboard", group = G4)
string dPos = input.string("top_right", "Dashboard position", options = ["top_left", "top_right", "bottom_left", "bottom_right", "middle_right"], group = G4)
string txtSz = input.string("small", "Text size", options = ["tiny", "small", "normal"], group = G4)
color bullCol = input.color(#00F5D4, "Bull", inline = "c", group = G4)
color bearCol = input.color(#FF2E63, "Bear", inline = "c", group = G4)
color cA = input.color(#FFE600, "Rank A", inline = "r", group = G4)
color cB = input.color(#FF9F1C, "B", inline = "r", group = G4)
color cC = input.color(#E76F51, "C", inline = "r", group = G4)
int minRk = minRank == "A" ? 3 : minRank == "B" ? 2 : 1
// ─── Regime: adaptive RSI trend (calculated only, not plotted) ───────────
float rsi = ta.rsi(close, rsiLen)
float smoothRsi = ta.ema(rsi, rsiSmooth)
float rsiTR = math.abs(smoothRsi - smoothRsi[1])
float rsiATR = ta.rma(rsiTR, 14)
float avgATR = ta.sma(rsiATR, atrSmooth)
float atrDelta = nz(rsiATR - avgATR)
float adapThr1 = math.max(0.0, math.min(100.0, thr1 + atrDelta * 4))
float adapThr2 = math.max(0.0, math.min(100.0, thr2 + atrDelta * 4))
var int trend = 0
if ta.crossover(smoothRsi, adapThr1) or ta.crossover(smoothRsi, adapThr2)
trend := 1
else if ta.crossunder(smoothRsi, adapThr2) or ta.crossunder(smoothRsi, adapThr1)
trend := -1
// ─── Sweep levels ────────────────────────────────────────────────────────
type Pvl
float price
int bIdx
bool isHigh
bool trig
type Sig
bool isHigh
string rk
int tBar
bool traded
var Pvl[] activeL = array.new<Pvl>()
var Sig[] sigs = array.new<Sig>()
float pH = ta.pivothigh(high, pLegs, pLegs)
float pL = ta.pivotlow(low, pLegs, pLegs)
float vMa = ta.sma(volume, 20)
float atr = ta.atr(14)
float lo10 = ta.lowest(low, 10)
float hi10 = ta.highest(high, 10)
// fading gradient zone: strongest at the pivot, fading to the right
drawZone(float lvl, int sBar, bool isHigh, float rng) =>
int sLen = math.max(1, math.floor(zLen / zSegs))
color c = isHigh ? bearCol : bullCol
float t = isHigh ? lvl : lvl + rng * 0.15
float b = isHigh ? lvl - rng * 0.15 : lvl
for i = 0 to zSegs - 1
int x1 = sBar + i * sLen
int tr = math.floor(30 + i * (70.0 / zSegs))
box.new(x1, t, x1 + sLen, b, xloc = xloc.bar_index, bgcolor = color.new(c, tr), border_color = na)
if not na(pH)
int pBar = bar_index - pLegs
if showZones
drawZone(pH, pBar, true, high[pLegs] - low[pLegs])
activeL.push(Pvl.new(pH, pBar, true, false))
if not na(pL)
int pBar = bar_index - pLegs
if showZones
drawZone(pL, pBar, false, high[pLegs] - low[pLegs])
activeL.push(Pvl.new(pL, pBar, false, false))
bool bullSweep = false
bool bearSweep = false
if barstate.isconfirmed and activeL.size() > 0
for i = activeL.size() - 1 to 0
Pvl lvl = activeL.get(i)
int age = bar_index - lvl.bIdx
if lvl.trig or age > zLen
activeL.remove(i)
continue
// wick through the pivot, close back on the original side
bool hit = lvl.isHigh ? (high > lvl.price and close < lvl.price) : (low < lvl.price and close > lvl.price)
if hit
lvl.trig := true
int ageTerm = age <= zLen / 3 ? 2 : age <= (2 * zLen) / 3 ? 1 : 0
int volTerm = volume > vMa * vThresh ? 2 : volume > vMa ? 1 : 0
int pts = ageTerm + volTerm
string rk = pts >= 3 ? "A" : pts == 2 ? "B" : "C"
int rkN = pts >= 3 ? 3 : pts == 2 ? 2 : 1
bool aligned = lvl.isHigh ? trend == -1 : trend == 1
bool traded = aligned and rkN >= minRk and (lvl.isHigh ? shortOn : longOn)
if traded
if lvl.isHigh
bearSweep := true
else
bullSweep := true
sigs.push(Sig.new(lvl.isHigh, rk, bar_index, traded))
color sc = rk == "A" ? cA : rk == "B" ? cB : cC
float sp = lvl.isHigh ? high : low
string tip = (lvl.isHigh ? "Bear sweep" : "Bull sweep") + " · rank " + rk + "\nAge " + str.tostring(age) + "b · score " + str.tostring(pts) + (traded ? "\nTRADED" : "\nfiltered by regime/rank")
label.new(bar_index, sp, lvl.isHigh ? rk + "\n▼" : "▲\n" + rk, style = lvl.isHigh ? label.style_label_down : label.style_label_up,
color = color.new(color.white, 100), textcolor = traded ? sc : color.new(sc, 65), size = size.normal, tooltip = tip)
// ─── Entries and exits ───────────────────────────────────────────────────
bool ready = not na(atr) and not na(smoothRsi)
bool flipLong = trend == 1 and trend[1] != 1
bool flipShort = trend == -1 and trend[1] != -1
bool goLong = longOn and ready and barstate.isconfirmed and trend == 1 and (bullSweep or (flipEntry and flipLong))
bool goShort = shortOn and ready and barstate.isconfirmed and trend == -1 and (bearSweep or (flipEntry and flipShort))
var float lStop = na
var float lTp = na
var float sStop = na
var float sTp = na
if goLong and strategy.position_size <= 0
float ref = bullSweep ? low : lo10
float stp = math.max(ref - stopBuf * atr, close - maxStop * atr)
stp := stp >= close ? close - 2 * atr : stp
lStop := stp
lTp := close + rr * (close - stp)
strategy.entry("Long", strategy.long)
if goShort and strategy.position_size >= 0
float ref = bearSweep ? high : hi10
float stp = math.min(ref + stopBuf * atr, close + maxStop * atr)
stp := stp <= close ? close + 2 * atr : stp
sStop := stp
sTp := close - rr * (stp - close)
strategy.entry("Short", strategy.short)
if strategy.position_size > 0 or goLong
strategy.exit("Exit L", "Long", stop = lStop, limit = lTp)
if strategy.position_size < 0 or goShort
strategy.exit("Exit S", "Short", stop = sStop, limit = sTp)
if exitFlip and barstate.isconfirmed
if strategy.position_size > 0 and trend == -1 and not goShort
strategy.close("Long", comment = "regime flip")
if strategy.position_size < 0 and trend == 1 and not goLong
strategy.close("Short", comment = "regime flip")
// ─── Visuals: glowing trend line with gradient, candles, trade lines ─────
color sigCol = trend == 1 ? bullCol : trend == -1 ? bearCol : color.gray
float tl = ta.ema(close, lineLen)
float edge = trend == 0 ? na : tl - trend * atr * glowW
float hiV = math.max(tl, nz(edge, tl))
float loV = math.min(tl, nz(edge, tl))
// gradient is strongest at the line and fades toward the far edge
color topC = trend == 1 ? color.new(sigCol, 55) : color.new(sigCol, 100)
color botC = trend == 1 ? color.new(sigCol, 100) : color.new(sigCol, 55)
// glow: wide faint strokes under the main line
plot(tl, "Glow outer", color.new(sigCol, 88), 10)
plot(tl, "Glow mid", color.new(sigCol, 75), 6)
pLine = plot(tl, "Trend line", sigCol, 2)
pEdge = plot(edge, "Gradient edge", color = na)
fill(pLine, pEdge, hiV, loV, topC, botC, "Trend gradient")
barcolor(dispCandles ? sigCol : na)
plotshape(goLong and strategy.position_size <= 0, "Long entry", shape.triangleup, location.belowbar, bullCol, size = size.small)
plotshape(goShort and strategy.position_size >= 0, "Short entry", shape.triangledown, location.abovebar, bearCol, size = size.small)
plot(strategy.position_size > 0 ? lStop : na, "Long stop", color.new(bearCol, 20), 1, plot.style_linebr)
plot(strategy.position_size > 0 ? lTp : na, "Long target", color.new(bullCol, 20), 1, plot.style_linebr)
plot(strategy.position_size < 0 ? sStop : na, "Short stop", color.new(bearCol, 20), 1, plot.style_linebr)
plot(strategy.position_size < 0 ? sTp : na, "Short target", color.new(bullCol, 20), 1, plot.style_linebr)
// ─── Dashboard ───────────────────────────────────────────────────────────
color dBg = #0B0E14
color dBrd = #2A3447
color dHdr = #94A3B8
var table dash = table.new(dPos, 3, 9, bgcolor = dBg, border_width = 1, border_color = dBrd, frame_width = 1, frame_color = dBrd)
if showDash and barstate.islast
int nT = strategy.closedtrades
string wr = nT > 0 ? str.tostring(100.0 * strategy.wintrades / nT, "#.#") + "%" : "—"
string reg = trend == 1 ? "BULL" : trend == -1 ? "BEAR" : "WAIT"
table.cell(dash, 0, 0, "REGIME", text_color = dHdr, text_size = txtSz)
table.cell(dash, 1, 0, reg, text_color = sigCol, text_size = txtSz, text_halign = text.align_center)
table.cell(dash, 2, 0, "RSI " + str.tostring(smoothRsi, "#.#"), text_color = dHdr, text_size = txtSz)
table.cell(dash, 0, 1, "TRADES", text_color = dHdr, text_size = txtSz)
table.cell(dash, 1, 1, str.tostring(nT), text_color = color.white, text_size = txtSz)
table.cell(dash, 2, 1, "WIN " + wr, text_color = color.white, text_size = txtSz)
table.cell(dash, 0, 2, "NET P/L", text_color = dHdr, text_size = txtSz)
table.cell(dash, 1, 2, str.tostring(strategy.netprofit, "#.##"), text_color = strategy.netprofit >= 0 ? bullCol : bearCol, text_size = txtSz)
table.cell(dash, 2, 2, "", text_size = txtSz)
table.cell(dash, 0, 3, "SWEEP", text_color = dHdr, text_size = txtSz)
table.cell(dash, 1, 3, "RANK", text_color = dHdr, text_size = txtSz)
table.cell(dash, 2, 3, "AGE", text_color = dHdr, text_size = txtSz)
for r = 4 to 8
table.cell(dash, 0, r, "", text_size = txtSz)
table.cell(dash, 1, r, "", text_size = txtSz)
table.cell(dash, 2, r, "", text_size = txtSz)
int tot = sigs.size()
if tot > 0
int row = 4
for i = tot - 1 to math.max(0, tot - 5)
Sig s = sigs.get(i)
color tc = s.isHigh ? bearCol : bullCol
color rc = s.rk == "A" ? cA : s.rk == "B" ? cB : cC
color bg = color.new(tc, s.traded ? 85 : 96)
table.cell(dash, 0, row, (s.isHigh ? "▼ BEAR" : "▲ BULL") + (s.traded ? " ✓" : ""), text_color = tc, bgcolor = bg, text_size = txtSz)
table.cell(dash, 1, row, "[" + s.rk + "]", text_color = rc, bgcolor = bg, text_size = txtSz)
table.cell(dash, 2, row, str.tostring(bar_index - s.tBar) + "b ago", text_color = #CBD5E1, bgcolor = bg, text_size = txtSz)
row += 1