本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Buyside and Sellside Liquidity Pools [Saikix]
// © Saikix
//@version=6
indicator("Buyside and Sellside Liquidity Pools [Saikix]", shorttitle="Saikix-LP", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500, behind_chart=false)
G_BASIC = "Basic settings"
swL = input.int(10, "Swing length", minval=2, maxval=50, group=G_BASIC, display=display.none,
tooltip="A swing high is a high with no higher high this many bars to its left. Same for swing lows.")
swR = input.int(5, "Confirmation bars", minval=1, maxval=50, group=G_BASIC, display=display.none,
tooltip="Bars to the right of the swing with no higher high (lower low). A pool appears this many bars after its swing, when the swing is confirmed. It is drawn from the swing bar and never moved or removed later. Higher timeframe pools wait for this many bars of that timeframe.")
eqTol = input.float(0.2, "Equal highs / lows (× ATR)", minval=0.0, step=0.05, group=G_BASIC, display=display.none,
tooltip="A new swing within this distance of a pool that has not been taken joins that pool instead of starting a new one: BSL ×3 = price turned back at this level 3 times (equal highs or lows). The pool keeps the price of its first swing. ATR(14) of the bar where the swing is confirmed. 0 = only the same price.")
brkN = input.int(1, "Closes beyond to break", minval=1, maxval=3, group=G_BASIC, display=display.none,
tooltip="Broken: this many closes beyond the pool (1 = the first close). Swept: price went past the pool and closed back inside before that. Judged on closed bars.")
brkAtr = input.float(0.0, "Break margin (× ATR)", minval=0.0, step=0.05, group=G_BASIC, display=display.none,
tooltip="A close counts as beyond the pool only when it is at least this far past it (ATR(14)). 0 = any distance. A close past the pool but within the margin waits for the next bar.")
distUnit = input.string("Auto", "Distance unit", options=["Auto", "Pips", "Absolute", "Percent"], group=G_BASIC, display=display.none,
tooltip="Auto: pips on forex and metals, percent on other markets. Absolute: price difference.")
G_DISP = "Chart display"
perSide = input.int(3, "Pools per side", minval=0, maxval=5, inline="ps", group=G_DISP, display=display.none)
perKey = input.int(3, "", minval=0, maxval=5, inline="ps", group=G_DISP, display=display.none,
tooltip="How many pools nearest to the price are drawn above (BSL) and below (SSL): single chart timeframe swings, key pools (higher timeframe, session, ×2 or more). Counted separately, so nearer small swings never hide a 4h or daily pool. Pools far outside the price range of the last 200 bars are not drawn.")
showForm = input.bool(true, "Show forming pools", group=G_DISP, display=display.none,
tooltip="The newest swing of the chart's timeframe that is not confirmed yet (fewer than Confirmation bars to its right): a dotted line named BSL (forming). It moves until it is confirmed, then becomes a pool. No alerts.")
showLine = input.bool(true, "Show lines", group=G_DISP, display=display.none)
showZone = input.bool(true, "Show zones", inline="zn", group=G_DISP, display=display.none)
zoneAtr = input.float(0.3, "", minval=0.05, step=0.05, inline="zn", group=G_DISP, display=display.none, active=showZone,
tooltip="Zone: a band just beyond the pool (above a BSL, below a SSL), this many × ATR(14) deep.")
showName = input.bool(true, "Show names", group=G_DISP, display=display.none,
tooltip="Names at the right end of the pools: BSL above the price, SSL below; 4h BSL for a higher timeframe pool, Tokyo SSL for a session pool, ×2 for equal highs or lows. Prices are on the price scale. When the pools on a side are outside the price range of the last 200 bars, the nearest one is named with its distance, e.g. 4h BSL 49.9 pips above.")
showDist = input.bool(false, "Show distance", group=G_DISP, display=display.none, active=showName,
tooltip="Adds the distance from the price to the names, e.g. 4h BSL · 12.3 pips.")
takenMode = input.string("Stop and dim", "Taken pools", options=["Stop and dim", "Hide"], group=G_DISP, display=display.none,
tooltip="Swept and broken pools. Stop and dim: the line and zone stop at the bar that took the pool and fade (Swept dashed, Broken dotted). Hide: removed.")
markScope = input.string("Key pools", "Taken pools shown for", options=["Key pools", "All pools"], group=G_DISP, display=display.none, active=takenMode != "Hide",
tooltip="Which taken pools keep their line, zone and label. Key pools: higher timeframe pools, session pools and equal highs or lows (×2 or more). All pools: single chart timeframe swings too (many on low timeframes).")
showSwLb = input.bool(true, "Show swept labels", group=G_DISP, display=display.none, active=takenMode != "Hide",
tooltip="A label on the bar that swept a pool, e.g. 4h BSL · Swept. Labels within 12 bars on the same side share one label. Hover it for the price and how far the wick went.")
showBrLb = input.bool(true, "Show broken labels", group=G_DISP, display=display.none, active=takenMode != "Hide",
tooltip="A gray label on the bar that broke a pool, e.g. 4h BSL · Broken. Without it a broken pool is shown by its dotted line.")
G_SRC = "Pools"
srcC = input.bool(true, "Chart timeframe", inline="sc", group=G_SRC, display=display.none)
wC = input.int(1, "", minval=1, maxval=5, inline="sc", group=G_SRC, display=display.none,
tooltip="Show and line width. Pools from the swings of the chart's own timeframe. Equal highs or lows (×2 or more) are one wider. Alerts are chosen separately.")
srcH1 = input.bool(true, "Higher timeframe 1", inline="h1", group=G_SRC, display=display.none)
wH1 = input.int(2, "", minval=1, maxval=5, inline="h1", group=G_SRC, display=display.none)
srcH2 = input.bool(true, "Higher timeframe 2", inline="h2", group=G_SRC, display=display.none)
wH2 = input.int(3, "", minval=1, maxval=5, inline="h2", group=G_SRC, display=display.none,
tooltip="Show and line width. Pools from the confirmed swings of two higher timeframes, judged on this chart's bars, so a wick of this chart can sweep a 4h pool. Named with the timeframe, e.g. 4h BSL. Alerts are chosen separately.")
htfAuto = input.bool(true, "Auto timeframes", group=G_SRC, display=display.none,
tooltip="On: the first timeframe at least 4 times the chart's, and the first at least 4 times that (15m → 1h, 4h; 1h → 4h, D; 4h → D, W). Off: the two on the right.")
htfIn1 = input.timeframe("240", "Timeframes", inline="htf", group=G_SRC, display=display.none, active=not htfAuto)
htfIn2 = input.timeframe("D", "", inline="htf", group=G_SRC, display=display.none, active=not htfAuto,
tooltip="A timeframe not higher than the chart is skipped.")
srcS = input.bool(false, "Sessions", inline="ss", group=G_SRC, display=display.none)
wS = input.int(1, "", minval=1, maxval=5, inline="ss", group=G_SRC, display=display.none,
tooltip="Show and line width. The high and the low of each session become pools when the session ends (charts of 1 hour and below). A session pool is dropped when the same session starts again. Alerts for session pools need this on.")
sTok = input.bool(true, "Tokyo", group=G_SRC, display=display.none, active=srcS)
sLon = input.bool(true, "London", group=G_SRC, display=display.none, active=srcS)
sNy = input.bool(true, "New York", group=G_SRC, display=display.none, active=srcS,
tooltip="Tokyo 9:00–15:00 Tokyo time, London 8:00–16:00 London time, New York 8:00–17:00 New York time (daylight saving time included).")
G_COL = "Colors and text"
bslCol = input.color(#F23645, "Colors", inline="col", group=G_COL, display=display.none)
sslCol = input.color(#089981, "", inline="col", group=G_COL, display=display.none)
brkCol = input.color(#787B86, "", inline="col", group=G_COL, display=display.none,
tooltip="BSL (pools above the price), SSL (pools below the price), Broken labels. Lines, zones, names and Swept labels use the BSL and SSL colors.")
txtSize = input.int(13, "Text size", minval=8, maxval=30, group=G_COL, display=display.none)
G_ALERT = "Alerts"
alOn = input.bool(true, "Pool alerts", group=G_ALERT, display=display.none,
tooltip="One alert for everything checked here: create the alert with \"Any alert() function call\". Sent when a bar closes (Reached: right away), never on past bars, once per pool and event. Independent of what is drawn on the chart.\nFor a separate alert (or webhook) per event, pick the event in the alert's Condition list instead, e.g. \"BSL · Swept\".")
alWhich = input.string("Key pools", "Pools to alert", options=["All pools", "Key pools", "Higher timeframe pools", "Session pools", "Chart timeframe pools"], group=G_ALERT, display=display.none, active=alOn,
tooltip="Key pools: higher timeframe pools, session pools and equal highs or lows (×2 or more). Chart timeframe pools: single swings of the chart's timeframe only.")
alSide = input.string("Both", "Side", options=["Both", "BSL only", "SSL only"], group=G_ALERT, display=display.none, active=alOn)
alNew = input.bool(false, "New pool", group=G_ALERT, display=display.none, active=alOn,
tooltip="A swing was confirmed and became a pool (not when it joins a pool as an equal high or low).")
alNear = input.bool(false, "Near", group=G_ALERT, display=display.none, active=alOn,
tooltip="The close came within Near distance of a pool that has not been reached yet. Comes before Reached.")
nearPips = input.float(5.0, "Near distance (pips / %)", minval=0.0, step=0.5, inline="near", group=G_ALERT, display=display.none, active=alOn and alNear)
nearPct = input.float(0.1, "", minval=0.0, step=0.05, inline="near", group=G_ALERT, display=display.none, active=alOn and alNear,
tooltip="Pips are used on forex and metals, percent on other markets.")
alReach = input.bool(false, "Reached", group=G_ALERT, display=display.none, active=alOn,
tooltip="The moment price first reaches the pool (the high at or above a BSL, the low at or below a SSL). Sent right away, without waiting for the bar to close.")
alSwept = input.bool(true, "Swept", group=G_ALERT, display=display.none, active=alOn,
tooltip="Price went past the pool and closed back inside (see Closes beyond to break).")
alBreak = input.bool(true, "Broken", group=G_ALERT, display=display.none, active=alOn,
tooltip="Price closed beyond the pool (see Closes beyond to break).")
alFmt = input.string("Text", "Message format", options=["Text", "JSON"], group=G_ALERT, display=display.none, active=alOn,
tooltip="JSON is for webhooks: indicator, ticker, chart_tf, price and a list of events (pool, side, event, pool_price).")
isPipMarket() =>
string s = str.upper(syminfo.ticker)
syminfo.type == "forex" or str.contains(s, "XAU") or str.contains(s, "GOLD") or str.contains(s, "XAG") or str.contains(s, "SILVER")
pipUnit() =>
string s = str.upper(syminfo.ticker)
str.contains(s, "JPY") ? 0.01 : str.contains(s, "XAU") or str.contains(s, "GOLD") ? 0.1 : str.contains(s, "XAG") or str.contains(s, "SILVER") ? 0.01 : str.contains(s, "BTC") or str.contains(s, "ETH") ? 1.0 : syminfo.type == "forex" ? 0.0001 : syminfo.mintick
bool pipMkt = isPipMarket()
string unitShown = distUnit == "Auto" ? (pipMkt ? "Pips" : "Percent") : distUnit
fmtDist(float d) =>
unitShown == "Pips" ? str.tostring(math.round(d / pipUnit(), 1), "#0.0") + " pips" : unitShown == "Percent" ? str.tostring(close != 0 ? d / close * 100.0 : 0.0, "#0.00") + "%" : str.tostring(d, format.mintick)
fmtP(float p) => str.tostring(p, format.mintick)
float nearDist = pipMkt ? nearPips * pipUnit() : close * nearPct / 100.0
tfLabel(string tf) =>
int sec = timeframe.in_seconds(tf)
sec < 3600 ? str.tostring(math.round(sec / 60.0), "#") + "m" : sec < 86400 ? str.tostring(math.round(sec / 3600.0), "#") + "h" : sec < 604800 ? "D" : sec < 2419200 ? "W" : "M"
int CHS = timeframe.in_seconds(timeframe.period)
string A1 = 4 * CHS <= 300 ? "5" : 4 * CHS <= 900 ? "15" : 4 * CHS <= 1800 ? "30" : 4 * CHS <= 3600 ? "60" : 4 * CHS <= 14400 ? "240" : 4 * CHS <= 86400 ? "D" : 4 * CHS <= 604800 ? "W" : "M"
int A1S = timeframe.in_seconds(A1)
string A2 = 4 * A1S <= 900 ? "15" : 4 * A1S <= 1800 ? "30" : 4 * A1S <= 3600 ? "60" : 4 * A1S <= 14400 ? "240" : 4 * A1S <= 86400 ? "D" : 4 * A1S <= 604800 ? "W" : "M"
string h1 = htfAuto ? A1 : htfIn1
string h2 = htfAuto ? A2 : htfIn2
bool ok1 = timeframe.in_seconds(h1) > CHS
bool ok2 = timeframe.in_seconds(h2) > CHS and timeframe.in_seconds(h2) != timeframe.in_seconds(h1)
string L1 = tfLabel(h1)
string L2 = tfLabel(h2)
bool sesOk = srcS and CHS <= 3600
type Pool
float price
float depth
int t0
int t1
int side
bool c = false
bool u1 = false
bool u2 = false
int ses = 0
int n = 1
int pend = 0
bool hit = false
float far = na
bool reached = false
bool nearDone = false
array<int> m0
array<int> m1
array<float> mp
var array<Pool> pools = array.new<Pool>()
sesName(int s) => s == 1 ? "Tokyo" : s == 2 ? "London" : "New York"
isKey(Pool P) => P.u1 or P.u2 or P.ses > 0 or P.n >= 2
nameOf(Pool P, bool disp) =>
string pre = P.u2 and (not disp or srcH2) ? L2 + " " : P.u1 and (not disp or srcH1) ? L1 + " " : P.ses > 0 and (not disp or srcS) ? sesName(P.ses) + " " : ""
pre + (P.side > 0 ? "BSL" : "SSL") + (P.n > 1 ? " ×" + str.tostring(P.n) : "")
shownPool(Pool P) => (P.c and srcC) or (P.u1 and srcH1) or (P.u2 and srcH2) or (P.ses > 0 and srcS)
keyShown(Pool P) => (P.u1 and srcH1) or (P.u2 and srcH2) or (P.ses > 0 and srcS) or P.n >= 2
widthOf(Pool P) =>
int b = P.u2 and srcH2 ? wH2 : P.u1 and srcH1 ? wH1 : P.ses > 0 and srcS ? wS : wC
math.min(5, b + (P.n >= 2 ? 1 : 0))
sideCol(int s) => s > 0 ? bslCol : sslCol
float atr14 = ta.atr(14)
int VIEW = 200
float vHi = ta.highest(high, VIEW)
float vLo = ta.lowest(low, VIEW)
float lbGap = math.max(nz(atr14) * 0.6, (nz(vHi, high) - nz(vLo, low)) * 0.05)
addPool(float price, int side, int t0, int t1, int src, int ses) =>
float tol = nz(atr14) * eqTol
int best = -1
float bestD = 0.0
bool same = false
if pools.size() > 0
for i = 0 to pools.size() - 1
Pool P = pools.get(i)
if P.side == side and not P.hit
float d = math.abs(P.price - price)
bool sameBar = false
for j = 0 to P.mp.size() - 1
if math.abs(P.mp.get(j) - price) <= syminfo.mintick / 2 and t0 < P.m1.get(j) and P.m0.get(j) < t1
sameBar := true
if sameBar
best := i
same := true
break
if d <= tol and (best < 0 or d < bestD)
best := i
bestD := d
bool made = false
if best >= 0
Pool P = pools.get(best)
if same
for j = 0 to P.mp.size() - 1
if math.abs(P.mp.get(j) - price) <= syminfo.mintick / 2 and t0 < P.m1.get(j) and P.m0.get(j) < t1 and t1 - t0 < P.m1.get(j) - P.m0.get(j)
P.m0.set(j, t0)
P.m1.set(j, t1)
P.t0 := P.m0.min()
else
P.n += 1
P.depth := math.max(P.depth, nz(atr14) * zoneAtr)
P.m0.push(t0)
P.m1.push(t1)
P.mp.push(price)
if src == 0
P.c := true
if src == 1
P.u1 := true
if src == 2
P.u2 := true
if ses > 0
P.ses := ses
else
Pool N = Pool.new(price, nz(atr14) * zoneAtr, t0, t1, side, src == 0, src == 1, src == 2, ses)
N.m0 := array.from(t0)
N.m1 := array.from(t1)
N.mp := array.from(price)
pools.push(N)
made := true
if pools.size() > 200
int far = 0
float fd = -1.0
for i = 0 to pools.size() - 1
float d = math.abs(pools.get(i).price - close)
if d > fd
fd := d
far := i
pools.remove(far)
made
inScope(Pool P) =>
bool w = alWhich == "All pools" or (alWhich == "Key pools" and isKey(P)) or (alWhich == "Higher timeframe pools" and (P.u1 or P.u2)) or (alWhich == "Session pools" and P.ses > 0) or (alWhich == "Chart timeframe pools" and P.c and not (P.u1 or P.u2 or P.ses > 0))
bool s = alSide == "Both" or (alSide == "BSL only" and P.side > 0) or (alSide == "SSL only" and P.side < 0)
w and s
jsStr(string v) => "\"" + v + "\""
addTxt(string acc, string v) => acc == "" ? v : acc + " · " + v
addName(string acc, string v) => acc == "" ? v : array.includes(str.split(acc, " / "), v) ? acc : acc + " / " + v
addJs(string acc, string v) => acc == "" ? v : acc + "," + v
evJs(Pool P, string evt) =>
"{\"pool\":" + jsStr(nameOf(P, false)) + ",\"side\":" + jsStr(P.side > 0 ? "bsl" : "ssl") + ",\"event\":" + jsStr(evt) + ",\"pool_price\":" + str.tostring(P.price) + "}"
msgOf(string txt, string js) =>
alFmt == "JSON" ?
"{\"indicator\":\"Saikix-LP\",\"ticker\":" + jsStr(syminfo.ticker) + ",\"chart_tf\":" + jsStr(timeframe.period) + ",\"price\":" + str.tostring(close) + ",\"events\":[" + js + "]}" :
"[Saikix-LP] " + syminfo.ticker + " " + tfLabel(timeframe.period) + " " + txt + ". Price " + fmtP(close)
string aTxt = ""
string aJs = ""
bool evNewB = false
bool evNewS = false
bool evNearB = false
bool evNearS = false
bool evSwB = false
bool evSwS = false
bool evBrB = false
bool evBrS = false
type Ses
float hi = na
float lo = na
int th = na
int thc = na
int tl = na
int tlc = na
bool run = false
var Ses S1 = Ses.new()
var Ses S2 = Ses.new()
var Ses S3 = Ses.new()
bool in1 = not na(time(timeframe.period, "0900-1500", "Asia/Tokyo"))
bool in2 = not na(time(timeframe.period, "0800-1600", "Europe/London"))
bool in3 = not na(time(timeframe.period, "0800-1700", "America/New_York"))
dropSes(int k) =>
if pools.size() > 0
for i = pools.size() - 1 to 0
Pool P = pools.get(i)
if P.ses == k
if P.c or P.u1 or P.u2
P.ses := 0
else
Pool gone = pools.remove(i)
gone.ses := 0
stepSes(Ses S, bool inNow, bool inPrev, int k) =>
float hi = na
float lo = na
int th = na
int thc = na
int tl = na
int tlc = na
if inNow and not inPrev
dropSes(k)
S.hi := high
S.lo := low
S.th := time
S.thc := time_close
S.tl := time
S.tlc := time_close
S.run := true
else if inNow and S.run
if high > S.hi
S.hi := high
S.th := time
S.thc := time_close
if low < S.lo
S.lo := low
S.tl := time
S.tlc := time_close
else if not inNow and inPrev and S.run
hi := S.hi
lo := S.lo
th := S.th
thc := S.thc
tl := S.tl
tlc := S.tlc
S.run := false
[hi, lo, th, thc, tl, tlc]
float phC = ta.pivothigh(high, swL, swR)
float plC = ta.pivotlow(low, swL, swR)
int tpC = time[swR]
int tpCc = time_close[swR]
htfPiv() => [ta.pivothigh(high, swL, swR)[1], ta.pivotlow(low, swL, swR)[1], time[swR + 1], time_close[swR + 1]]
[ph1, pl1, t01, t11] = request.security(syminfo.tickerid, h1, htfPiv(), lookahead=barmerge.lookahead_on)
[ph2, pl2, t02, t12] = request.security(syminfo.tickerid, h2, htfPiv(), lookahead=barmerge.lookahead_on)
var int last1 = na
var int last2 = na
if barstate.isconfirmed
array<Pool> made = array.new<Pool>()
if sesOk
if sTok
[hi, lo, th, thc, tl, tlc] = stepSes(S1, in1, in1[1], 1)
if not na(hi)
if addPool(hi, 1, th, thc, -1, 1)
made.push(pools.last())
if addPool(lo, -1, tl, tlc, -1, 1)
made.push(pools.last())
if sLon
[hi, lo, th, thc, tl, tlc] = stepSes(S2, in2, in2[1], 2)
if not na(hi)
if addPool(hi, 1, th, thc, -1, 2)
made.push(pools.last())
if addPool(lo, -1, tl, tlc, -1, 2)
made.push(pools.last())
if sNy
[hi, lo, th, thc, tl, tlc] = stepSes(S3, in3, in3[1], 3)
if not na(hi)
if addPool(hi, 1, th, thc, -1, 3)
made.push(pools.last())
if addPool(lo, -1, tl, tlc, -1, 3)
made.push(pools.last())
if not na(phC)
if addPool(phC, 1, tpC, tpCc, 0, 0)
made.push(pools.last())
if not na(plC)
if addPool(plC, -1, tpC, tpCc, 0, 0)
made.push(pools.last())
if ok1 and not na(t01) and (na(last1) or t01 != last1)
last1 := t01
if not na(ph1)
if addPool(ph1, 1, t01, t11, 1, 0)
made.push(pools.last())
if not na(pl1)
if addPool(pl1, -1, t01, t11, 1, 0)
made.push(pools.last())
if ok2 and not na(t02) and (na(last2) or t02 != last2)
last2 := t02
if not na(ph2)
if addPool(ph2, 1, t02, t12, 2, 0)
made.push(pools.last())
if not na(pl2)
if addPool(pl2, -1, t02, t12, 2, 0)
made.push(pools.last())
for P in made
if P.side > 0
evNewB := true
else
evNewS := true
if alOn and alNew and inScope(P)
aTxt := addTxt(aTxt, "New " + nameOf(P, false) + " " + fmtP(P.price))
aJs := addJs(aJs, evJs(P, "new"))
string swNameB = ""
string swNameS = ""
string brNameB = ""
string brNameS = ""
string swTipB = ""
string swTipS = ""
string brTipB = ""
string brTipS = ""
bool keyB = false
bool keyS = false
if barstate.isconfirmed and pools.size() > 0
for i = pools.size() - 1 to 0
Pool P = pools.get(i)
float p = P.price
bool past = P.side > 0 ? high > p : low < p
int st = 0
if P.hit or past
P.hit := true
P.far := P.side > 0 ? math.max(nz(P.far, high), high) : math.min(nz(P.far, low), low)
float mg = brkAtr * nz(atr14)
bool inside = P.side > 0 ? close <= p : close >= p
bool beyond = P.side > 0 ? close > p + mg : close < p - mg
if inside
st := 1
else if beyond
P.pend += 1
if P.pend >= brkN
st := 2
if st > 0
string nm = nameOf(P, true)
string tip = nameOf(P, false) + " " + fmtP(p) + " · " + (P.side > 0 ? "high " : "low ") + fmtDist(math.abs(P.far - p)) + (P.side > 0 ? " above" : " below")
bool keep = takenMode != "Hide" and shownPool(P) and (markScope == "All pools" or isKey(P))
bool mk = keep and (st == 1 ? showSwLb : showBrLb)
if mk
if P.side > 0
keyB := true
if st == 1
swNameB := addName(swNameB, nm)
swTipB := swTipB == "" ? tip : swTipB + "\n" + tip
else
brNameB := addName(brNameB, nm)
brTipB := brTipB == "" ? tip : brTipB + "\n" + tip
else
keyS := true
if st == 1
swNameS := addName(swNameS, nm)
swTipS := swTipS == "" ? tip : swTipS + "\n" + tip
else
brNameS := addName(brNameS, nm)
brTipS := brTipS == "" ? tip : brTipS + "\n" + tip
if keep
color c = sideCol(P.side)
if showZone
box.new(P.t0, P.side > 0 ? p + P.depth : p, time, P.side > 0 ? p : p - P.depth, xloc=xloc.bar_time, border_color=na, bgcolor=color.new(c, 93))
if showLine
line.new(P.t0, p, time, p, xloc=xloc.bar_time, color=color.new(c, 55), width=1, style=st == 1 ? line.style_dashed : line.style_dotted)
if P.side > 0
evSwB := evSwB or st == 1
evBrB := evBrB or st == 2
else
evSwS := evSwS or st == 1
evBrS := evBrS or st == 2
if alOn and inScope(P) and ((st == 1 and alSwept) or (st == 2 and alBreak))
string ev = st == 1 ? "Swept" : "Broken"
aTxt := addTxt(aTxt, nameOf(P, false) + " " + fmtP(p) + " " + str.lower(ev))
aJs := addJs(aJs, evJs(P, str.lower(ev)))
pools.remove(i)
else
if P.side > 0 ? high >= p : low <= p
P.reached := true
float d = P.side > 0 ? p - close : close - p
if not P.nearDone and not P.reached and d > 0 and d <= nearDist
P.nearDone := true
if P.side > 0
evNearB := true
else
evNearS := true
if alOn and alNear and inScope(P)
aTxt := addTxt(aTxt, nameOf(P, false) + " " + fmtP(p) + " near (" + fmtDist(d) + ")")
aJs := addJs(aJs, evJs(P, "near"))
var array<label> mkL = array.new<label>(2)
var array<int> mkX = array.new<int>(2, -100)
var array<float> mkY = array.new<float>(2, na)
var array<string> mkS = array.new<string>(2, "")
var array<string> mkB = array.new<string>(2, "")
var array<string> mkT = array.new<string>(2, "")
addNames(string acc, string nm) =>
string r = acc
if nm != ""
for one in str.split(nm, " / ")
r := addName(r, one)
r
markText(string sw, string br) =>
string a = sw != "" ? sw + " · Swept" : ""
string b = br != "" ? br + " · Broken" : ""
a != "" and b != "" ? a + "\n" + b : a + b
markAt(int k, string sw, string br, string tip, int side) =>
if sw != "" or br != ""
float y = side > 0 ? high : low
if bar_index - mkX.get(k) <= 12 and not na(mkL.get(k))
string s2 = addNames(mkS.get(k), sw)
string b2 = addNames(mkB.get(k), br)
string t2 = mkT.get(k) + "\n" + tip
float y2 = side > 0 ? math.max(mkY.get(k), y) : math.min(mkY.get(k), y)
label L = mkL.get(k)
label.set_xy(L, time, y2)
label.set_text(L, markText(s2, b2))
label.set_color(L, s2 != "" ? sideCol(side) : brkCol)
label.set_tooltip(L, t2)
mkS.set(k, s2)
mkB.set(k, b2)
mkT.set(k, t2)
mkY.set(k, y2)
else
mkL.set(k, label.new(time, y, markText(sw, br), xloc=xloc.bar_time, yloc=yloc.price,
style=side > 0 ? label.style_label_down : label.style_label_up, color=sw != "" ? sideCol(side) : brkCol, textcolor=color.white, size=txtSize, tooltip=tip))
mkS.set(k, sw)
mkB.set(k, br)
mkT.set(k, tip)
mkY.set(k, y)
mkX.set(k, bar_index)
joinTip(string a, string b) => a != "" and b != "" ? a + "\n" + b : a + b
if barstate.isconfirmed
markAt(0, swNameB, brNameB, joinTip(swTipB, brTipB), 1)
markAt(1, swNameS, brNameS, joinTip(swTipS, brTipS), -1)
string rTxt = ""
string rJs = ""
bool evReB = false
bool evReS = false
if barstate.isrealtime and pools.size() > 0
for P in pools
if not P.reached and not P.hit and (P.side > 0 ? high >= P.price : low <= P.price)
if P.side > 0
evReB := true
else
evReS := true
if alOn and alReach and inScope(P)
rTxt := addTxt(rTxt, nameOf(P, false) + " " + fmtP(P.price) + " reached")
rJs := addJs(rJs, evJs(P, "reached"))
if aTxt != ""
alert(msgOf(aTxt, aJs), alert.freq_once_per_bar_close)
if rTxt != ""
alert(msgOf(rTxt, rJs), alert.freq_once_per_bar)
alertcondition(evNewB, "New BSL", "[Saikix-LP] {{ticker}} {{interval}} New BSL. Price {{close}}")
alertcondition(evNewS, "New SSL", "[Saikix-LP] {{ticker}} {{interval}} New SSL. Price {{close}}")
alertcondition(evNearB, "BSL · Near", "[Saikix-LP] {{ticker}} {{interval}} Near a BSL. Price {{close}}")
alertcondition(evNearS, "SSL · Near", "[Saikix-LP] {{ticker}} {{interval}} Near a SSL. Price {{close}}")
alertcondition(evReB, "BSL · Reached", "[Saikix-LP] {{ticker}} {{interval}} BSL reached. Price {{close}}")
alertcondition(evReS, "SSL · Reached", "[Saikix-LP] {{ticker}} {{interval}} SSL reached. Price {{close}}")
alertcondition(evSwB, "BSL · Swept", "[Saikix-LP] {{ticker}} {{interval}} BSL swept. Price {{close}}")
alertcondition(evSwS, "SSL · Swept", "[Saikix-LP] {{ticker}} {{interval}} SSL swept. Price {{close}}")
alertcondition(evBrB, "BSL · Broken", "[Saikix-LP] {{ticker}} {{interval}} BSL broken. Price {{close}}")
alertcondition(evBrS, "SSL · Broken", "[Saikix-LP] {{ticker}} {{interval}} SSL broken. Price {{close}}")
int fkH = -ta.highestbars(high, swR)
int fkL = -ta.lowestbars(low, swR)
float lH = ta.highest(high, swL)
float lL = ta.lowest(low, swL)
bool fOkH = bar_index > swL + swR and high[fkH] > lH[fkH + 1]
bool fOkL = bar_index > swL + swR and low[fkL] < lL[fkL + 1]
var array<line> dLn = array.new<line>()
var array<box> dBx = array.new<box>()
var array<label> dLb = array.new<label>()
var array<float> pxB = array.new<float>(10, na)
var array<float> pxS = array.new<float>(10, na)
nearest(int side) =>
array<Pool> out = array.new<Pool>()
array<float> ds = array.new<float>()
if pools.size() > 0
for P in pools
if P.side == side and shownPool(P)
float d = math.abs(P.price - close)
int k = ds.size()
if ds.size() > 0
for j = ds.size() - 1 to 0
if ds.get(j) > d
k := j
ds.insert(k, d)
out.insert(k, P)
out
drawSide(int side, float lo, float hi, float edgeY, array<float> px) =>
array<Pool> L = nearest(side)
int shown = 0
string pend = ""
float pendP = na
float pendD = na
Pool edge = na
Pool edgeK = na
color c = sideCol(side)
float gap = lbGap
int nC = 0
int nK = 0
for P in L
bool key = keyShown(P)
if key ? nK >= perKey : nC >= perSide
continue
float p = P.price
bool inView = p >= lo and p <= hi
if not inView
if na(edge)
edge := P
if key and na(edgeK)
edgeK := P
continue
if key
nK += 1
else
nC += 1
shown += 1
px.set(shown - 1, p)
if showZone
dBx.push(box.new(P.t0, side > 0 ? p + P.depth : p, time, side > 0 ? p : p - P.depth, xloc=xloc.bar_time, border_color=na, bgcolor=color.new(c, key ? 85 : 91)))
if showLine
dLn.push(line.new(P.t0, p, time, p, xloc=xloc.bar_time, color=key ? c : color.new(c, 25), width=widthOf(P), style=P.hit ? line.style_dashed : line.style_solid))
if showName
string nm = nameOf(P, true) + (showDist ? " · " + fmtDist(math.abs(p - close)) : "")
if pend != "" and math.abs(p - pendP) < gap
if not array.includes(str.split(pend, " / "), nm)
pend := pend + " / " + nm
pendP := p
else
if pend != ""
dLb.push(label.new(time, pendP, pend, xloc=xloc.bar_time, style=side > 0 ? label.style_label_lower_right : label.style_label_upper_right, color=c, textcolor=color.white, size=txtSize))
pend := nm
pendP := p
Pool eg = nK == 0 and not na(edgeK) ? edgeK : shown == 0 ? edge : na
string egTxt = na(eg) ? "" : nameOf(eg, true) + " " + fmtDist(math.abs(eg.price - close)) + (side > 0 ? " above" : " below")
if pend != ""
dLb.push(label.new(time, pendP, egTxt != "" and showName ? (side > 0 ? egTxt + "\n" + pend : pend + "\n" + egTxt) : pend, xloc=xloc.bar_time,
style=side > 0 ? label.style_label_lower_right : label.style_label_upper_right, color=c, textcolor=color.white, size=txtSize))
if showForm and srcC
bool ok = side > 0 ? fOkH : fOkL
float fp = side > 0 ? high[fkH] : low[fkL]
int ft = side > 0 ? time[fkH] : time[fkL]
bool near = false
for j = 0 to px.size() - 1
if not na(px.get(j)) and math.abs(px.get(j) - fp) < gap
near := true
if ok and not near and fp >= lo and fp <= hi
dLn.push(line.new(ft, fp, time, fp, xloc=xloc.bar_time, color=color.new(c, 30), width=wC, style=line.style_dotted))
if showName
dLb.push(label.new(time, fp, (side > 0 ? "BSL" : "SSL") + " (forming)", xloc=xloc.bar_time, style=side > 0 ? label.style_label_lower_right : label.style_label_upper_right,
color=color.new(c, 45), textcolor=color.white, size=txtSize))
if showName and egTxt != "" and pend == ""
dLb.push(label.new(time, edgeY, egTxt, xloc=xloc.bar_time,
style=side > 0 ? label.style_label_upper_right : label.style_label_lower_right, color=color.new(c, 30), textcolor=color.white, size=txtSize))
if barstate.islast
for x in dLn
line.delete(x)
for x in dBx
box.delete(x)
for x in dLb
label.delete(x)
dLn.clear()
dBx.clear()
dLb.clear()
for j = 0 to 9
pxB.set(j, na)
pxS.set(j, na)
float vh = nz(vHi, high)
float vl = nz(vLo, low)
float m = (vh - vl) * 0.25
drawSide(1, vl - m, vh + m, vh, pxB)
drawSide(-1, vl - m, vh + m, vl, pxS)
plot(barstate.islast ? pxB.get(0) : na, "BSL 1", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(1) : na, "BSL 2", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(2) : na, "BSL 3", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(3) : na, "BSL 4", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(4) : na, "BSL 5", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(5) : na, "BSL 6", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(6) : na, "BSL 7", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(7) : na, "BSL 8", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(8) : na, "BSL 9", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxB.get(9) : na, "BSL 10", bslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(0) : na, "SSL 1", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(1) : na, "SSL 2", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(2) : na, "SSL 3", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(3) : na, "SSL 4", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(4) : na, "SSL 5", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(5) : na, "SSL 6", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(6) : na, "SSL 7", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(7) : na, "SSL 8", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(8) : na, "SSL 9", sslCol, display=display.price_scale, editable=false)
plot(barstate.islast ? pxS.get(9) : na, "SSL 10", sslCol, display=display.price_scale, editable=false)