Sweep Cancel Tracker [Zen]

ChartZenFX · study · 153 行 · 点赞 24 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// Sweep Cancel Tracker [Zen]
// What happens to a liquidity sweep BEFORE structure breaks. Counts the sweeps that were
// invalidated, timed out or replaced, against the ones that produced a break of structure.
// One symbol, closed candles only, open source.
indicator("Sweep Cancel Tracker [Zen]", overlay = true, max_labels_count = 500)
 
// ----------------------------------------------------------------- inputs
string gM = "Model"
int poolLen = input.int(20, "Pool length (candles)", minval = 5, group = gM, tooltip = "The pool is the highest high and the lowest low of this many closed candles ending just before the sweep candle.")
int winBars = input.int(12, "Setup window (candles after sweep)", minval = 1, maxval = 200, group = gM, tooltip = "Structure must break while the sweep is younger than this. A sweep that reaches this age with nothing decided closes as NO BOS.")
 
string gV = "Display"
bool   showLbl = input.bool(true, "Label every resolved sweep", group = gV)
bool   showTbl = input.bool(true, "Summary table", group = gV)
string posIn   = input.string("Bottom right", "Table position", options = ["Bottom right", "Bottom left", "Top right", "Top left"], group = gV)
string txtIn   = input.string("Normal", "Text size", options = ["Small", "Normal", "Large"], group = gV)
 
// ----------------------------------------------------------------- helpers
f_pct(int part, int whole) =>
    whole <= 0 ? "-" : str.tostring(100.0 * part / whole, "0.0") + "%"
 
f_avg(int total, int n) =>
    n <= 0 ? "-" : str.tostring(total * 1.0 / n, "0.0")
 
f_cell(table tb, int c, int r, string t, color col, color bg, string sz) =>
    table.cell(tb, c, r, t, text_color = col, bgcolor = bg, text_size = sz, text_halign = text.align_left, text_font_family = font.family_monospace)
 
// ----------------------------------------------------------------- closed-candle inputs
float cH = high[1]
float cL = low[1]
float cC = close[1]
float hh = ta.highest(high, poolLen)[2]
float ll = ta.lowest(low,  poolLen)[2]
 
bool ready = bar_index > poolLen + 2
bool sH    = ready and cH > hh and cC <= hh      // high sweep: wick above the pool, close back inside
bool sL    = ready and cL < ll and cC >= ll      // low sweep:  wick below the pool, close back inside
bool newSw = (sH or sL) and not (sH and sL)      // a candle taking out both pools is not a sweep
 
// ----------------------------------------------------------------- state
var int   side  = 0      // 1 high sweep, -1 low sweep, 0 nothing tracked
var int   age   = 0      // closed candles since the sweep candle
var int   swIdx = na     // bar index of the sweep candle
var float ext   = na     // the sweep extreme: the invalidation level
var float trig  = na     // opposite end of the sweep candle: a close beyond it is the break of structure
 
// ----------------------------------------------------------------- census
var int cCancel   = 0      // invalidated before any break of structure
var int ageCancel = 0      // their ages, summed
var int cNoBos  = 0                             // setup window ran out, nothing decided
var int cReset  = 0                             // a fresh sweep arrived first
var int cBos      = 0      // structure broke
var int ageBos    = 0      // their ages, summed
 
// 0 open, 1 CANCEL, 2 NO BOS, 3 RESET, 4 BOS
int done = 0
 
if side != 0
    age += 1
    bool beyond = side == 1 ? cH > ext  : cL < ext
    bool brk    = side == 1 ? cC < trig : cC > trig
    // Invalidation is read before the break, so a candle carrying both counts as CANCEL:
    // without intrabar data the order of the two inside that candle is unknown.
    if beyond and not newSw
        done := 1
    else if brk
        done := 4
    else if age >= winBars
        done := 2
    else if newSw
        // A fresh sweep replaces an unresolved one. It gets counted, not dropped.
        done := 3
 
if done == 1
    cCancel   += 1
    ageCancel += age
if done == 2
    cNoBos += 1
if done == 3
    cReset += 1
if done == 4
    cBos    += 1
    ageBos  += age
 
