本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
indicator("Open Interest Multi-Symbol Screener (13 Symbols, Timeframe Independent)", overlay = true, max_bars_back = 5000, dynamic_requests = true)
import TradingView/LibraryCOT/6 as cot
//--------------------------------------------------------------------
// General settings
grpG = "General"
dataTF = input.timeframe("1W", "Data Timeframe (fixed, independent of chart)", group = grpG,
tooltip = "Timeframe on which the CFTC COT Open Interest is requested (current and previous period).\nIndependent of the chart timeframe - values are identical on every chart.\nCOT reports are published weekly, therefore 1W is recommended.\nAlso used for the columns 'OI Trend' and 'Δ Prev. Period'.")
inclOptions = input.bool(false, "Include Options (Futures + Options)", group = grpG,
tooltip = "Off: COT report 'Futures Only'.\nOn: COT report 'Futures and Options Combined' (options converted to futures-equivalent positions).\nAffects only the COT columns (OI Trend, OI Value, Δ Prev. Period), not 'Symbol OI (1D)'.")
tabPosOption = input.string("top_right", "Table Position", options = ["top_right","top_left","bottom_right","bottom_left","middle_right","middle_left","top_center","bottom_center","middle_center"], group = grpG,
tooltip = "Position of the screener table on the chart.")
headerText = input.string("Symbol", "Header Text (Column 1)", group = grpG,
tooltip = "Free text shown in the header cell of the first column (symbol column).")
stopOnNoOI = input.bool(false, "Stop Script on Missing OI (Runtime Error)", group = grpG,
tooltip = "Off: symbols without exchange Open Interest are shown with status 'Error: No Open Interest', the script keeps running.\nOn: the script stops with a runtime error listing all symbols without Open Interest.")
//--------------------------------------------------------------------
// Color settings
grpC = "Colors"
colRising = input.color(#4CAF50, "Background OI Rising (↑)", group = grpC,
tooltip = "Background color of the 'OI Trend' cell when the COT Open Interest is higher than in the previous period.\nAlso used as background of the 'Status' cell for status 'OK'.")
colFalling = input.color(#F23645, "Background OI Falling (↓)", group = grpC,
tooltip = "Background color of the 'OI Trend' cell when the COT Open Interest is lower than in the previous period.\nAlso used as background of the 'Status' cell for errors.")
txtColData = input.color(#000000, "Text Color Symbol / OI Value / Δ Prev. Period / Symbol OI", group = grpC,
tooltip = "Text color of the data columns: Symbol, OI Value, Δ Prev. Period and Symbol OI (1D).")
txtColSig = input.color(#000000, "Text Color OI Trend / Status", group = grpC,
tooltip = "Text color of the signal columns: OI Trend (arrows) and Status.")
//--------------------------------------------------------------------
// COT report type fixed to Legacy (no selection)
COT_TYPE = "Legacy"
//--------------------------------------------------------------------
// Symbols (input.symbol) + optional CFTC code (takes priority)
grpS = "Symbols (Symbol / optional CFTC Code)"
ttSym = "Symbol: any futures contract (continuous e.g. CL1! or specific month e.g. CLZ2026). Used for the column 'Symbol OI (1D)' (exchange Open Interest of exactly this symbol) and for the automatic CFTC code lookup.\nCFTC: optional CFTC contract market code (e.g. 067651 for Crude Oil). If filled in, it takes priority over the automatic lookup. Leave empty to use the built-in code table. Unknown symbols without a code show status 'No COT Code'."
sym1 = input.symbol("NYMEX:CL1!", "Symbol 1", inline = "1", group = grpS), cftcIn1 = input.string("", "CFTC", inline = "1", group = grpS, tooltip = ttSym)
sym2 = input.symbol("COMEX:GC1!", "Symbol 2", inline = "2", group = grpS), cftcIn2 = input.string("", "CFTC", inline = "2", group = grpS, tooltip = ttSym)
sym3 = input.symbol("COMEX:SI1!", "Symbol 3", inline = "3", group = grpS), cftcIn3 = input.string("", "CFTC", inline = "3", group = grpS, tooltip = ttSym)
sym4 = input.symbol("NYMEX:HO1!", "Symbol 4", inline = "4", group = grpS), cftcIn4 = input.string("", "CFTC", inline = "4", group = grpS, tooltip = ttSym)
sym5 = input.symbol("CBOT:KE1!", "Symbol 5", inline = "5", group = grpS), cftcIn5 = input.string("", "CFTC", inline = "5", group = grpS, tooltip = ttSym)
sym6 = input.symbol("ICEUS:KC1!", "Symbol 6", inline = "6", group = grpS), cftcIn6 = input.string("", "CFTC", inline = "6", group = grpS, tooltip = ttSym)
sym7 = input.symbol("CME_MINI:NQ1!", "Symbol 7", inline = "7", group = grpS), cftcIn7 = input.string("", "CFTC", inline = "7", group = grpS, tooltip = ttSym)
sym8 = input.symbol("NYMEX:NG1!", "Symbol 8", inline = "8", group = grpS), cftcIn8 = input.string("", "CFTC", inline = "8", group = grpS, tooltip = ttSym)
sym9 = input.symbol("CME_MINI:ES1!", "Symbol 9", inline = "9", group = grpS), cftcIn9 = input.string("", "CFTC", inline = "9", group = grpS, tooltip = ttSym)
sym10 = input.symbol("CBOT:ZW1!", "Symbol 10", inline = "10", group = grpS), cftcIn10 = input.string("", "CFTC", inline = "10", group = grpS, tooltip = ttSym)
sym11 = input.symbol("CBOT:ZC1!", "Symbol 11", inline = "11", group = grpS), cftcIn11 = input.string("", "CFTC", inline = "11", group = grpS, tooltip = ttSym)
sym12 = input.symbol("CBOT:ZS1!", "Symbol 12", inline = "12", group = grpS), cftcIn12 = input.string("", "CFTC", inline = "12", group = grpS, tooltip = ttSym)
sym13 = input.symbol("CME:LE1!", "Symbol 13", inline = "13", group = grpS), cftcIn13 = input.string("", "CFTC", inline = "13", group = grpS, tooltip = ttSym)
//--------------------------------------------------------------------
// Helper functions
f_table_pos(pos) =>
pos == "top_right" ? position.top_right :
pos == "top_left" ? position.top_left :
pos == "bottom_right" ? position.bottom_right :
pos == "bottom_left" ? position.bottom_left :
pos == "middle_right" ? position.middle_right :
pos == "middle_left" ? position.middle_left :
pos == "top_center" ? position.top_center :
pos == "bottom_center" ? position.bottom_center :
position.middle_center
// Root of a futures ticker, e.g. "CME_MINI_DL:NQ1!" -> "NQ", "CBOT:ZCZ2026" -> "ZC"
f_root(sym) =>
parts = str.split(str.upper(str.trim(sym)), ":")
string tk = array.size(parts) > 0 ? array.get(parts, array.size(parts) - 1) : sym
if str.endswith(tk, "!")
tk := str.substring(tk, 0, str.length(tk) - 2) // continuous: "NQ1!" -> "NQ"
else if str.match(tk, "[FGHJKMNQUVXZ][0-9]{4}$") != ""
tk := str.substring(tk, 0, str.length(tk) - 5) // "ZCZ2026" -> "ZC"
else if str.match(tk, "[FGHJKMNQUVXZ][0-9]{2}$") != ""
tk := str.substring(tk, 0, str.length(tk) - 3) // "ZCZ26" -> "ZC"
tk
// Own CFTC code table (verified against CFTC public reporting data).
// Unknown roots return "" -> row shows "No COT Code", script keeps running.
f_root_to_cftc(root) =>
switch root
// Currencies
'6A' => '232741'
'AUD' => '232741'
'6B' => '096742'
'GBP' => '096742'
'6C' => '090741'
'CAD' => '090741'
'6E' => '099741'
'EUR' => '099741'
'6J' => '097741'
'JPY' => '097741'
'6L' => '102741'
'BRL' => '102741'
'6M' => '095741'
'MXN' => '095741'
'6N' => '112741'
'NZD' => '112741'
'6S' => '092741'
'CHF' => '092741'
'6Z' => '122741'
'ZAR' => '122741'
'DX' => '098662'
'BTC' => '133741'
'ETH' => '146021'
// Stock indices
'ES' => '13874A'
'NQ' => '209742'
'YM' => '124603'
'RTY' => '239742'
'NKD' => '240741'
'VX' => '1170E1'
// Energy
'CL' => '067651'
'NG' => '023651'
'HO' => '022651'
'RB' => '111659'
// Metals
'GC' => '088691'
'SI' => '084691'
'HG' => '085692'
'PL' => '076651'
'PA' => '075651'
'ALI' => '191691'
'LTH' => '189691'
// Grains & oilseeds
'ZC' => '002602'
'C' => '002602'
'ZW' => '001602'
'W' => '001602'
'ZS' => '005602'
'S' => '005602'
'ZM' => '026603'
'ZL' => '007601'
'ZO' => '004603'
'ZR' => '039601'
'KE' => '001612'
'RS' => '135731'
// Softs
'KC' => '083731'
'SB' => '080732'
'CC' => '073732'
'CT' => '033661'
'OJ' => '040701'
'LBR' => '058644'
'LBS' => '058644'
// Livestock & dairy
'LE' => '057642'
'GF' => '061641'
'HE' => '054642'
'DC' => '052641'
// Interest rates
'ZT' => '042601'
'ZF' => '044601'
'ZN' => '043602'
'ZB' => '020601'
'UB' => '020604'
'SR3' => '134741'
=> ''
// CFTC code: manual input takes priority, otherwise lookup via own code table
f_get_cftc_code(sym, cftcDirect) =>
string code = str.trim(cftcDirect)
if code == ""
code := f_root_to_cftc(f_root(sym))
code
// Display name in table: ticker without exchange prefix (e.g. "NYMEX:CL1!" -> "CL1!")
f_display_name(sym) =>
parts = str.split(sym, ":")
array.size(parts) > 0 ? array.get(parts, array.size(parts) - 1) : sym
// Fetch COT Open Interest on the FIXED data timeframe (current + previous period).
f_open_interest(code) =>
float cur = na
float prev = na
if code != "" // COTTickerid() (v6) raises an error on empty code -> guard
tid = cot.COTTickerid(COT_TYPE, code, inclOptions, "Open Interest", "No direction", "All")
[c, p] = request.security(tid, dataTF, [close, close[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
cur := c
prev := p
[cur, prev]
f_oi_trend(cur, prev) =>
na(cur) or na(prev) ? "-" : cur > prev ? "↑" : cur < prev ? "↓" : "="
f_oi_color(cur, prev) =>
na(cur) or na(prev) ? color.gray : cur > prev ? colRising : cur < prev ? colFalling : color.silver
f_delta_text(cur, prev) =>
if na(cur) or na(prev) or prev == 0
"-"
else
d = cur - prev
pct = d / prev * 100
str.tostring(d, "+#,###;-#,###") + " (" + str.tostring(pct, "+0.0;-0.0") + "%)"
// OI ticker of the selected symbol - identical to the TradingView "Open interest" indicator:
// str.format("{0}_OI", ticker.standard(symbol)), e.g. "NYMEX:CL1!" -> "NYMEX:CL1!_OI"
f_oi_ticker(sym) =>
str.format("{0}_OI", ticker.standard(sym))
// Daily Open Interest of the SELECTED symbol, calculated exactly like the
// TradingView "Open interest" indicator: request.security(tickerOI, "1D", close)
// with default settings (gaps off, lookahead off) -> identical on every chart timeframe.
f_symbol_oi(sym) =>
float cur = na
float prev = na
if str.trim(sym) != ""
[c, p] = request.security(f_oi_ticker(sym), "1D", [close, close[1]], ignore_invalid_symbol = true)
cur := c
prev := p
[cur, prev]
f_exch_text(cur, prev) =>
if na(cur)
"-"
else if na(prev) or prev == 0
str.tostring(cur, "#,###")
else
str.tostring(cur, "#,###") + " (" + str.tostring((cur - prev) / prev * 100, "+0.0;-0.0") + "%)"
//--------------------------------------------------------------------
// Determine CFTC codes
c1 = f_get_cftc_code(sym1, cftcIn1)
c2 = f_get_cftc_code(sym2, cftcIn2)
c3 = f_get_cftc_code(sym3, cftcIn3)
c4 = f_get_cftc_code(sym4, cftcIn4)
c5 = f_get_cftc_code(sym5, cftcIn5)
c6 = f_get_cftc_code(sym6, cftcIn6)
c7 = f_get_cftc_code(sym7, cftcIn7)
c8 = f_get_cftc_code(sym8, cftcIn8)
c9 = f_get_cftc_code(sym9, cftcIn9)
c10 = f_get_cftc_code(sym10, cftcIn10)
c11 = f_get_cftc_code(sym11, cftcIn11)
c12 = f_get_cftc_code(sym12, cftcIn12)
c13 = f_get_cftc_code(sym13, cftcIn13)
// COT Open Interest (current / previous period) on fixed timeframe
[oi1, p1] = f_open_interest(c1)
[oi2, p2] = f_open_interest(c2)
[oi3, p3] = f_open_interest(c3)
[oi4, p4] = f_open_interest(c4)
[oi5, p5] = f_open_interest(c5)
[oi6, p6] = f_open_interest(c6)
[oi7, p7] = f_open_interest(c7)
[oi8, p8] = f_open_interest(c8)
[oi9, p9] = f_open_interest(c9)
[oi10, p10] = f_open_interest(c10)
[oi11, p11] = f_open_interest(c11)
[oi12, p12] = f_open_interest(c12)
[oi13, p13] = f_open_interest(c13)
// Daily Open Interest of the selected symbol
[ex1, exp1] = f_symbol_oi(sym1)
[ex2, exp2] = f_symbol_oi(sym2)
[ex3, exp3] = f_symbol_oi(sym3)
[ex4, exp4] = f_symbol_oi(sym4)
[ex5, exp5] = f_symbol_oi(sym5)
[ex6, exp6] = f_symbol_oi(sym6)
[ex7, exp7] = f_symbol_oi(sym7)
[ex8, exp8] = f_symbol_oi(sym8)
[ex9, exp9] = f_symbol_oi(sym9)
[ex10, exp10] = f_symbol_oi(sym10)
[ex11, exp11] = f_symbol_oi(sym11)
[ex12, exp12] = f_symbol_oi(sym12)
[ex13, exp13] = f_symbol_oi(sym13)
//--------------------------------------------------------------------
// Table (6 columns)
var table t = table.new(f_table_pos(tabPosOption), 6, 14, border_width = 1)
// Returns the symbol name if it has NO Open Interest (for the error message), otherwise ""
f_set_row(sym, code, cur, prev, exCur, exPrev, row) =>
noOI = na(exCur)
valid = not noOI and code != "" and not na(cur)
oiVal = na(cur) ? "-" : str.tostring(cur, "#,###")
status = str.trim(sym) == "" ? "Error: No Symbol" :
noOI ? "Error: No Open Interest" :
code == "" ? "No COT Code" :
na(cur) ? "No COT Data" : "OK"
table.cell(t, 0, row, f_display_name(sym), text_color = txtColData, bgcolor = color.silver, text_size = size.small)
table.cell(t, 1, row, f_oi_trend(cur, prev), text_color = txtColSig, bgcolor = f_oi_color(cur, prev), text_size = size.small)
table.cell(t, 2, row, oiVal, text_color = txtColData, bgcolor = color.silver, text_size = size.small)
table.cell(t, 3, row, f_delta_text(cur, prev), text_color = txtColData, bgcolor = color.silver, text_size = size.small)
table.cell(t, 4, row, f_exch_text(exCur, exPrev), text_color = txtColData, bgcolor = color.silver, text_size = size.small)
table.cell(t, 5, row, status, text_color = txtColSig, bgcolor = valid ? colRising : colFalling, text_size = size.small)
noOI ? f_display_name(sym) : ""
// Draw only on the last bar (faster, identical values on every chart timeframe)
if barstate.islast
table.cell(t, 0, 0, headerText, bgcolor = color.black, text_color = color.white, text_size = size.small)
table.cell(t, 1, 0, "OI Trend (" + dataTF + ")", bgcolor = color.black, text_color = color.white, text_size = size.small)
table.cell(t, 2, 0, "OI Value", bgcolor = color.black, text_color = color.white, text_size = size.small)
table.cell(t, 3, 0, "Δ Prev. Period (" + dataTF + ")", bgcolor = color.black, text_color = color.white, text_size = size.small)
table.cell(t, 4, 0, "Symbol OI (1D)", bgcolor = color.black, text_color = color.white, text_size = size.small)
table.cell(t, 5, 0, "Status", bgcolor = color.black, text_color = color.white, text_size = size.small)
missing = array.new_string()
array.push(missing, f_set_row(sym1, c1, oi1, p1, ex1, exp1, 1))
array.push(missing, f_set_row(sym2, c2, oi2, p2, ex2, exp2, 2))
array.push(missing, f_set_row(sym3, c3, oi3, p3, ex3, exp3, 3))
array.push(missing, f_set_row(sym4, c4, oi4, p4, ex4, exp4, 4))
array.push(missing, f_set_row(sym5, c5, oi5, p5, ex5, exp5, 5))
array.push(missing, f_set_row(sym6, c6, oi6, p6, ex6, exp6, 6))
array.push(missing, f_set_row(sym7, c7, oi7, p7, ex7, exp7, 7))
array.push(missing, f_set_row(sym8, c8, oi8, p8, ex8, exp8, 8))
array.push(missing, f_set_row(sym9, c9, oi9, p9, ex9, exp9, 9))
array.push(missing, f_set_row(sym10, c10, oi10, p10, ex10, exp10, 10))
array.push(missing, f_set_row(sym11, c11, oi11, p11, ex11, exp11, 11))
array.push(missing, f_set_row(sym12, c12, oi12, p12, ex12, exp12, 12))
array.push(missing, f_set_row(sym13, c13, oi13, p13, ex13, exp13, 13))
// Optional: stop the script with a runtime error listing all symbols without OI
if stopOnNoOI
string lst = ""
for s in missing
if s != ""
lst := lst == "" ? s : lst + ", " + s
if lst != ""
runtime.error("No Open Interest available for: " + lst)