本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartZenFX
indicator("Sweep Entry Zone [Zen]", overlay = true, max_boxes_count = 50)
// ---------- Inputs ----------
gP = "Pool & sweep"
poolLen = input.int(20, "Pool length (candles)", minval = 5, group = gP, display = display.none, tooltip = "Pool = highest high / lowest low of the previous N closed candles. Sweep = wick beyond the pool and close back inside, on a closed candle.")
zoneLife = input.int(48, "Zone life (candles)", minval = 5, maxval = 500, group = gP, display = display.none, tooltip = "One limit for two waits: how long a sweep waits for its BOS, and how long a zone stays live after the BOS. After that the zone freezes as it is.")
gV = "Display"
keepBroken = input.bool(false, "Keep broken zones", group = gV, display = display.none, tooltip = "A broken zone (a candle closed beyond its far edge) stays on the chart in grey. Off = it is removed.")
// ---------- ChartZenFX palette (auto light / dark) ----------
bool isLight = (color.r(chart.bg_color) + color.g(chart.bg_color) + color.b(chart.bg_color)) / 3 > 128
color cHI = isLight ? #AD4A12 : #D9703A
color cLO = isLight ? #1B46B4 : #6F88EE
color cMUTED = isLight ? #647482 : #8391A4
color cACC = #C98B78
int fillU = isLight ? 85 : 78 // untested
int bordU = isLight ? 60 : 50
int fillT = isLight ? 70 : 62 // tested
int bordT = isLight ? 25 : 20
int fillB = isLight ? 92 : 90 // broken
int bordB = isLight ? 85 : 80
int accT = isLight ? 60 : 70 // BOS candle background
// ---------- Pool and sweep (closed candles) ----------
float hh = ta.highest(high, poolLen)[1]
float ll = ta.lowest(low, poolLen)[1]
bool ok = bar_index >= poolLen
bool swH = ok and high > hh and close <= hh
bool swL = ok and low < ll and close >= ll
bool swHonly = swH and not swL // a candle that sweeps both pools is skipped
bool swLonly = swL and not swH
// ---------- Pending sweeps (one per side, waiting for BOS) ----------
var int pHbar = na
var float pHhi = na
var float pHlo = na
var float pHbody = na
var int pLbar = na
var float pLhi = na
var float pLlo = na
var float pLbody = na
// ---------- Zones ----------
var int[] zSide = array.new_int() // 1 = after a high sweep (zone above price), 2 = after a low sweep (zone below)
var int[] zBosBar = array.new_int()
var float[] zTop = array.new_float()
var float[] zBot = array.new_float()
var int[] zStat = array.new_int() // 0 untested, 1 tested, 2 broken
var bool[] zLive = array.new_bool()
var box[] zBox = array.new<box>()
bool bosNow = false
bool anySet = false
bool anyTest = false
bool anyBreak = false
string msg = ""
f_remove(int k) =>
zSide.remove(k)
zBosBar.remove(k)
zTop.remove(k)
zBot.remove(k)
zStat.remove(k)
zLive.remove(k)
zBox.remove(k)
f_addZone(int side, int swBar, float top, float bot) =>
// keep the arrays within the drawing limit: the oldest zone goes first
if zSide.size() >= 50
box.delete(zBox.get(0))
f_remove(0)
float t = top
float b = bot
if t - b < syminfo.mintick // a sweep candle with no wick-to-body gap: keep the zone one tick tall
if side == 1
b := t - syminfo.mintick
else
t := b + syminfo.mintick
color c = side == 1 ? cHI : cLO
float near = side == 1 ? b : t
// the tag sits inside the zone at its far edge: top for a zone above price, bottom for a zone below
box bx = box.new(swBar, t, bar_index + 1, b, border_color = color.new(c, bordU), border_width = 1, border_style = line.style_solid, bgcolor = color.new(c, fillU), text = str.tostring(near, format.mintick), text_size = size.small, text_color = c, text_halign = text.align_right, text_valign = side == 1 ? text.align_top : text.align_bottom)
zSide.push(side)
zBosBar.push(bar_index)
zTop.push(t)
zBot.push(b)
zStat.push(0)
zLive.push(true)
zBox.push(bx)
near
if barstate.isconfirmed
// ----- live zones: broken / tested / extend -----
int k = zSide.size() - 1
while k >= 0
if zLive.get(k) and zBosBar.get(k) < bar_index
int side = zSide.get(k)
float top = zTop.get(k)
float bot = zBot.get(k)
float near = side == 1 ? bot : top
color c = side == 1 ? cHI : cLO
box bx = zBox.get(k)
int life = bar_index - zBosBar.get(k)
string sideTxt = side == 1 ? "high sweep" : "low sweep"
string px = str.tostring(near, format.mintick)
bool broken = side == 1 ? close > top : close < bot
bool inZone = side == 1 ? high >= bot : low <= top
if broken
zStat.set(k, 2)
zLive.set(k, false)
anyBreak := true
msg += "Zone broken: " + sideTxt + " | " + str.tostring(bot, format.mintick) + " – " + str.tostring(top, format.mintick) + "\n"
if keepBroken
box.set_right(bx, bar_index)
box.set_bgcolor(bx, color.new(cMUTED, fillB))
box.set_border_color(bx, color.new(cMUTED, bordB))
box.set_text(bx, "BROKEN")
box.set_text_color(bx, cMUTED)
box.set_text_size(bx, size.tiny)
else
box.delete(bx)
f_remove(k)
else
if inZone and zStat.get(k) == 0
zStat.set(k, 1)
anyTest := true
msg += "Zone tested: " + sideTxt + " | " + px + "\n"
box.set_bgcolor(bx, color.new(c, fillT))
box.set_border_color(bx, color.new(c, bordT))
box.set_text(bx, "TESTED " + px)
if life >= zoneLife
zLive.set(k, false)
box.set_right(bx, bar_index)
box.set_text_size(bx, size.tiny)
else
box.set_right(bx, bar_index + 1)
k -= 1
// ----- pending high sweep: cancelled / BOS / timed out -----
if not na(pHbar)
if high > pHhi
pHbar := na // price went back above the sweep high
else if close < pHlo
float e = f_addZone(1, pHbar, pHhi, pHbody)
bosNow := true
anySet := true
msg += "Zone set: high sweep | edge " + str.tostring(e, format.mintick) + "\n"
pHbar := na
else if bar_index - pHbar >= zoneLife
pHbar := na
// ----- pending low sweep -----
if not na(pLbar)
if low < pLlo
pLbar := na
else if close > pLhi
float e = f_addZone(2, pLbar, pLbody, pLlo)
bosNow := true
anySet := true
msg += "Zone set: low sweep | edge " + str.tostring(e, format.mintick) + "\n"
pLbar := na
else if bar_index - pLbar >= zoneLife
pLbar := na
// ----- new sweeps on this candle (a new same-side sweep replaces the pending one) -----
if swHonly
pHbar := bar_index
pHhi := high
pHlo := low
pHbody := math.max(open, close)
if swLonly
pLbar := bar_index
pLhi := high
pLlo := low
pLbody := math.min(open, close)
// ---------- BOS candle accent ----------
bgcolor(bosNow ? color.new(cACC, accT) : na, title = "BOS candle")
// ---------- Alerts ----------
if barstate.isconfirmed and msg != ""
alert(msg + syminfo.ticker + " " + timeframe.period, alert.freq_once_per_bar_close)
alertcondition(anySet, "Zone set", "Zone set on {{ticker}} {{interval}}")
alertcondition(anyTest, "Zone tested", "Zone tested on {{ticker}} {{interval}}")
alertcondition(anyBreak, "Zone broken", "Zone broken on {{ticker}} {{interval}}")