本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Crowd Positioning Gauge - Dip and Rally Forecast from Long/Short Accounts", shorttitle = "Crowd Gauge", overlay = false, max_labels_count = 500, max_lines_count = 500, max_boxes_count = 50)
import TradingView/Request/3 as rq
// ─────────────────────────────────────────────────────────────────────────────
// Crowd Positioning Gauge
// A weather-style forecast for dips and rallies, built from how one-sided the crowd is.
// - Gauge: rank of the long/short accounts ratio within the trailing year
// (0 = most accounts short, 100 = most accounts long).
// - Card: "a dip of X% or more within N days: P%" and the same for a rally.
// P is the share of past bars in the same crowd state after which price
// traded at least that far below (above) the close. X is sized to the
// symbol's own current volatility. The same share over all bars is shown
// next to each number.
// - Signals: a sell setup (▼) when the crowd is strongly long-heavy and a buy
// setup (▲) when it is strongly short-heavy, each marked on the price chart
// with a target and a stop. Every past setup on this
// chart is followed to its exit. The card shows how many ended at the
// target, at the stop, or ran out of time.
// Everything is counted on this chart; nothing is hard-coded.
// Works on the chart's own timeframe, from 1 hour to 1 day, on a crypto
// perpetual contract. Windows are set in days and converted to bars.
// Both can happen in the same window. Not a direction call, not a buy / sell signal.
// ─────────────────────────────────────────────────────────────────────────────
// ─────────────────────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────────────────────
string GRP_LANG = "Language"
string langIn = input.string("English", "Language", options = ["English", "Japanese"], group = GRP_LANG, tooltip = "Language of the panel and labels. Setting names stay in English; hover the (i) icons for Japanese.\n日本語:パネルとラベルの言語。設定の項目名は英語のままです。")
bool JA = langIn == "Japanese"
string GRP_G = "Gauge"
int lookN = input.int(365, "Ranking window (days)", minval = 120, maxval = 730, group = GRP_G, tooltip = "The ratio is ranked within this many past days (converted to bars of the chart timeframe). Verified at 365.\n日本語:比率の順位を出す期間(日数)。表示している時間足の本数に換算して使います。検証は365日で行いました。")
float extPct = input.float(90.0, "Extreme zone starts at rank", minval = 70.0, maxval = 97.0, step = 5.0, group = GRP_G, tooltip = "Ranks at or above this value count as 'crowd long-heavy'; ranks at or below 100 minus this value count as 'crowd short-heavy'. Verified at 90.\n日本語:この順位以上を「買いに偏り」、100からこの値を引いた順位以下を「売りに偏り」とします。検証は90で行いました。")
string GRP_O = "Forecast"
int hDays = input.int(7, "Days ahead", options = [1, 3, 7], group = GRP_O, tooltip = "The forecast window (converted to bars of the chart timeframe). Verified at 7 and 3.\n日本語:何日先までを予報するか(表示している時間足の本数に換算します)。検証は7日と3日で行いました。")
float kDip = input.float(0.5, "Move size (x typical move over the window)", minval = 0.25, maxval = 1.5, step = 0.25, group = GRP_O, tooltip = "How large a dip or rally must be to count, as a multiple of the symbol's typical move over the forecast window (current volatility x square root of time). 0.5 = a moderate move that happens after roughly 6 bars in 10; 1.0 = a large move that happens after roughly 3 in 10. The card shows it as a percentage and a price.\n日本語:数える下げ・上げの大きさ。その銘柄の「予報期間のふつうの値動き」(いまのボラティリティ×時間の平方根)の何倍かで決めます。0.5=ふだんでも10回に6回ほど起きる動き、1.0=10回に3回ほどの大きな動き。カードには%と価格で表示します。")
float lam = input.float(0.94, "Volatility smoothing (EWMA lambda, per day)", minval = 0.80, maxval = 0.99, step = 0.01, group = GRP_O, tooltip = "Used to size the move to the symbol's volatility. Higher = slower-moving volatility estimate. Given per day; on intraday charts it is converted so the memory in days stays the same.\n日本語:動きの大きさを決めるのに使うボラティリティの平滑化係数(1日あたり)。日足より短い足では、日数で見た効き方が同じになるよう換算します。")
string GRP_S = "Signals (crowd strongly one-sided)"
bool showSig = input.bool(true, "Mark setups on the price chart (▼ sell, ▲ buy)", group = GRP_S, tooltip = "▼ sell setup: the close of a bar on which the crowd is strongly long-heavy (rank halfway between the extreme threshold and 100, so 95 or more by default). ▲ buy setup: the mirror image, crowd strongly short-heavy (rank 5 or less by default). A new setup of the same side does not start while an earlier one is still open. It ends at the target, at the stop, or at the close after the forecast window. A ▼ or ▲ mark at the setup, dashed target and stop lines, and a dot where it ended (green = target, red = stop, grey = time) are drawn on the price chart.\n日本語:▼売りの合図=個人の口座が買いに強く偏った足(順位が、極端のしきい値と100の中間以上。初期値では95以上)の終値。▲買いの合図=その逆で、売りに強く偏った足(初期値では順位5以下)の終値。同じ向きの前の合図が続いている間は出しません。利確・損切り・予報期間の終わりのどれかで終了します。合図の足に▼か▲、利確と損切りの点線、終わった足に点(緑=利確、赤=損切り、灰=時間切れ)を価格チャートに描きます。")
float tpK = input.float(1.0, "Target (x typical move over the window)", minval = 0.25, maxval = 3.0, step = 0.25, group = GRP_S, tooltip = "Distance from the entry to the target, as a multiple of the typical move over the forecast window. Verified at 1.0.\n日本語:エントリーから利確までの距離。予報期間のふつうの値動きの何倍か。検証は1.0で行いました。")
float slK = input.float(1.0, "Stop (x typical move over the window)", minval = 0.25, maxval = 3.0, step = 0.25, group = GRP_S, tooltip = "Distance from the entry to the stop. If one bar reaches both the stop and the target, the stop is counted. Verified at 1.0.\n日本語:エントリーから損切りまでの距離。同じ足で利確と損切りの両方に届いた場合は、損切りとして数えます。検証は1.0で行いました。")
bool showRec = input.bool(true, "Show the count of past setups in the card", group = GRP_S, tooltip = "How past setups on this chart ended, for sell and buy setups separately: number of setups and how many reached the target, the stop, or ran out of time.\n日本語:このチャートでの過去の合図(売り・買いそれぞれ)の回数と、利確・損切り・時間切れで終わった数をカードに表示します。")
string GRP_V = "Display"
bool showTbl = input.bool(true, "Show panel", group = GRP_V, tooltip = "The forecast card.\n日本語:予報のカードを表示します。")
string tblWhere = input.string("Price chart", "Show panel on", options = ["Price chart", "Gauge pane"], group = GRP_V, tooltip = "Which pane the panel is drawn in.\n日本語:パネルを表示する場所(価格チャート/メーターの枠)。")
string tblPos = input.string("Top right", "Panel position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = GRP_V, tooltip = "Where the panel is placed.\n日本語:パネルの位置(右上・右下・左上・左下)。")
string tblSz = input.string("Normal", "Panel text size", options = ["Tiny", "Small", "Normal", "Large"], group = GRP_V, tooltip = "Text size of the panel.\n日本語:パネルの文字の大きさ(極小・小・標準・大)。")
bool showZone = input.bool(true, "Draw forecast zones on the price chart", group = GRP_V, tooltip = "Two zones to the right of the last bar: from the current price down to the dip level and up to the rally level, as wide as the forecast window. The stronger the colour, the higher the percentage.\n日本語:価格チャートの右側に、予報のゾーンを2つ描きます。現在値から下げの目安まで、上げの目安までの範囲で、横幅は予報の期間です。色が濃いほど確率が高いことを表します。")
bool showDet = input.bool(false, "Show details (the numbers behind the forecast)", group = GRP_V, tooltip = "Adds bar graphs for each crowd state (long-heavy, middle, short-heavy, all), for dips and for rallies, with the number of chart bars counted.\n日本語:予報の根拠を表示します。偏りの状態ごと(買いに偏り・中間・売りに偏り・全体)の棒グラフと、数えた足の本数です。")
bool showNote = input.bool(true, "Show the note at the bottom of the card", group = GRP_V, tooltip = "The short explanation at the bottom of the panel.\n日本語:パネル下の注記を表示します。")
bool showBg = input.bool(true, "Shade extreme readings in the gauge pane", group = GRP_V, tooltip = "Background shading on bars in an extreme zone.\n日本語:メーターの枠で、極端な水準の足に背景色を付けます。")
bool showPx = input.bool(true, "Shade extreme readings on the price chart", group = GRP_V, tooltip = "Amber = crowd was long-heavy (dips were more frequent afterwards), blue = crowd was short-heavy. Lets you see what price did afterwards.\n日本語:価格チャートにも色を付けます。橙=買いに偏った足(その後の下げが多かった)、青=売りに偏った足。その後の値動きを目で確かめられます。")
// ─────────────────────────────────────────────────────────────────────────────
// Constants
// ─────────────────────────────────────────────────────────────────────────────
// Amber and blue on purpose: red / green would read as sell / buy
color COL_RISK = #f59e0b // crowd long-heavy -> dips were more frequent
color COL_CALM = #38bdf8 // crowd short-heavy
color COL_MID = #94a3b8
color COL_TEXT = #e2e8f0
color COL_BG = #0f172a
color COL_WIN = #22c55e
color COL_LOSS = #ef4444
int MET_N = 10 // meter cells; one cell = 10 percentage points
int COLS = 2 + MET_N
float hiCut = extPct
float loCut = 100.0 - extPct
float hi2 = (extPct + 100.0) / 2.0 // 'strongly long-heavy' starts here (95 by default)
float lo2 = 100.0 - hi2 // 'strongly short-heavy' ends here (5 by default)
// Chart timeframe: 1 hour to 1 day. Everything set in days is converted to bars.
int tfSec = timeframe.in_seconds()
bool tfOk = tfSec >= 3600 and tfSec <= 86400
float bpd = 86400.0 / tfSec // bars per day
int rankN = int(math.round(lookN * bpd)) // ranking window in bars
int hBars = math.max(1, int(math.round(hDays * bpd))) // outcome window in bars
float lamB = math.pow(lam, 1.0 / bpd) // smoothing per bar (same memory in days)
int BINS = 1500 // ratio 0.00 .. 14.99 in steps of 0.01
// ─────────────────────────────────────────────────────────────────────────────
// Data
// ─────────────────────────────────────────────────────────────────────────────
// Number of accounts net long divided by number of accounts net short (exchange-reported)
float lsr = rq.cryptoDerivativeMetric(syminfo.tickerid, "Long/Short Ratio Accounts", timeframe = timeframe.period)
// Rank of the current ratio among the previous `rankN` bars, 0..100:
// the share of those readings that were lower. Kept as a histogram plus a ring of
// the last `rankN` readings, so the cost per bar does not grow with the window.
var array<int> hCnt = array.new_int(BINS, 0)
var array<int> hRing = array.new_int(math.max(1, rankN), -1)
var int hPos = 0
var int hN = 0
float rk = na
if tfOk and not na(lsr)
int b = math.max(0, math.min(BINS - 1, int(math.round(lsr * 100))))
if hN >= rankN / 2
int below = 0
if b > 0
for i = 0 to b - 1
below += hCnt.get(i)
rk := 100.0 * below / hN
if barstate.isconfirmed
int old = hRing.get(hPos)
if old >= 0
hCnt.set(old, hCnt.get(old) - 1)
else
hN += 1
hRing.set(hPos, b)
hCnt.set(b, hCnt.get(b) + 1)
hPos := (hPos + 1) % rankN
// Per-bar volatility (EWMA of squared log returns), used only to size the dip
float r = math.log(close / close[1])
var float v = na
if not na(r)
v := na(v) ? r * r : lamB * v + (1 - lamB) * r * r
float sig = math.sqrt(v)
bool isLongZone = not na(rk) and rk >= hiCut
bool isShortZone = not na(rk) and rk <= loCut
// ─────────────────────────────────────────────────────────────────────────────
// Outcome records: one per bar, once the window of `hDays` days after it has finished
// ─────────────────────────────────────────────────────────────────────────────
var array<float> aRk = array.new<float>() // rank on the bar the window started
var array<float> aDD = array.new<float>() // deepest dip below that bar's close inside the window, in typical-move units
var array<float> aRU = array.new<float>() // highest rally above that bar's close inside the window, in typical-move units
float loH = ta.lowest(low, hBars)
float hiH = ta.highest(high, hBars)
float c0 = close[hBars]
float s0 = sig[hBars]
float rk0 = rk[hBars]
float unit = s0 * math.sqrt(hBars) // typical move over the window, as seen at its start
if barstate.isconfirmed and tfOk and not na(rk0) and not na(unit) and unit > 0 and not na(c0) and c0 > 0
aRk.push(rk0)
aDD.push(math.max(0.0, 1 - loH / c0) / unit)
aRU.push(math.max(0.0, hiH / c0 - 1) / unit)
// For records whose starting rank lies in [a, b]:
// [bars, share (%) with a dip of at least k typical moves, share (%) with a rally of at least k]
f_group(float a, float b, float k) =>
int n = 0
int nD = 0
int nU = 0
if aRk.size() > 0
for i = 0 to aRk.size() - 1
float x = aRk.get(i)
if x >= a and x <= b
n += 1
nD += aDD.get(i) >= k ? 1 : 0
nU += aRU.get(i) >= k ? 1 : 0
[n, n > 0 ? 100.0 * nD / n : na, n > 0 ? 100.0 * nU / n : na]
// ─────────────────────────────────────────────────────────────────────────────
// Signals: sell setups while the crowd is strongly long-heavy, buy setups while it is
// strongly short-heavy. Each is followed to its exit.
// ─────────────────────────────────────────────────────────────────────────────
// Decided on confirmed bars only, from values known at that bar's close, so marks never move afterwards.
// Index 0 = sell side, 1 = buy side.
var array<bool> tIn = array.new_bool(2, false)
var array<float> tE = array.new_float(2, na) // entry price
var array<float> tTP = array.new_float(2, na)
var array<float> tSL = array.new_float(2, na)
var array<int> tBar = array.new_int(2, 0)
var array<line> tLnTP = array.new_line(2, na)
var array<line> tLnSL = array.new_line(2, na)
var array<int> cAll = array.new_int(2, 0) // finished setups
var array<int> cTP = array.new_int(2, 0)
var array<int> cSL = array.new_int(2, 0)
var array<int> cTime = array.new_int(2, 0)
bool sigSell = false
bool sigBuy = false
float unitCur = sig * math.sqrt(hBars)
// Follows one side (dir = -1 sell, +1 buy) on the current confirmed bar. Returns true when a new setup starts.
f_side(int k, int dir, bool zone) =>
bool started = false
if tIn.get(k)
float e = tE.get(k)
float tp = tTP.get(k)
float sl = tSL.get(k)
float res = na
float xPx = na
int why = 0 // 1 target, 2 stop, 3 time
// if one bar reaches both levels, the stop is counted
if dir < 0 ? high >= sl : low <= sl
xPx := sl
why := 2
else if dir < 0 ? low <= tp : high >= tp
xPx := tp
why := 1
else if bar_index - tBar.get(k) >= hBars
xPx := close
why := 3
if why > 0
res := dir * (xPx / e - 1)
cAll.set(k, cAll.get(k) + 1)
if why == 1
cTP.set(k, cTP.get(k) + 1)
else if why == 2
cSL.set(k, cSL.get(k) + 1)
else
cTime.set(k, cTime.get(k) + 1)
tIn.set(k, false)
if showSig
line.set_x2(tLnTP.get(k), bar_index)
line.set_x2(tLnSL.get(k), bar_index)
string tip = (why == 1 ? (JA ? "利確 " : "target ") : why == 2 ? (JA ? "損切り " : "stop ") : (JA ? "時間切れ " : "time exit ")) + (res >= 0 ? "+" : "") + str.tostring(100 * res, "0.0") + "%"
label.new(bar_index, xPx, "●", style = label.style_none, textcolor = why == 1 ? COL_WIN : why == 2 ? COL_LOSS : COL_MID, size = size.small, tooltip = tip, force_overlay = true)
else if showSig
line.set_x2(tLnTP.get(k), bar_index)
line.set_x2(tLnSL.get(k), bar_index)
if not tIn.get(k) and zone and not na(unitCur) and unitCur > 0
started := true
float tp = close * (1 + dir * tpK * unitCur)
float sl = close * (1 - dir * slK * unitCur)
tIn.set(k, true)
tE.set(k, close)
tBar.set(k, bar_index)
tTP.set(k, tp)
tSL.set(k, sl)
if showSig
tLnTP.set(k, line.new(bar_index, tp, bar_index + 1, tp, color = color.new(COL_WIN, 20), width = 1, style = line.style_dashed, force_overlay = true))
tLnSL.set(k, line.new(bar_index, sl, bar_index + 1, sl, color = color.new(COL_LOSS, 20), width = 1, style = line.style_dashed, force_overlay = true))
string tip2 = (dir < 0 ? (JA ? "売りの合図" : "Sell setup") : (JA ? "買いの合図" : "Buy setup")) + (JA ? " · 利確 " : " · target ") + str.tostring(tp, format.mintick) + (JA ? " · 損切り " : " · stop ") + str.tostring(sl, format.mintick)
label.new(bar_index, dir < 0 ? high : low, dir < 0 ? "▼" : "▲", yloc = dir < 0 ? yloc.abovebar : yloc.belowbar, style = label.style_none, textcolor = dir < 0 ? COL_RISK : COL_CALM, size = size.normal, tooltip = tip2, force_overlay = true)
started
if tfOk and barstate.isconfirmed
sigSell := f_side(0, -1, not na(rk) and rk >= hi2)
sigBuy := f_side(1, 1, not na(rk) and rk <= lo2)
// ─────────────────────────────────────────────────────────────────────────────
// Gauge
// ─────────────────────────────────────────────────────────────────────────────
color rkCol = isLongZone ? COL_RISK : isShortZone ? COL_CALM : COL_MID
plot(rk, "Crowd rank (0 = most accounts short, 100 = most accounts long)", color = rkCol, linewidth = 2)
hline(50, "Middle", color = color.new(COL_MID, 70), linestyle = hline.style_dotted)
hiLine = hline(hiCut, "Long-heavy threshold", color = color.new(COL_RISK, 40), linestyle = hline.style_dashed)
topLine = hline(100, "Top", color = color.new(COL_RISK, 100))
loLine = hline(loCut, "Short-heavy threshold", color = color.new(COL_CALM, 40), linestyle = hline.style_dashed)
botLine = hline(0, "Bottom", color = color.new(COL_CALM, 100))
fill(hiLine, topLine, color = color.new(COL_RISK, 86), title = "Long-heavy zone")
fill(loLine, botLine, color = color.new(COL_CALM, 86), title = "Short-heavy zone")
bgcolor(showBg and isLongZone ? color.new(COL_RISK, 90) : showBg and isShortZone ? color.new(COL_CALM, 90) : na, title = "Extreme reading shading")
bgcolor(showPx and isLongZone ? color.new(COL_RISK, 86) : showPx and isShortZone ? color.new(COL_CALM, 86) : na, title = "Extreme reading shading (price chart)", force_overlay = true)
plot(lsr, "Long/short accounts ratio (raw)", color = COL_MID, display = display.data_window)
// Zone captions at the right edge of the gauge
var label capHi = na
var label capLo = na
if barstate.islast
label.delete(capHi)
label.delete(capLo)
capHi := label.new(bar_index + 2, (hiCut + 100) / 2, JA ? "買いに偏り" : "crowd long-heavy", style = label.style_label_left, color = color.new(COL_BG, 20), textcolor = COL_RISK, size = size.small)
capLo := label.new(bar_index + 2, loCut / 2, JA ? "売りに偏り" : "crowd short-heavy", style = label.style_label_left, color = color.new(COL_BG, 20), textcolor = COL_CALM, size = size.small)
// ─────────────────────────────────────────────────────────────────────────────
// Panel
// ─────────────────────────────────────────────────────────────────────────────
f_pos(string s) =>
s == "Top right" ? position.top_right : s == "Bottom right" ? position.bottom_right : s == "Top left" ? position.top_left : position.bottom_left
string SZ_BIG = tblSz == "Large" ? size.huge : tblSz == "Normal" ? size.large : tblSz == "Small" ? size.normal : size.small
string SZ_MID = tblSz == "Large" ? size.large : tblSz == "Normal" ? size.normal : tblSz == "Small" ? size.small : size.tiny
string SZ_SML = tblSz == "Large" ? size.normal : tblSz == "Normal" ? size.small : size.tiny
var table tb = na
var box bxD = na
var box bxU = na
var label lbD = na
var label lbU = na
f_cell(int c, int r_, string txt, color tc, color bg, string sz, string al) =>
table.cell(tb, c, r_, txt, text_color = tc, bgcolor = bg, text_size = sz, text_halign = al)
// One line of text across the whole card
f_line(int r_, string txt, color tc, string sz) =>
f_cell(0, r_, txt, tc, color(na), sz, text.align_left)
table.merge_cells(tb, 0, r_, COLS - 1, r_)
f_p(float x) =>
na(x) ? "-" : str.tostring(x, "0") + "%"
// A meter made of coloured cells: filled up to `pct`, with a tick in the cell where `base` (the normal level) falls
f_meter(int r_, float pct, float base, color c) =>
for i = 0 to MET_N - 1
bool on = not na(pct) and pct >= (i + 0.5) * 100.0 / MET_N
bool tick = not na(base) and int(math.min(MET_N - 1, math.floor(base * MET_N / 100.0))) == i
table.cell(tb, 2 + i, r_, tick ? "▼" : " ", text_color = COL_TEXT, bgcolor = on ? color.new(c, 10) : color.new(COL_MID, 82), text_size = size.tiny, width = 1.1)
// Headline row: what, the percentage in large type, the meter
f_main(int r_, string name, float pct, float base, color c, bool hot) =>
f_cell(0, r_, name, COL_TEXT, color(na), SZ_MID, text.align_left)
f_cell(1, r_, f_p(pct), hot ? c : COL_TEXT, hot ? color.new(c, 82) : color(na), SZ_BIG, text.align_right)
f_meter(r_, pct, base, hot ? c : COL_MID)
// Detail row: state name, percentage, meter
f_row(int r_, string name, float pct, float base, color c) =>
f_cell(0, r_, name, COL_MID, color(na), SZ_SML, text.align_left)
f_cell(1, r_, f_p(pct), c, color(na), SZ_SML, text.align_right)
f_meter(r_, pct, base, c)
if barstate.islast
float unitNow = sig * math.sqrt(hBars)
bool ready = tfOk and not na(lsr) and not na(rk) and not na(unitNow) and aRk.size() >= 100 * bpd
[nA, dA, uA] = f_group(0.0, 100.0, kDip)
[nL, dL, uL] = f_group(hiCut, 100.0, kDip)
[nL2, dL2, uL2] = f_group(hi2, 100.0, kDip)
[nS, dS, uS] = f_group(0.0, loCut, kDip)
[nS2, dS2, uS2] = f_group(0.0, lo2, kDip)
[nM, dM, uM] = f_group(loCut + 0.0001, hiCut - 0.0001, kDip)
// the state the current bar is in; the strongest tiers are used only when they have enough history
bool strongL = isLongZone and rk >= hi2 and nL2 >= 30 * bpd
bool strongS = isShortZone and rk <= lo2 and nS2 >= 30 * bpd
float dZ = strongL ? dL2 : isLongZone ? dL : strongS ? dS2 : isShortZone ? dS : dM
float uZ = strongL ? uL2 : isLongZone ? uL : strongS ? uS2 : isShortZone ? uS : uM
// a number is highlighted only when it is clearly above its own normal level
bool dHot = ready and dZ >= dA + 5
bool uHot = ready and uZ >= uA + 5
float mv = kDip * unitNow
float pxD = close * (1 - mv)
float pxU = close * (1 + mv)
string mTx = str.tostring(100.0 * mv, "0.0") + "%"
// ── Forecast zones on the price chart ──
box.delete(bxD)
box.delete(bxU)
label.delete(lbD)
label.delete(lbU)
if showZone and ready
int x1 = bar_index + 2
int x2 = x1 + math.min(300, math.max(hBars, 8))
bxD := box.new(x1, close, x2, pxD, border_color = color.new(COL_RISK, 30), border_width = 1, bgcolor = color.new(COL_RISK, math.max(35, 100 - dZ * 0.7)), force_overlay = true)
bxU := box.new(x1, pxU, x2, close, border_color = color.new(COL_CALM, 30), border_width = 1, bgcolor = color.new(COL_CALM, math.max(35, 100 - uZ * 0.7)), force_overlay = true)
// the numbers sit outside the zones, at the two price levels, so they stay readable at any zoom
lbD := label.new(x2, pxD, "▼ " + f_p(dZ) + " -" + mTx + " " + str.tostring(pxD, format.mintick), style = label.style_label_upper_left, color = color.new(COL_RISK, dHot ? 0 : 55), textcolor = dHot ? COL_BG : COL_TEXT, size = SZ_MID, force_overlay = true)
lbU := label.new(x2, pxU, "▲ " + f_p(uZ) + " +" + mTx + " " + str.tostring(pxU, format.mintick), style = label.style_label_lower_left, color = color.new(COL_CALM, uHot ? 0 : 55), textcolor = uHot ? COL_BG : COL_TEXT, size = SZ_MID, force_overlay = true)
// ── Card ──
table.delete(tb)
if showTbl
if tblWhere == "Price chart"
tb := table.new(f_pos(tblPos), COLS, 28, bgcolor = color.new(COL_BG, 6), border_color = COL_BG, border_width = 2, frame_color = color.new(COL_MID, 55), frame_width = 1, force_overlay = true)
else
tb := table.new(f_pos(tblPos), COLS, 28, bgcolor = color.new(COL_BG, 6), border_color = COL_BG, border_width = 2, frame_color = color.new(COL_MID, 55), frame_width = 1)
int row = 0
if not tfOk
f_line(row, JA ? "1時間足〜日足で使ってください" : "Use a chart from 1h to 1D", COL_RISK, SZ_MID)
else if na(lsr)
f_line(row, JA ? "この銘柄にはデータがありません\n(暗号資産の無期限先物で使ってください)" : "No data for this symbol\n(use a crypto perpetual contract)", COL_RISK, SZ_MID)
else if not ready
f_line(row, JA ? "履歴が足りません\n(もっと長い時間足で試してください)" : "Not enough history yet\n(try a higher timeframe)", COL_RISK, SZ_MID)
else
string dWin = JA ? "今後" + str.tostring(hDays) + "日" : "NEXT " + str.tostring(hDays) + (hDays == 1 ? " DAY" : " DAYS")
color stCol = isLongZone ? COL_RISK : isShortZone ? COL_CALM : COL_MID
string state = strongL ? (JA ? "買いに強く偏り" : "STRONGLY LONG-HEAVY") : isLongZone ? (JA ? "買いに偏り" : "LONG-HEAVY") : strongS ? (JA ? "売りに強く偏り" : "STRONGLY SHORT-HEAVY") : isShortZone ? (JA ? "売りに偏り" : "SHORT-HEAVY") : (JA ? "偏りなし" : "BALANCED")
// header: title on the left, crowd state as a coloured badge on the right
f_cell(0, row, JA ? "値動き予報 · " + dWin : "MOVE FORECAST · " + dWin, COL_MID, color(na), SZ_SML, text.align_left)
table.merge_cells(tb, 0, row, 1, row)
f_cell(2, row, state, isLongZone or isShortZone ? COL_BG : COL_TEXT, isLongZone or isShortZone ? color.new(stCol, 0) : color.new(COL_MID, 70), SZ_SML, text.align_center)
table.merge_cells(tb, 2, row, COLS - 1, row)
row += 1
f_main(row, JA ? "▼ 下げ -" + mTx + " 以上" : "▼ DIP -" + mTx + " or deeper", dZ, dA, COL_RISK, dHot)
row += 1
f_main(row, JA ? "▲ 上げ +" + mTx + " 以上" : "▲ RALLY +" + mTx + " or higher", uZ, uA, COL_CALM, uHot)
row += 1
f_line(row, (JA ? "▼印=ふだんの水準(下げ " : "▼ mark = normal level (dip ") + f_p(dA) + (JA ? "・上げ " : ", rally ") + f_p(uA) + (JA ? ")" : ")"), COL_MID, SZ_SML)
row += 1
f_line(row, (JA ? "目安の価格 " : "Price levels ") + str.tostring(pxD, format.mintick) + " / " + str.tostring(pxU, format.mintick) + (JA ? " · 順位 " : " · rank ") + str.tostring(rk, "0") + "/100", COL_MID, SZ_SML)
if showRec
// how past setups on this chart ended
row += 1
f_line(row, (JA ? "▼ 売りの合図 " : "▼ Sell setups ") + str.tostring(cAll.get(0)) + (JA ? "回 · 利確 " : " · target ") + str.tostring(cTP.get(0)) + (JA ? " · 損切り " : " · stop ") + str.tostring(cSL.get(0)) + (JA ? " · 時間切れ " : " · time exit ") + str.tostring(cTime.get(0)), COL_MID, SZ_SML)
row += 1
f_line(row, (JA ? "▲ 買いの合図 " : "▲ Buy setups ") + str.tostring(cAll.get(1)) + (JA ? "回 · 利確 " : " · target ") + str.tostring(cTP.get(1)) + (JA ? " · 損切り " : " · stop ") + str.tostring(cSL.get(1)) + (JA ? " · 時間切れ " : " · time exit ") + str.tostring(cTime.get(1)), COL_MID, SZ_SML)
for k = 0 to 1
if tIn.get(k)
row += 1
f_line(row, (k == 0 ? (JA ? "▼ 売りの合図が進行中 · 利確 " : "▼ Sell setup open · target ") : (JA ? "▲ 買いの合図が進行中 · 利確 " : "▲ Buy setup open · target ")) + str.tostring(tTP.get(k), format.mintick) + (JA ? " · 損切り " : " · stop ") + str.tostring(tSL.get(k), format.mintick) + (JA ? " · 残り " : " · bars left ") + str.tostring(math.max(0, hBars - (bar_index - tBar.get(k)))) + (JA ? "本" : ""), k == 0 ? COL_RISK : COL_CALM, SZ_SML)
if showDet
row += 1
f_line(row, JA ? "▼ 下げが起きた割合(偏りの状態ごと・足の本数)" : "▼ Dip frequency by crowd state (bars counted)", COL_TEXT, SZ_SML)
row += 1
f_row(row, (JA ? "買いに強く偏り " : "Strongly long-heavy ") + str.tostring(nL2), dL2, dA, COL_RISK)
row += 1
f_row(row, (JA ? "買いに偏り " : "Long-heavy ") + str.tostring(nL), dL, dA, COL_RISK)
row += 1
f_row(row, (JA ? "中間 " : "Middle ") + str.tostring(nM), dM, dA, COL_MID)
row += 1
f_row(row, (JA ? "売りに偏り " : "Short-heavy ") + str.tostring(nS), dS, dA, COL_CALM)
row += 1
f_line(row, JA ? "▲ 上げが起きた割合(偏りの状態ごと・足の本数)" : "▲ Rally frequency by crowd state (bars counted)", COL_TEXT, SZ_SML)
row += 1
f_row(row, (JA ? "買いに偏り " : "Long-heavy ") + str.tostring(nL), uL, uA, COL_RISK)
row += 1
f_row(row, (JA ? "中間 " : "Middle ") + str.tostring(nM), uM, uA, COL_MID)
row += 1
f_row(row, (JA ? "売りに偏り " : "Short-heavy ") + str.tostring(nS), uS, uA, COL_CALM)
row += 1
f_row(row, (JA ? "売りに強く偏り " : "Strongly short-heavy ") + str.tostring(nS2), uS2, uA, COL_CALM)
if showNote
row += 1
f_line(row, JA ? "同じ状態だった過去の足のうち、期間内に一度でもその価格まで\n動いた割合。両方起きることもあります。" : "Share of past bars in the same state after which price reached that\nlevel at least once. Both can happen in one window.", COL_MID, SZ_SML)
// ─────────────────────────────────────────────────────────────────────────────
// Alerts
// ─────────────────────────────────────────────────────────────────────────────
alertcondition(sigSell, "Sell setup (crowd strongly long-heavy)", "Crowd Gauge: sell setup. The crowd is strongly long-heavy. See the chart for the target and the stop.")
alertcondition(sigBuy, "Buy setup (crowd strongly short-heavy)", "Crowd Gauge: buy setup. The crowd is strongly short-heavy. See the chart for the target and the stop.")
alertcondition(barstate.isconfirmed and isLongZone and not isLongZone[1], "Crowd became long-heavy", "Crowd Gauge: the long/short accounts ratio entered its long-heavy zone.")
alertcondition(barstate.isconfirmed and isShortZone and not isShortZone[1], "Crowd became short-heavy", "Crowd Gauge: the long/short accounts ratio entered its short-heavy zone.")
alertcondition(barstate.isconfirmed and not isLongZone and isLongZone[1], "Crowd is no longer long-heavy", "Crowd Gauge: the long/short accounts ratio left its long-heavy zone.")
alertcondition(barstate.isconfirmed and not isShortZone and isShortZone[1], "Crowd is no longer short-heavy", "Crowd Gauge: the long/short accounts ratio left its short-heavy zone.")