本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Open Interest Radar v4.2. Price/OI context, never an assertion about which traders opened positions.
// Native OI source, confirmed-minute changes, matched price comparisons and pivot divergence retained.
// English default; Russian is selectable. Static Pine labels remain English.
// Research-only observations: no positions, broker orders or guaranteed directional signals.
indicator("Open Interest Radar: Price & OI Divergence [Artio]", shorttitle="OI Radar [Artio]", overlay=false, max_bars_back=3000, max_lines_count=40, max_labels_count=40, format=format.volume, precision=2)
string GM = "00 · Core"
string lang = input.string("English", "Language / Язык", options=["English", "Русский"], group=GM)
bool isRu = lang == "Русский"
f_t(string english, string russian) =>
isRu ? russian : english
string oiOverride = input.string("", "OI ticker override (blank = chart symbol + _OI)", group=GM)
string oiUnits = input.string("native", "OI unit label (no conversion)", group=GM)
string arrowHorizon = input.string("AUTO", "Price/OI comparison horizon", options=["AUTO", "5 minutes", "1 hour", "24 hours"], group=GM, tooltip="AUTO: <=5m chart = 5m; intraday above 5m = 1h; daily and above = 24h. Based on closed 1-minute observations; not the length of the chart bar.")
float oiGate = input.float(0.20, "OI movement threshold, %", minval=0.01, maxval=10, step=0.05, group=GM)
float priceGate = input.float(0.05, "Price movement threshold, %", minval=0.001, maxval=5, step=0.01, group=GM)
int oiFreshMinutes = input.int(5, "Max age of closed 1m snapshot (minutes)", minval=2, maxval=60, group=GM, tooltip="Age of the source bar, NOT an exchange timestamp proving when outstanding positions changed.")
bool showOiDivergence = input.bool(true, "Show latest confirmed Price/OI divergence", group=GM)
bool oiOnPrice = input.bool(false, "Draw Price/OI divergence on price chart", group=GM)
bool oiAlerts = input.bool(false, "State-change alerts (confirmed chart bars)", group=GM)
string GV = "01 · Display"
bool showHeader = input.bool(true, "Show status header", group=GV)
bool showTile = input.bool(true, "Show Price + OI direction tile", group=GV)
bool showTileText = input.bool(true, "Show tile caption", group=GV)
bool showLongContext = input.bool(true, "Show 1h / 24h context", group=GV)
bool diagnostics = input.bool(false, "Show diagnostics", group=GV)
int arrowSize = input.int(26, "Tile arrow size (pt)", minval=18, maxval=36, group=GV)
int fontSize = input.int(11, "Header font size (pt)", minval=8, maxval=16, group=GV)
color panelBg = input.color(#111827, "Header background", group=GV)
color panelFg = input.color(#E5E7EB, "Header text", group=GV)
color cNeutral = input.color(#94A3B8, "Neutral / unavailable", group=GV)
color cOI = input.color(#FBBF24, "Open interest line", group=GV)
color cBuy = input.color(#34D399, "Price rises with OI expansion", group=GV)
color cSell = input.color(#FB7185, "Price falls with OI expansion", group=GV)
color cConflict = input.color(#FACC15, "OI contraction / limited price movement", group=GV)
bool openBar = barstate.isrealtime and not barstate.isconfirmed
int cutoff = openBar ? math.min(timenow, time_close) : time_close
string standardSymbol = ticker.standard(syminfo.tickerid)
string oiSymbol = str.length(oiOverride) > 0 ? oiOverride : standardSymbol + "_OI"
f_num(float x) =>
na(x) ? "—" : str.tostring(x, "#.##")
f_signed(float x) =>
na(x) ? "—" : (x > 0 ? "+" : "") + str.tostring(x, "#.##")
f_change(float nowV, float before, int t0, int t1, int spanMs) =>
not na(nowV) and not na(before) and before > 0 and not na(t0) and not na(t1) and t0 - t1 == spanMs ? 100.0 * (nowV / before - 1.0) : na
// These are exact clock intervals in the ONE-MINUTE source. Gaps fail the time equality.
[oiRaw, oiT, oi5, oi5T, oiH, oiHT, oiD, oiDT] = request.security(oiSymbol, "1", [close[1], time_close[1], close[6], time_close[6], close[61], time_close[61], close[1441], time_close[1441]], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true, calc_bars_count=10000)
[prRaw, prT, pr5, pr5T, prH, prHT, prD, prDT] = request.security(standardSymbol, "1", [close[1], time_close[1], close[6], time_close[6], close[61], time_close[61], close[1441], time_close[1441]], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true, calc_bars_count=10000)
bool oiOK = not na(oiRaw) and oiRaw >= 0 and not na(oiT) and oiT <= cutoff and cutoff - oiT <= oiFreshMinutes * 60000
float oi5m = oiOK ? f_change(oiRaw, oi5, oiT, oi5T, 300000) : na
float oi1h = oiOK ? f_change(oiRaw, oiH, oiT, oiHT, 3600000) : na
float oi24h = oiOK ? f_change(oiRaw, oiD, oiT, oiDT, 86400000) : na
int autoMinutes = timeframe.in_seconds() <= 300 ? 5 : timeframe.isintraday ? 60 : 1440
int horizonMinutes = arrowHorizon == "AUTO" ? autoMinutes : arrowHorizon == "5 minutes" ? 5 : arrowHorizon == "1 hour" ? 60 : 1440
string horizonText = horizonMinutes == 5 ? "5m" : horizonMinutes == 60 ? "1h" : "24h"
string localizedHorizon = horizonMinutes == 5 ? f_t("5m", "5м") : horizonMinutes == 60 ? f_t("1h", "1ч") : f_t("24h", "24ч")
float beforeOI = horizonMinutes == 5 ? oi5 : horizonMinutes == 60 ? oiH : oiD
int beforeOIT = horizonMinutes == 5 ? oi5T : horizonMinutes == 60 ? oiHT : oiDT
float beforePrice = horizonMinutes == 5 ? pr5 : horizonMinutes == 60 ? prH : prD
int beforePriceT = horizonMinutes == 5 ? pr5T : horizonMinutes == 60 ? prHT : prDT
bool pairOK = oiOK and not na(prRaw) and oiT == prT and beforeOIT == beforePriceT and oiT - beforeOIT == horizonMinutes * 60000
float oiChange = pairOK ? f_change(oiRaw, beforeOI, oiT, beforeOIT, horizonMinutes * 60000) : na
float priceChange = pairOK ? f_change(prRaw, beforePrice, prT, beforePriceT, horizonMinutes * 60000) : na
int oDir = na(oiChange) ? 0 : oiChange >= oiGate ? 1 : oiChange <= -oiGate ? -1 : 0
int pDir = na(priceChange) ? 0 : priceChange >= priceGate ? 1 : priceChange <= -priceGate ? -1 : 0
// 1/-1: expansion + price direction. 3/-3: contraction + price direction (AMBER).
// 2: OI moves but price does not. 0: OI near flat. 9: unavailable.
int oiState = not pairOK or na(oiChange) or na(priceChange) ? 9 : oDir == 0 ? 0 : pDir == 0 ? 2 : oDir > 0 ? pDir : pDir * 3
string oiArrow = oiState == 1 or oiState == 3 ? "↑" : oiState == -1 or oiState == -3 ? "↓" : oiState == 2 ? "↔" : oiState == 9 ? "—" : "→"
color oiTileColor = oiState == 1 ? cBuy : oiState == -1 ? cSell : math.abs(oiState) == 3 or oiState == 2 ? cConflict : cNeutral
string oiName = oiState == 9 ? f_t("NO MATCH", "НЕТ ПАРЫ") : oiState == 0 ? f_t("OI FLAT", "OI РОВНО") : oiState == 2 ? f_t("PRICE FLAT", "ЦЕНА ≈") : math.abs(oiState) == 3 ? f_t("OI CONTRACTS", "OI СЖИМАЕТСЯ") : f_t("OI EXPANDS", "OI РАСТЁТ")
string oiTip = oiSymbol + " | " + oiUnits + ". " + f_t("Matched Price/OI change over ", "Изменение цены и OI за ") + localizedHorizon + ". OI " + f_signed(oiChange) + "%, " + f_t("price ", "цена ") + f_signed(priceChange) + "%. " + f_t("Last closed 1m snapshot: ", "Последний закрытый снимок 1м: ") + (na(oiT) ? "—" : str.format_time(oiT, "HH:mm", "Etc/UTC")) + " UTC. " + f_t("OI growth does not identify new longs versus shorts; OI contraction does not prove liquidations. Amber arrows mean price moved while OI contracted. Context, not a forecast.", "Рост OI не определяет, кто открыл LONG или SHORT. Снижение OI не доказывает ликвидации. Жёлтые стрелки: цена двигалась при сокращении OI. Это контекст, не прогноз.")
string oiChartTF = timeframe.in_seconds() < 60 ? "1" : timeframe.period
[oiBarRaw, oiBarTime, oiBarClose] = request.security(oiSymbol, oiChartTF, [close, time, time_close], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true, calc_bars_count=5000)
bool oiBarOK = not na(oiBarRaw) and oiBarRaw >= 0 and (timeframe.in_seconds() < 60 ? oiBarTime <= time and oiBarClose >= time : oiBarTime == time)
float oiLine = oiBarOK ? oiBarRaw : na
plot(oiLine, "Open interest · native source units", color=cOI, linewidth=2, style=plot.style_linebr, format=format.volume, display=display.pane + display.price_scale + display.data_window)
// OI divergence is a context mismatch. No BUY/SELL scoring, no funding DIV.
int r = timeframe.in_seconds() <= 300 ? 1 : 2
float pl = ta.pivotlow(close, 3, r)
float ph = ta.pivothigh(close, 3, r)
float atrPrev = ta.atr(14)[1]
var int oiSegment = 0
if oiBarOK and not oiBarOK[1]
oiSegment += 1
var float oldL = na
var float oldLO = na
var int oldLT = na
var int oldLB = na
var int oldLS = na
var float oldH = na
var float oldHO = na
var int oldHT = na
var int oldHB = na
var int oldHS = na
var int e1 = na
var int e2 = na
var int ed = na
var float ep1 = na
var float ep2 = na
var float eo1 = na
var float eo2 = na
var float eod = na
var bool eBull = false
float gate = math.max(syminfo.mintick * 2, nz(atrPrev[r]) * 0.05)
if barstate.isconfirmed and not na(pl) and not na(oiLine[r])
bool ok = not na(oldLB) and bar_index - r - oldLB >= 4 and bar_index - r - oldLB <= 60 and oldLS == oiSegment[r] and oiSegment[r] == oiSegment and oiBarOK
if ok and pl < oldL - gate and oiLine[r] > oldLO * 1.0025
e1 := oldLT
e2 := time[r]
ed := time
ep1 := oldL
ep2 := pl
eo1 := oldLO
eo2 := oiLine[r]
eod := oiLine
eBull := true
oldL := pl
oldLO := oiLine[r]
oldLT := time[r]
oldLB := bar_index - r
oldLS := oiSegment[r]
if barstate.isconfirmed and not na(ph) and not na(oiLine[r])
bool ok = not na(oldHB) and bar_index - r - oldHB >= 4 and bar_index - r - oldHB <= 60 and oldHS == oiSegment[r] and oiSegment[r] == oiSegment and oiBarOK
if ok and ph > oldH + gate and oiLine[r] < oldHO * 0.9975
e1 := oldHT
e2 := time[r]
ed := time
ep1 := oldH
ep2 := ph
eo1 := oldHO
eo2 := oiLine[r]
eod := oiLine
eBull := false
oldH := ph
oldHO := oiLine[r]
oldHT := time[r]
oldHB := bar_index - r
oldHS := oiSegment[r]
var line ol = na
var line op = na
var label ot = na
var table header = table.new(position.top_left, 3, 2, bgcolor=na, border_width=0)
var table tile = table.new(position.top_right, 1, 2, bgcolor=na, border_width=0)
if barstate.islast
line.delete(ol)
line.delete(op)
label.delete(ot)
if showOiDivergence and not na(e1) and e1 >= chart.left_visible_bar_time and ed <= chart.right_visible_bar_time
string tip = f_t("Confirmed price pivot vs. OI at those pivot bars. Not CVD divergence, not a buy/sell signal.", "Подтверждённые экстремумы цены и OI в моменты этих экстремумов. Не CVD-дивергенция и не сигнал входа.")
ol := line.new(e1, eo1, e2, eo2, xloc=xloc.bar_time, color=cOI, width=3)
ot := label.new(ed, eod, f_t("OI ≠ PRICE", "OI ≠ ЦЕНА"), xloc=xloc.bar_time, style=eBull ? label.style_label_up : label.style_label_down, color=cOI, textcolor=panelBg, size=size.small, tooltip=tip)
if oiOnPrice
op := line.new(e1, ep1, e2, ep2, xloc=xloc.bar_time, color=cOI, width=2, force_overlay=true)
table.clear(header, 0, 0, 2, 1)
table.clear(tile, 0, 0, 0, 1)
if showHeader
table.cell(header, 0, 0, " OI " + (na(oiLine) ? "—" : str.tostring(oiLine, format.volume)) + " ", text_color=cOI, bgcolor=panelBg, text_size=fontSize, tooltip=oiTip)
table.cell(header, 1, 0, " " + localizedHorizon + " " + f_signed(oiChange) + "% · " + f_t("PRICE ", "ЦЕНА ") + f_signed(priceChange) + "% ", text_color=panelFg, bgcolor=panelBg, text_size=fontSize, tooltip=oiTip)
if showLongContext
table.cell(header, 2, 0, " " + f_t("1h ", "1ч ") + f_signed(oi1h) + "% · " + f_t("24h ", "24ч ") + f_signed(oi24h) + "% ", text_color=cNeutral, bgcolor=panelBg, text_size=fontSize, tooltip=oiTip)
if showTile
table.cell(tile, 0, 0, " " + oiArrow + " ", text_size=arrowSize, text_color=panelBg, bgcolor=oiTileColor, tooltip=oiTip)
if showTileText
table.cell(tile, 0, 1, oiName + " · " + localizedHorizon, text_size=9, text_color=panelBg, bgcolor=oiTileColor, tooltip=oiTip)
if diagnostics
table.cell(header, 0, 1, oiSymbol + " · " + oiUnits, text_color=cNeutral, bgcolor=panelBg, text_size=9)
table.cell(header, 1, 1, f_t("SNAP ", "СНИМОК ") + (na(oiT) ? "—" : str.format_time(oiT, "MM-dd HH:mm", "Etc/UTC")) + " UTC", text_color=cNeutral, bgcolor=panelBg, text_size=9)
plot(oiChange, "DATA · OI change over selected horizon (%)", display=display.data_window, format=format.price)
plot(priceChange, "DATA · Matched price change (%)", display=display.data_window, format=format.price)
plot(oiState, "DATA · Price/OI state: +/-1 expansion, +/-3 contraction, 2 flat price, 0 flat OI, 9 unavailable", display=display.data_window, format=format.price)
plot(oi1h, "DATA · OI change 1h (%)", display=display.data_window, format=format.price)
plot(oi24h, "DATA · OI change 24h (%)", display=display.data_window, format=format.price)
alertcondition(oiAlerts and not isRu and barstate.isconfirmed and oiState == 1 and oiState[1] != 1, "EN · OI expansion + price up", "[Artio] Open Interest Radar: OI expands as price rises. This does not identify buyers or predict direction.")
alertcondition(oiAlerts and not isRu and barstate.isconfirmed and oiState == -1 and oiState[1] != -1, "EN · OI expansion + price down", "[Artio] Open Interest Radar: OI expands as price falls. This does not identify sellers or predict direction.")
alertcondition(oiAlerts and isRu and barstate.isconfirmed and oiState == 1 and oiState[1] != 1, "RU · OI растёт + цена растёт", "[Artio] Open Interest Radar: OI увеличивается при росте цены. Это не доказательство преобладания покупателей.")
alertcondition(oiAlerts and isRu and barstate.isconfirmed and oiState == -1 and oiState[1] != -1, "RU · OI растёт + цена падает", "[Artio] Open Interest Radar: OI увеличивается при снижении цены. Это не доказательство преобладания продавцов.")
// https://www\.tradingview\.com/support/solutions/43000685269-open-interest/
// v4.2: bilingual release. Logic unchanged. Pine Editor compilation / live feed test required.