本页源码来自 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 Target Hit [Zen]", overlay = true, max_labels_count = 400, max_boxes_count = 200)
// ---------- 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.")
timeout = input.int(48, "Target timeout (candles)", minval = 5, maxval = 500, group = gP, display = display.none, tooltip = "If the target is not reached within this many candles after the sweep candle, the sweep counts as MISS.")
gV = "Display"
showBoxes = input.bool(true, "Show target boxes", group = gV, display = display.none, tooltip = "A box from the sweep candle to the target: pale while the target is open, filled after a HIT. A MISS leaves only a small grey MISS tag on the sweep candle.")
tblPosIn = input.string("Top right", "Table position", options = ["Top right", "Bottom right", "Bottom left", "Top left"], group = gV, display = display.none)
// ---------- 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 cTXT = isLight ? #131722 : #D1D4DC
color cINK = isLight ? #FFFFFF : #131722
color cBG = isLight ? color.new(#FFFFFF, 8) : color.new(#131722, 8)
color cHEAD = isLight ? color.new(#647482, 80) : color.new(#8391A4, 75)
tblPos = tblPosIn == "Top right" ? position.top_right : tblPosIn == "Bottom right" ? position.bottom_right : tblPosIn == "Bottom left" ? position.bottom_left : position.top_left
// ---------- 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: its target is already touched
bool swLonly = swL and not swH
// ---------- State ----------
var int[] pBar = array.new_int()
var int[] pSide = array.new_int() // 1 = high sweep, target below; 2 = low sweep, target above
var float[] pTgt = array.new_float()
var float[] pExt = array.new_float() // extreme of the sweep candle
var box[] pBox = array.new<box>()
var label[] pLbl = array.new<label>()
var int cntH = 0
var int hitH = 0
var int cntL = 0
var int hitL = 0
var int[] durH = array.new_int()
var int[] durL = array.new_int()
var table tbl = na
bool anyHit = false
bool anyMiss = false
bool anyNew = false
string msg = ""
if barstate.isconfirmed
// ----- resolve open targets -----
int k = pBar.size() - 1
while k >= 0
int side = pSide.get(k)
float tgt = pTgt.get(k)
float ext = pExt.get(k)
int age = bar_index - pBar.get(k)
bool inval = side == 1 ? high > ext : low < ext
bool reach = side == 1 ? low <= tgt : high >= tgt
bool again = side == 1 ? swHonly : swLonly // the same side is swept again: the new sweep replaces this one
int res = inval ? (again ? 3 : 2) : reach ? 1 : age >= timeout ? 2 : 0
color c = side == 1 ? cHI : cLO
string sideTxt = side == 1 ? "high sweep" : "low sweep"
if res > 0
box bx = pBox.get(k)
label lb = pLbl.get(k)
if res == 1
if side == 1
cntH += 1
hitH += 1
durH.push(age)
else
cntL += 1
hitL += 1
durL.push(age)
if not na(bx)
box.set_right(bx, bar_index)
box.set_bgcolor(bx, color.new(c, 85))
box.set_border_color(bx, color.new(c, 40))
label.set_text(lb, "HIT " + str.tostring(age))
label.set_color(lb, c)
label.set_textcolor(lb, cINK)
label.set_size(lb, size.normal)
label.set_tooltip(lb, sideTxt + "\ntarget " + str.tostring(tgt, format.mintick) + "\nreached in " + str.tostring(age) + " candles")
anyHit := true
msg += "Target HIT: " + sideTxt + " | " + str.tostring(age) + " candles\n"
else
if not na(bx)
box.delete(bx)
if res == 2
if side == 1
cntH += 1
else
cntL += 1
string why = inval ? "price went back beyond the sweep extreme" : "timeout " + str.tostring(timeout) + " candles"
label.set_text(lb, "MISS")
label.set_color(lb, color.new(cMUTED, 100))
label.set_textcolor(lb, cMUTED)
label.set_size(lb, size.tiny)
label.set_tooltip(lb, sideTxt + "\ntarget " + str.tostring(tgt, format.mintick) + "\n" + why + "\nafter " + str.tostring(age) + " candles")
anyMiss := true
msg += "Target MISS: " + sideTxt + " | " + why + "\n"
else
label.delete(lb)
pBar.remove(k)
pSide.remove(k)
pTgt.remove(k)
pExt.remove(k)
pBox.remove(k)
pLbl.remove(k)
else
box bx = pBox.get(k)
if not na(bx)
box.set_right(bx, bar_index)
k -= 1
// ----- new sweeps on this candle -----
if swHonly
pBar.push(bar_index)
pSide.push(1)
pTgt.push(ll)
pExt.push(high)
pBox.push(showBoxes ? box.new(bar_index, high, bar_index, ll, border_color = color.new(cHI, 70), border_width = 1, border_style = line.style_solid, bgcolor = color.new(cHI, 92)) : na)
pLbl.push(label.new(bar_index, high, "...", style = label.style_label_down, color = color.new(cHI, 100), textcolor = cHI, size = size.tiny, tooltip = "high sweep\ntarget " + str.tostring(ll, format.mintick) + "\nwaiting"))
anyNew := true
msg += "High sweep, target set at " + str.tostring(ll, format.mintick) + "\n"
if swLonly
pBar.push(bar_index)
pSide.push(2)
pTgt.push(hh)
pExt.push(low)
pBox.push(showBoxes ? box.new(bar_index, hh, bar_index, low, border_color = color.new(cLO, 70), border_width = 1, border_style = line.style_solid, bgcolor = color.new(cLO, 92)) : na)
pLbl.push(label.new(bar_index, low, "...", style = label.style_label_up, color = color.new(cLO, 100), textcolor = cLO, size = size.tiny, tooltip = "low sweep\ntarget " + str.tostring(hh, format.mintick) + "\nwaiting"))
anyNew := true
msg += "Low sweep, target set at " + str.tostring(hh, format.mintick) + "\n"
// ---------- Alerts ----------
if barstate.isconfirmed and msg != ""
alert(msg + syminfo.ticker + " " + timeframe.period, alert.freq_once_per_bar_close)
alertcondition(anyHit, "Target hit", "Target hit on {{ticker}} {{interval}}")
alertcondition(anyMiss, "Target missed", "Target missed on {{ticker}} {{interval}}")
alertcondition(anyNew, "New sweep, target set", "New sweep target on {{ticker}} {{interval}}")
// ---------- Table ----------
f_pct(int h, int n) => n == 0 ? "-" : str.tostring(100.0 * h / n, "0") + "%"
f_med(int[] a) => a.size() == 0 ? "-" : str.tostring(array.median(a), "0")
if barstate.islast
if not na(tbl)
table.delete(tbl)
tbl := table.new(tblPos, 4, 5, bgcolor = cBG, border_width = 0, frame_width = 0)
table.cell(tbl, 0, 0, "SWEEP TARGET HIT · " + timeframe.period + " · timeout " + str.tostring(timeout), text_color = cTXT, text_size = size.small, text_halign = text.align_left, bgcolor = cHEAD)
table.merge_cells(tbl, 0, 0, 3, 0)
table.cell(tbl, 0, 1, "after sweep", text_color = cMUTED, text_size = size.tiny, text_halign = text.align_left)
table.cell(tbl, 1, 1, "n", text_color = cMUTED, text_size = size.tiny, text_halign = text.align_right)
table.cell(tbl, 2, 1, "HIT", text_color = cMUTED, text_size = size.tiny, text_halign = text.align_right)
table.cell(tbl, 3, 1, "median candles", text_color = cMUTED, text_size = size.tiny, text_halign = text.align_right)
table.cell(tbl, 0, 2, "High sweep to low pool", text_color = cHI, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 2, str.tostring(cntH), text_color = cTXT, text_size = size.small, text_halign = text.align_right)
table.cell(tbl, 2, 2, f_pct(hitH, cntH), text_color = cTXT, text_size = size.small, text_halign = text.align_right)
table.cell(tbl, 3, 2, f_med(durH), text_color = cTXT, text_size = size.small, text_halign = text.align_right)
table.cell(tbl, 0, 3, "Low sweep to high pool", text_color = cLO, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 3, str.tostring(cntL), text_color = cTXT, text_size = size.small, text_halign = text.align_right)
table.cell(tbl, 2, 3, f_pct(hitL, cntL), text_color = cTXT, text_size = size.small, text_halign = text.align_right)
table.cell(tbl, 3, 3, f_med(durL), text_color = cTXT, text_size = size.small, text_halign = text.align_right)
table.cell(tbl, 0, 4, str.tostring(pBar.size()) + " open · loaded history · historical data, not a forecast", text_color = cMUTED, text_size = size.tiny, text_halign = text.align_left)
table.merge_cells(tbl, 0, 4, 3, 4)