本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ProofCharts
//@version=6
indicator("Liquidity Check: Is Your Stop Where Everyone's Is?", shorttitle = "Liquidity Stop Check", overlay = true, max_labels_count = 60, max_lines_count = 10)
// You type your stop price. The script shows whether it sits near an obvious level (the nearest unbroken swing low
// or high, and the nearest round number) and how often, on this chart, price traded a little past such swing levels
// and then closed back (a "poke" that was reclaimed), with the count n. It does not judge the stop and does not tell
// you where to put it. A closed candle's values never change afterwards.
G_MAIN = "Your stop"
G_RULE = "Rules (most people leave these as they are)"
G_LOOK = "Display"
iStop = input.float(0.0, "Stop price", minval = 0, step = 0.01, group = G_MAIN, tooltip = "The price where your stop-loss order sits. A stop-loss is an order that closes your trade at that price, so the loss stays small. 0 = off (the chart statistics still show). Type it to see if it sits where everyone's stop is.")
iSide = input.string("Buy (long)", "My trade", options = ["Buy (long)", "Sell (short)"], group = G_MAIN, tooltip = "Buy (long): your stop is below the price. It is compared with swing lows, the low turning points. Sell (short): your stop is above the price. It is compared with swing highs.")
iN = input.int(5, "Turning point: candles on each side", minval = 2, maxval = 20, group = G_RULE, tooltip = "A swing low is the lowest low with this many candles on each side of it. So you only know it this many candles later. Swing highs work the same way.")
iW = input.int(3, "Back over the level within (candles)", minval = 1, maxval = 20, group = G_RULE, tooltip = "A poke means the price goes past a swing level (a turning point). If a candle closes back on the other side within this many candles, the poke counts as reclaimed. The poke candle itself counts too.")
iZ = input.float(0.3, "Near = within (average candles)", minval = 0.05, maxval = 3, step = 0.05, group = G_RULE, tooltip = "Your stop counts as near a level when it is at most this many average candles away. The average candle is ATR 14: the average size of the last 14 candles.")
iStep = input.float(0.0, "Round number step (0 = auto)", minval = 0, group = G_RULE, tooltip = "The gap between round numbers. Auto picks it from your stop price, for example 1,000 at 60,000, 1 at 60 and 0.000001 at 0.000012.")
iMarks = input.bool(true, "Mark levels that held or broke", group = G_LOOK, tooltip = "A small dot where a swing level was reclaimed (sky blue) or broken (grey). Broken means price stayed past the level. The dot sits on the candle where this was decided.")
iPanel = input.bool(true, "Panel", group = G_LOOK, tooltip = "Box with plain sentences about your stop.")
iPos = input.string("Top right", "Panel position", options = ["Top right", "Bottom right", "Bottom left", "Top left"], group = G_LOOK, tooltip = "Where the panel sits on the chart.", active = iPanel)
// ---------- Calculation ----------
ATR_LEN = 14
EPS = 1e-9
MAX_LEVELS = 50
MAX_EVENTS = 3000
int side = iSide == "Buy (long)" ? 1 : -1
float atr = ta.atr(ATR_LEN)
float atrPrev = atr[1]
float piv = side == 1 ? ta.pivotlow(low, iN, iN) : ta.pivothigh(high, iN, iN)
var float[] lvP = array.new_float(0) // unbroken swing levels: price
var int[] lvB = array.new_int(0) // and the swing candle's bar_index
var float[] pdP = array.new_float(0) // pokes still waiting for a close back: level
var int[] pdB = array.new_int(0) // poke candle
var float[] pdX = array.new_float(0) // furthest price past the level so far
var float[] pdA = array.new_float(0) // ATR of the candle before the poke
var int[] evB = array.new_int(0) // resolved pokes: poke candle
var int[] evT = array.new_int(0) // candle where it was decided
var int[] evR = array.new_int(0) // 1 reclaimed, 0 broken
var float[] evD = array.new_float(0) // depth past the level in average candles (na if broken)
past(float p) => side == 1 ? math.sign(low - p) < 0 : math.sign(high - p) > 0
back(float p) => side == 1 ? math.sign(close - p) > 0 : math.sign(close - p) < 0
resolve(float p, int pb, float x, float a, bool rec) =>
array.push(evB, pb)
array.push(evT, bar_index)
array.push(evR, rec ? 1 : 0)
array.push(evD, rec ? (side == 1 ? p - x : x - p) / a : na)
if array.size(evB) > MAX_EVENTS
array.shift(evB)
array.shift(evT)
array.shift(evR)
array.shift(evD)
rec
int recNow = 0
int brkNow = 0
// (a) pokes from earlier candles
int i = 0
while i < array.size(pdP)
float p = array.get(pdP, i)
float x = side == 1 ? math.min(array.get(pdX, i), low) : math.max(array.get(pdX, i), high)
array.set(pdX, i, x)
bool done = false
if back(p)
resolve(p, array.get(pdB, i), x, array.get(pdA, i), true)
recNow += 1
done := true
else if bar_index - array.get(pdB, i) + 1 >= iW
resolve(p, array.get(pdB, i), x, array.get(pdA, i), false)
brkNow += 1
done := true
if done
array.remove(pdP, i)
array.remove(pdB, i)
array.remove(pdX, i)
array.remove(pdA, i)
else
i += 1
// (b) new pokes of unbroken levels on this candle
int j = 0
while j < array.size(lvP)
float p = array.get(lvP, j)
if past(p)
if not na(atrPrev) and math.sign(atrPrev) > 0
float x = side == 1 ? low : high
if back(p)
resolve(p, bar_index, x, atrPrev, true)
recNow += 1
else if iW <= 1
resolve(p, bar_index, x, atrPrev, false)
brkNow += 1
else
array.push(pdP, p)
array.push(pdB, bar_index)
array.push(pdX, x)
array.push(pdA, atrPrev)
array.remove(lvP, j)
array.remove(lvB, j)
else
j += 1
// (c) level confirmed on this candle (the swing was iN candles ago)
if not na(piv)
array.push(lvP, piv)
array.push(lvB, bar_index - iN)
if array.size(lvP) > MAX_LEVELS
array.shift(lvP)
array.shift(lvB)
// statistics of resolved pokes
int poked = array.size(evR)
int reclaimed = 0
if poked > 0
for e = 0 to poked - 1
reclaimed += array.get(evR, e)
float recPct = poked > 0 ? 100.0 * reclaimed / poked : na
// the user's stop
bool on = iStop > 0 and not na(atr) and math.sign(atr) > 0
float rnd = na
float rdist = na
float level = na
int age = na
float dist = na
int near = na
int deep = na
float deepPct = na
if on
float stp = iStep
if stp <= 0
int ex = int(math.floor(math.log10(iStop)))
if math.sign(iStop - (ex + 1 >= 0 ? math.pow(10, ex + 1) : 1.0 / math.pow(10, -(ex + 1)))) >= 0
ex += 1
if math.sign((ex >= 0 ? math.pow(10, ex) : 1.0 / math.pow(10, -ex)) - iStop) > 0
ex -= 1
stp := ex - 1 >= 0 ? math.pow(10, ex - 1) : 1.0 / math.pow(10, -(ex - 1))
rnd := math.floor(iStop / stp + 0.5) * stp
rdist := math.abs(iStop - rnd) / atr
bool isNear = math.sign(iZ * atr - math.abs(iStop - rnd)) >= 0
int best = -1
if array.size(lvP) > 0
for k = 0 to array.size(lvP) - 1
if best < 0 or math.sign(math.abs(iStop - array.get(lvP, best)) - math.abs(iStop - array.get(lvP, k))) >= 0
best := k
if best >= 0
level := array.get(lvP, best)
age := bar_index - array.get(lvB, best)
dist := (side == 1 ? level - iStop : iStop - level) / atr
isNear := isNear or math.sign(iZ * atr - math.abs(iStop - level)) >= 0
if dist > 0 and reclaimed > 0
int dc = 0
for e = 0 to poked - 1
if array.get(evR, e) == 1 and array.get(evD, e) >= dist - EPS
dc += 1
deep := dc
deepPct := 100.0 * dc / reclaimed
near := isNear ? 1 : 0
// ---------- Look ----------
color C_PANEL = #0f172a
color C_HEAD = #1e293b
color C_TEXT = #e2e8f0
color C_MUTED = #94a3b8
color C_RED = #ef4444
color C_AMBER = #f59e0b
color C_SKY = #38bdf8
plot(iStop > 0 ? iStop : na, "Stop line", color = C_RED, linewidth = 2, style = plot.style_linebr, display = display.pane)
plot(level, "Nearest swing level line", color = C_AMBER, linewidth = 1, style = plot.style_linebr, display = display.pane)
plot(rnd, "Nearest round number line", color = color.new(C_MUTED, 30), linewidth = 1, style = plot.style_linebr, display = display.pane)
plotshape(iMarks and recNow > 0 and barstate.isconfirmed, "Reclaimed poke", shape.circle, side == 1 ? location.belowbar : location.abovebar, color.new(C_SKY, 0), size = size.tiny, display = display.pane)
plotshape(iMarks and brkNow > 0 and barstate.isconfirmed, "Broken poke", shape.circle, side == 1 ? location.belowbar : location.abovebar, color.new(C_MUTED, 30), size = size.tiny, display = display.pane)
// ---------- Values in the Data Window (also used by the tests) ----------
plot(iStop > 0 ? iStop : na, "Stop price", display = display.data_window)
plot(atr, "Average candle (ATR 14)", display = display.data_window)
plot(level, "Nearest unbroken swing level", display = display.data_window)
plot(age, "Swing level age (candles)", display = display.data_window)
plot(dist, "Stop past the level (average candles)", display = display.data_window)
plot(rnd, "Nearest round number", display = display.data_window)
plot(rdist, "Distance to round number (average candles)", display = display.data_window)
plot(near, "Near an obvious level (1 yes, 0 no)", display = display.data_window)
plot(poked, "Levels traded through", display = display.data_window)
plot(reclaimed, "Reclaimed", display = display.data_window)
plot(poked - reclaimed, "Broken", display = display.data_window)
plot(recPct, "Reclaimed %", display = display.data_window)
plot(deep, "Reclaimed pokes as deep as your stop", display = display.data_window)
plot(deepPct, "Reclaimed pokes as deep as your stop %", display = display.data_window)
// ---------- Labels and panel ----------
pos = iPos == "Bottom right" ? position.bottom_right : iPos == "Bottom left" ? position.bottom_left : iPos == "Top left" ? position.top_left : position.top_right
// Breaks a text into lines of at most n characters, at spaces only (no word is changed), so the panel fits the chart
// of a phone held upright (compliance/phone_fit.py; owner's Android screenshot 2026-11-03: wide panels were cut off).
wrapText(string s, int n) =>
string out = ""
int len = -1
for w in str.split(s, " ")
int wl = str.length(w)
if len < 0
out := w
len := wl
else if len + 1 + wl > n
out := out + "\n" + w
len := wl
else
out := out + " " + w
len := len + 1 + wl
out
var table tb = table.new(pos, 1, 8, bgcolor = color.new(C_PANEL, 5), frame_color = color.new(C_MUTED, 70), frame_width = 1)
var label lS = na
var label lL = na
px(float v) => str.tostring(v, format.mintick)
f2(float v) => str.tostring(v, "0.00")
string kind = side == 1 ? "swing low" : "swing high"
string pt = side == 1 ? "low point" : "high point"
string ptLong = side == 1 ? "low point (swing low)" : "high point (swing high)"
string word = side == 1 ? "below" : "above"
string other = side == 1 ? "above" : "below"
// Owner 2026-11-03: "make it more readable and simpler, like for a small child, with exact descriptions".
// Most important sentence first, one thought per sentence, every number and count kept; reference.py mirrors it.
row(int r, string t, color c) =>
table.cell(tb, 0, r, wrapText(t, 30), text_color = c, text_size = size.small, text_halign = text.align_left)
if barstate.islast
label.delete(lS)
label.delete(lL)
if iStop > 0
lS := label.new(bar_index + 2, iStop, "Your stop " + px(iStop), style = label.style_label_left, color = color.new(C_RED, 20), textcolor = color.white, size = size.small, tooltip = "Your stop price (from the settings). Your stop is the price where you get out if you are wrong.")
if not na(level)
lL := label.new(bar_index + 2, level, "Nearest " + pt + " " + px(level), style = label.style_label_left, color = color.new(C_AMBER, 20), textcolor = C_PANEL, size = size.small, tooltip = "The nearest " + ptLong + " that the price has not gone " + word + " since it formed. Many traders put their stops just " + word + " it.")
if iPanel
for i = 0 to 7
table.cell(tb, 0, i, "")
table.cell(tb, 0, 0, wrapText("IS YOUR STOP WHERE EVERYONE'S IS?" + (barstate.isconfirmed ? "" : " · live"), 30), text_color = color.white, bgcolor = C_HEAD, text_size = size.small, text_halign = text.align_left)
int r = 1
if iStop <= 0
row(r, "Type your stop price in the settings. Your stop is the price where you get out if you are wrong. Pick Buy (long) if your stop is " + "below the price. Pick Sell (short) if it is above the price.", C_TEXT)
r += 1
else if not on
row(r, "Please wait. The script needs " + str.tostring(ATR_LEN) + " candles to learn the size of an average candle.", C_TEXT)
r += 1
else
row(r, near == 1 ? "Careful: your stop is close to a spot where many traders put their stops. Close = " + f2(iZ) + " average candles or less." : "✓ Your stop is not close to a spot where many traders put their stops. It is more than " + f2(iZ) + " average candles away.", near == 1 ? C_AMBER : C_SKY)
r += 1
if na(level)
row(r, "There is no " + ptLong + " on this chart yet that the price has not gone " + word + ".", C_TEXT)
else
row(r, "Nearest " + ptLong + ": " + px(level) + ". It formed " + str.tostring(age) + " candles ago. Your stop is " + f2(math.abs(dist)) + " average candles " + (dist > 0 ? word : other) + " it.", C_TEXT)
r += 1
row(r, "Nearest round number: " + px(rnd) + ". Your stop is " + f2(rdist) + " average candles from it.", C_TEXT)
r += 1
if poked == 0
table.cell(tb, 0, r, wrapText("On this chart the price has not gone " + word + " a " + pt + " yet (n = 0).", 30), text_color = C_TEXT, text_size = size.small, text_halign = text.align_left)
else
table.cell(tb, 0, r, wrapText("On this chart the price went " + word + " a " + pt + " " + str.tostring(poked) + " times. " + str.tostring(reclaimed) + " times (" + str.tostring(recPct, "0") + "%) a candle closed back " + other + " it within " + str.tostring(iW) + " candles (blue dots). " + str.tostring(poked - reclaimed) + " times it did not (grey dots).", 30), text_color = C_TEXT, text_size = size.small, text_halign = text.align_left, tooltip = "Each " + pt + " counts once: the first time the price goes " + word + " it after the " + pt + " is known.")
r += 1
if not na(level)
if dist <= 0
row(r, "Your stop is " + other + " that " + pt + ". So the price reaches your stop first, before it goes " + word + " the " + pt + ".", C_TEXT)
r += 1
else if not na(deep)
table.cell(tb, 0, r, wrapText(str.tostring(deep) + " of those " + str.tostring(reclaimed) + " times (" + str.tostring(deepPct, "0") + "%) the price went at least " + f2(dist) + " average candles " + word + " the " + pt + ". That is as far as your stop is.", 30), text_color = C_TEXT, text_size = size.small, text_halign = text.align_left, tooltip = "How far = the furthest price past the " + pt + " before the close back, divided by the average candle (ATR 14) of the candle before.")
r += 1
if iStop > 0 and on
row(r, "Red line = your stop. Orange line = nearest " + pt + ". Grey line = nearest round number.", C_MUTED)
r += 1
table.cell(tb, 0, r, wrapText("A " + ptLong + " is the " + (side == 1 ? "lowest low" : "highest high") + " of " + str.tostring(2 * iN + 1) + " candles. You only see it " + str.tostring(iN) + " candles later. Average candle = ATR 14, the average size of recent candles. Past behaviour, not a forecast.", 36), text_color = C_MUTED, text_size = size.tiny, text_halign = text.align_left)