if done != 0 and showLbl and not na(swIdx)
    string lt = done == 1 ? "CANCEL" : done == 2 ? "NO BOS" : done == 3 ? "RESET" : "BOS"
    color  lc = done == 4 ? color.new(#2E7D32, 20) : done == 1 ? color.new(#B23B3B, 20) : color.new(#9AA0A6, 25)
    label.new(swIdx, na, lt, xloc = xloc.bar_index,
         yloc      = side == 1 ? yloc.abovebar : yloc.belowbar,
         style     = side == 1 ? label.style_label_down : label.style_label_up,
         color     = lc,
         textcolor = color.white,
         size      = size.tiny)
 
if done != 0
    side := 0
 
if newSw
    side  := sH ? 1 : -1
    age   := 0
    swIdx := bar_index - 1
    ext   := sH ? cH : cL
    trig  := sH ? cL : cH
 
// ----------------------------------------------------------------- summary table
string sz = txtIn == "Small" ? size.small : txtIn == "Large" ? size.large : size.normal
var table tb = table.new(
     posIn == "Bottom right" ? position.bottom_right : posIn == "Bottom left" ? position.bottom_left : posIn == "Top right" ? position.top_right : position.top_left,
     3, 9, border_width = 1, frame_width = 1, frame_color = color.new(color.gray, 60), border_color = color.new(color.gray, 60))
 
if barstate.islast and showTbl
    int resolved = cCancel + cNoBos + cReset + cBos
    int noStruct = cCancel + cNoBos + cReset
 
    color cTXT  = chart.fg_color
    color cBG   = color.new(chart.bg_color, 10)
    color cHEAD = color.new(color.gray, 85)
 
    table.clear(tb, 0, 0, 2, 8)
 
    table.merge_cells(tb, 0, 0, 2, 0)
    f_cell(tb, 0, 0, "SWEEP CANCEL TRACKER · " + syminfo.ticker + " · " + timeframe.period + " · " + str.tostring(resolved) + " resolved sweeps", cTXT, cHEAD, sz)
 
    f_cell(tb, 0, 1, "BEFORE STRUCTURE BROKE", cTXT, cHEAD, sz)
    f_cell(tb, 1, 1, "N",     cTXT, cHEAD, sz)
    f_cell(tb, 2, 1, "SHARE", cTXT, cHEAD, sz)
 
    f_cell(tb, 0, 2, "CANCEL  back beyond the sweep extreme", cTXT, cBG, sz)
    f_cell(tb, 1, 2, str.tostring(cCancel), cTXT, cBG, sz)
    f_cell(tb, 2, 2, f_pct(cCancel, resolved), cTXT, cBG, sz)
 
    f_cell(tb, 0, 3, "NO BOS  setup window ran out", cTXT, cBG, sz)
    f_cell(tb, 1, 3, str.tostring(cNoBos), cTXT, cBG, sz)
    f_cell(tb, 2, 3, f_pct(cNoBos, resolved), cTXT, cBG, sz)
 
    f_cell(tb, 0, 4, "RESET   a fresh sweep came first", cTXT, cBG, sz)
    f_cell(tb, 1, 4, str.tostring(cReset), cTXT, cBG, sz)
    f_cell(tb, 2, 4, f_pct(cReset, resolved), cTXT, cBG, sz)
 
    f_cell(tb, 0, 5, "BOS     close beyond the far end", cTXT, cBG, sz)
    f_cell(tb, 1, 5, str.tostring(cBos), cTXT, cBG, sz)
    f_cell(tb, 2, 5, f_pct(cBos, resolved), cTXT, cBG, sz)
 
    f_cell(tb, 0, 6, "never reached a break of structure", cTXT, cHEAD, sz)
    f_cell(tb, 1, 6, str.tostring(noStruct), cTXT, cHEAD, sz)
    f_cell(tb, 2, 6, f_pct(noStruct, resolved), cTXT, cHEAD, sz)
 
    table.merge_cells(tb, 0, 7, 2, 7)
    f_cell(tb, 0, 7, "average candles: " + f_avg(ageBos, cBos) + " to a break of structure · " + f_avg(ageCancel, cCancel) + " to a cancellation", cTXT, cBG, sz)
 
    table.merge_cells(tb, 0, 8, 2, 8)
    f_cell(tb, 0, 8, "closed candles only · counts of price events, not trade results · stops at the break of structure", color.new(chart.fg_color, 35), cBG, sz)

← 返回列表