Cross-Exchange Sweep [Zen]

ChartZenFX · study · 245 行 · 点赞 150 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartZenFX
//@version=6
indicator("Cross-Exchange Sweep [Zen]", overlay = true, max_labels_count = 60, behind_chart = false)
 
// Cross-Exchange Sweep [Zen]
// A sweep on the chart exchange (wick beyond the pool of the previous N closed bars, close back inside, closed bar)
// is compared with three reference exchanges on the same candle. Each exchange is checked against ITS OWN pool.
 
// ---------- inputs ----------
poolLen  = input.int(20, "Pool length (bars)", minval = 5, maxval = 500, tooltip = "Pool = highest high / lowest low of the previous N closed bars, computed separately on every exchange.", display = display.none)
showPool = input.bool(true, "Show pools", tooltip = "Step lines of the chart exchange pools.", display = display.none)
showDots = input.bool(true, "Show LOCAL WICK dots", tooltip = "LOCAL WICK: the sweep happened only on the chart exchange, no reference exchange reached its own pool.", display = display.none)
showTbl  = input.bool(true, "Show table", group = "Display", display = display.none)
tblPosIn = input.string("Bottom right", "Table position", options = ["Bottom right", "Top right", "Bottom left", "Top left"], group = "Display", 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 cHItxt  = isLight ? #994110 : #D9703A
color cLO     = isLight ? #1B46B4 : #6F88EE
color cNEU    = isLight ? #647482 : #8C9098
color cMUTED  = isLight ? #505C69 : #9AA3B2
color cINK    = isLight ? #1F2937 : #D1D4DC
color cSOLID  = isLight ? #FFFFFF : #131722
color cPANEL  = isLight ? #F4F5F7 : #1E222D
color cHEAD   = #2A2E39
color cHEADTX = isLight ? #FFFFFF : #D1D4DC
color cBORDER = isLight ? #C4C8CE : #363A45
color cFRESH  = color.new(#C98B78, 60)
 
// ---------- reference exchanges (the chart exchange is replaced by Bitget) ----------
string pref = syminfo.prefix
string tk   = syminfo.ticker
string ex1  = pref == "BINANCE" ? "BITGET" : "BINANCE"
string ex2  = pref == "BYBIT" ? "BITGET" : "BYBIT"
string ex3  = pref == "OKX" ? "BITGET" : "OKX"
 
// reference bar with the chart bar's time: on a live bar the reference may already have opened its next bar,
// so its current and previous bars are both requested and the one whose time equals the chart bar's time is used
f_ref() =>
    float ph = ta.highest(high, poolLen)[1] + (bar_index >= poolLen ? 0.0 : na)
    float pl = ta.lowest(low, poolLen)[1] + (bar_index >= poolLen ? 0.0 : na)
    [high, low, close, time, ph, pl, high[1], low[1], close[1], time[1], ph[1], pl[1]]
 
f_pick(int tA, int tB, float vA, float vB) => tA == time ? vA : tB == time ? vB : na
 
[h1a, l1a, c1a, t1a, ph1a, pl1a, h1b, l1b, c1b, t1b, ph1b, pl1b] = request.security(ex1 + ":" + tk, timeframe.period, f_ref(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
[h2a, l2a, c2a, t2a, ph2a, pl2a, h2b, l2b, c2b, t2b, ph2b, pl2b] = request.security(ex2 + ":" + tk, timeframe.period, f_ref(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
[h3a, l3a, c3a, t3a, ph3a, pl3a, h3b, l3b, c3b, t3b, ph3b, pl3b] = request.security(ex3 + ":" + tk, timeframe.period, f_ref(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
 
int   t1  = t1a == time or t1b == time ? time : na
float h1  = f_pick(t1a, t1b, h1a, h1b)
float l1  = f_pick(t1a, t1b, l1a, l1b)
float c1  = f_pick(t1a, t1b, c1a, c1b)
float ph1 = f_pick(t1a, t1b, ph1a, ph1b)
float pl1 = f_pick(t1a, t1b, pl1a, pl1b)
int   t2  = t2a == time or t2b == time ? time : na
float h2  = f_pick(t2a, t2b, h2a, h2b)
float l2  = f_pick(t2a, t2b, l2a, l2b)
float c2  = f_pick(t2a, t2b, c2a, c2b)
float ph2 = f_pick(t2a, t2b, ph2a, ph2b)
float pl2 = f_pick(t2a, t2b, pl2a, pl2b)
int   t3  = t3a == time or t3b == time ? time : na
float h3  = f_pick(t3a, t3b, h3a, h3b)
float l3  = f_pick(t3a, t3b, l3a, l3b)
float c3  = f_pick(t3a, t3b, c3a, c3b)
float ph3 = f_pick(t3a, t3b, ph3a, ph3b)
float pl3 = f_pick(t3a, t3b, pl3a, pl3b)
 
// ---------- sweep on the chart exchange ----------
float rawHi  = ta.highest(high, poolLen)[1]
float rawLo  = ta.lowest(low, poolLen)[1]
float poolHi = bar_index >= poolLen ? rawHi : na
float poolLo = bar_index >= poolLen ? rawLo : na
bool swHi = not na(poolHi) and high > poolHi and close < poolHi
bool swLo = not na(poolLo) and low < poolLo and close > poolLo
 
plot(showPool ? poolHi : na, "Pool high", cHI, 1, plot.style_stepline)
plot(showPool ? poolLo : na, "Pool low", cLO, 1, plot.style_stepline)
 
// ---------- helpers ----------
// state of one exchange on this candle: -1 n/a, 0 did not reach its pool, 1 wick beyond + close back (swept), 2 wick beyond + close beyond (broke)
f_st(int t, float x, float c, float p, bool up) =>
    int st = -1
    if t == time and not na(p) and not na(x) and not na(c)
        bool reached = up ? x > p : x < p
        bool back = up ? c < p : c > p
        st := reached ? (back ? 1 : 2) : 0
    st
 
f_nm(int st) => st == -1 ? "n/a" : st == 0 ? "no" : st == 1 ? "swept" : "broke"
f_sq(int st) => st == -1 ? "–" : st >= 1 ? "■" : "□"
f_tk(int st) => st == -1 ? "n/a" : st >= 1 ? "took" : "did not take"
 
// class: an exchange "took" the level when its wick went beyond its own pool (closed back or not)
f_cls(int a, int b, int c) =>
    int avail = (a >= 0 ? 1 : 0) + (b >= 0 ? 1 : 0) + (c >= 0 ? 1 : 0)
    int hit = (a >= 1 ? 1 : 0) + (b >= 1 ? 1 : 0) + (c >= 1 ? 1 : 0)
    avail < 2 ? 3 : hit == avail ? 0 : hit == 0 ? 2 : 1
 
f_cname(int k) => k == 0 ? "MARKET-WIDE" : k == 1 ? "SPLIT" : k == 2 ? "LOCAL WICK" : "n/a"
f_tf() => timeframe.isminutes ? (timeframe.multiplier % 60 == 0 ? str.tostring(int(timeframe.multiplier / 60)) + "H" : str.tostring(timeframe.multiplier) + "m") : timeframe.period
f_px(float x) => na(x) ? "—" : str.tostring(x, format.mintick)
 
// small dot marker (a label body, so the tooltip always shows)
f_dot(int x, float y, color col, string tip) =>
    label.new(x, y, "", style = label.style_circle, color = col, size = size.tiny, tooltip = tip)
 
// turn an existing label into a small dot (two plates of the same side closer than MERGE_BARS bars: the older one becomes a dot)
f_toDot(label l, color col) =>
    label.set_style(l, label.style_circle)
    label.set_text(l, "")
    label.set_color(l, col)
    label.set_size(l, size.tiny)
 
// ---------- state ----------
int MERGE_BARS = 8
var array<int> cnt = array.from(0, 0, 0, 0)
var label lastLblHi = na
var int lastBarHi = na
var label lastLblLo = na
var int lastBarLo = na
var int lastT = na
var int lastBar = na
var string lastSide = "—"
var int lastCls = -1
var array<int>   lastSt = array.from(-1, -1, -1, -1)
var array<float> lastPool = array.new<float>(4, na)
var array<float> lastWick = array.new<float>(4, na)
var array<int>   alig = array.from(0, 0, 0)
bool evW = false
bool evS = false
bool evL = false
string evMsg = ""
 
if barstate.isconfirmed
    array.set(alig, 0, array.get(alig, 0) + (t1 == time ? 1 : 0))
    array.set(alig, 1, array.get(alig, 1) + (t2 == time ? 1 : 0))
    array.set(alig, 2, array.get(alig, 2) + (t3 == time ? 1 : 0))
    for side = 0 to 1
        bool up = side == 0
        if up ? swHi : swLo
            int a = up ? f_st(t1, h1, c1, ph1, true) : f_st(t1, l1, c1, pl1, false)
            int b = up ? f_st(t2, h2, c2, ph2, true) : f_st(t2, l2, c2, pl2, false)
            int c = up ? f_st(t3, h3, c3, ph3, true) : f_st(t3, l3, c3, pl3, false)
            int k = f_cls(a, b, c)
            array.set(cnt, k, array.get(cnt, k) + 1)
            float pool = up ? poolHi : poolLo
            float wick = up ? high : low
            string sq = f_sq(a) + " " + f_sq(b) + " " + f_sq(c)
            string tip = f_cname(k) + " · " + (up ? "high" : "low") + " sweep · " + str.format_time(time, "yyyy-MM-dd HH:mm", "UTC") + " UTC"
            tip += "\n" + pref + " (chart): pool " + f_px(pool) + ", wick " + f_px(wick) + ", swept"
            tip += "\n" + ex1 + ": " + f_nm(a) + "\n" + ex2 + ": " + f_nm(b) + "\n" + ex3 + ": " + f_nm(c)
            color cS = up ? cHI : cLO
            if k == 2
                if showDots
                    f_dot(bar_index, wick, cNEU, tip)
            else if k <= 1
                label prevL = up ? lastLblHi : lastLblLo
                int prevB = up ? lastBarHi : lastBarLo
                if not na(prevL) and bar_index - prevB < MERGE_BARS
                    f_toDot(prevL, cS)
                color bg = k == 0 ? cS : cPANEL
                color tx = k == 0 ? cSOLID : (up ? cHItxt : cLO)
                string txt = (k == 0 ? "WIDE" : "SPLIT") + "\n" + sq
                label nl = label.new(bar_index, wick, txt, style = up ? label.style_label_down : label.style_label_up, color = bg, textcolor = tx, size = size.small, tooltip = tip)
                if up
                    lastLblHi := nl
                    lastBarHi := bar_index
                else
                    lastLblLo := nl
                    lastBarLo := bar_index
            // remember the latest sweep for the table
            lastT := time
            lastBar := bar_index
            lastSide := up ? "high" : "low"
            lastCls := k
            array.set(lastSt, 0, 1)
            array.set(lastSt, 1, a)
            array.set(lastSt, 2, b)
            array.set(lastSt, 3, c)
            array.set(lastPool, 0, pool)
            array.set(lastPool, 1, a >= 0 ? (up ? ph1 : pl1) : na)
            array.set(lastPool, 2, b >= 0 ? (up ? ph2 : pl2) : na)
            array.set(lastPool, 3, c >= 0 ? (up ? ph3 : pl3) : na)
            array.set(lastWick, 0, wick)
            array.set(lastWick, 1, a >= 0 ? (up ? h1 : l1) : na)
            array.set(lastWick, 2, b >= 0 ? (up ? h2 : l2) : na)
            array.set(lastWick, 3, c >= 0 ? (up ? h3 : l3) : na)
            if k <= 2
                evW := evW or k == 0
                evS := evS or k == 1
                evL := evL or k == 2
                evMsg += syminfo.ticker + " " + f_tf() + " " + pref + ": " + f_cname(k) + " " + (up ? "high" : "low") + " sweep. " + ex1 + " " + f_tk(a) + ", " + ex2 + " " + f_tk(b) + ", " + ex3 + " " + f_tk(c) + ". "
 
if evMsg != ""
    alert(evMsg, alert.freq_once_per_bar_close)
alertcondition(evW, "MARKET-WIDE sweep", "{{exchange}}:{{ticker}} {{interval}} Cross-Exchange Sweep: MARKET-WIDE sweep")
alertcondition(evS, "SPLIT sweep", "{{exchange}}:{{ticker}} {{interval}} Cross-Exchange Sweep: SPLIT sweep")
alertcondition(evL, "LOCAL WICK sweep", "{{exchange}}:{{ticker}} {{interval}} Cross-Exchange Sweep: LOCAL WICK sweep")
 
// ---------- table ----------
tblPos = switch tblPosIn
    "Top right"    => position.top_right
    "Bottom right" => position.bottom_right
    "Bottom left"  => position.bottom_left
    => position.top_left
 
var table tb = table.new(tblPos, 4, 9, bgcolor = cPANEL, frame_color = cBORDER, frame_width = 1, border_width = 0)
if barstate.isfirst and showTbl
    table.merge_cells(tb, 0, 0, 3, 0)
 
f_cell(int col, int row, string t, color tc, bool num, color bg) =>
    table.cell(tb, col, row, t, text_color = tc, bgcolor = bg, text_size = size.small, text_halign = num ? text.align_right : text.align_left, text_font_family = num ? font.family_monospace : font.family_default)
 
if barstate.islast and showTbl
    bool fresh = not na(lastBar) and bar_index - lastBar <= 1
    color rowBg = fresh ? cFRESH : cPANEL
    int total = array.sum(cnt)
    table.cell(tb, 0, 0, "Cross-Exchange Sweep [Zen] · " + f_tf(), text_color = cHEADTX, bgcolor = cHEAD, text_size = size.small)
    bool noRef = array.sum(alig) == 0
    f_cell(0, 1, "Last", fresh ? cINK : cMUTED, false, rowBg)
    f_cell(1, 1, na(lastT) ? "—" : str.format_time(lastT, "MM-dd HH:mm", "UTC"), cINK, false, rowBg)
    f_cell(2, 1, lastSide, cINK, false, rowBg)
    f_cell(3, 1, noRef ? "no reference data" : lastCls < 0 ? "—" : f_cname(lastCls), noRef ? cMUTED : cINK, false, rowBg)
    f_cell(0, 2, "Exchange", cMUTED, false, cPANEL)
    f_cell(1, 2, "Pool", cMUTED, true, cPANEL)
    f_cell(2, 2, "Wick", cMUTED, true, cPANEL)
    f_cell(3, 2, "Result", cMUTED, false, cPANEL)
    for i = 0 to 3
        string nm = i == 0 ? pref + " (chart)" : i == 1 ? ex1 : i == 2 ? ex2 : ex3
        int st = array.get(lastSt, i)
        f_cell(0, 3 + i, nm, cINK, false, cPANEL)
        f_cell(1, 3 + i, lastCls < 0 ? "—" : f_px(array.get(lastPool, i)), cINK, true, cPANEL)
        f_cell(2, 3 + i, lastCls < 0 ? "—" : f_px(array.get(lastWick, i)), cINK, true, cPANEL)
        f_cell(3, 3 + i, lastCls < 0 ? "—" : f_sq(st) + " " + f_nm(st), st >= 1 ? cINK : cMUTED, false, cPANEL)
    f_cell(0, 7, "History", cMUTED, false, cPANEL)
    f_cell(1, 7, "WIDE " + str.tostring(array.get(cnt, 0)), cINK, true, cPANEL)
    f_cell(2, 7, "SPLIT " + str.tostring(array.get(cnt, 1)), cINK, true, cPANEL)
    f_cell(3, 7, "LOCAL " + str.tostring(array.get(cnt, 2)), cINK, false, cPANEL)
    f_cell(0, 8, "n/a " + str.tostring(array.get(cnt, 3)), cMUTED, false, cPANEL)
    f_cell(1, 8, "of " + str.tostring(total), cMUTED, true, cPANEL)
    f_cell(2, 8, "", cMUTED, true, cPANEL)
    f_cell(3, 8, "■ took  □ did not  – no data", cMUTED, false, cPANEL)

← 返回列表