本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Options OI Profile on Futures (v7 - fast + clean, redraws when levels drift)
// Draws ETF open interest by strike as a profile on NQ / ES, converted live:
// futures level = strike * (futures price / ETF price). Analysis only - not financial advice.
indicator("Options OI Profile on Futures", shorttitle = "OI Profile", overlay = true, max_boxes_count = 500, max_lines_count = 50, max_labels_count = 50)
// ---------------- Data ----------------
etfSym = input.symbol("BATS:QQQ", "Options ETF (QQQ for NQ, SPY for ES)", group = "Data")
oiData = input.text_area("", "OI data (strike:callOI:putOI,...)", group = "Data")
dataDate = input.string("", "Data date", group = "Data")
testMode = input.bool(false, "Test mode (fake OI)", group = "Data")
useManual = input.bool(false, "Use manual ratio", group = "Data")
manualRatio = input.float(0.0, "Manual ratio (futures / ETF)", group = "Data")
// ---------------- Layout ----------------
posMode = input.string("Right of price", "Profile position", options = ["Right of price", "Left"], group = "Layout")
gapBars = input.int(5, "Gap from price (bars)", minval = 0, maxval = 100, group = "Layout")
leftBack = input.int(150, "Left mode: bars back from price", minval = 20, maxval = 4000, group = "Layout")
maxLen = input.int(30, "Longest bar (bars)", minval = 5, maxval = 150, group = "Layout")
thick = input.float(0.7, "Bar thickness", minval = 0.1, maxval = 1.0, group = "Layout")
minPct = input.float(10.0, "Hide strikes under % of biggest OI", minval = 0, maxval = 90, group = "Layout")
redrawPts = input.float(1.0, "Redraw when levels move more than (points)", minval = 0.25, group = "Layout")
// ---------------- Walls ----------------
topN = input.int(2, "Highlight top N call and put walls", minval = 0, maxval = 5, group = "Walls")
lineBack = input.int(60, "Wall line length into chart (bars, 0 = off)", minval = 0, maxval = 2000, group = "Walls")
showLbl = input.bool(true, "Show wall labels", group = "Walls")
// ---------------- Style ----------------
callCol = input.color(color.new(color.teal, 75), "Call OI", group = "Style")
putCol = input.color(color.new(color.red, 75), "Put OI", group = "Style")
callWallCol = input.color(color.new(color.teal, 15), "Call wall", group = "Style")
putWallCol = input.color(color.new(color.red, 15), "Put wall", group = "Style")
showPanel = input.bool(false, "Always show info panel", group = "Style")
// ---------------- Live ratio ----------------
etfTicker = ticker.new(syminfo.prefix(etfSym), syminfo.ticker(etfSym), session.extended)
etfGap = request.security(etfTicker, timeframe.period, close, gaps = barmerge.gaps_on)
etfAny = request.security(etfTicker, timeframe.period, close)
var float liveRatio = na
if not na(etfGap) and etfGap > 0
liveRatio := close / etfGap
float fallbackRatio = not na(etfAny) and etfAny > 0 ? close / etfAny : na
float autoRatio = not na(liveRatio) ? liveRatio : fallbackRatio
float r = useManual and manualRatio > 0 ? manualRatio : autoRatio
hiRange = ta.highest(high, 300)
loRange = ta.lowest(low, 300)
// ---------------- Drawing (only once per bar = no lag) ----------------
var array<box> boxes = array.new<box>()
var array<line> lines = array.new<line>()
var array<label> labels = array.new<label>()
var table info = table.new(position.bottom_right, 2, 4, bgcolor = color.new(color.black, 60))
var float drawnR = na
ratioMoved = not na(r) and not na(drawnR) and not na(etfAny) and math.abs(r - drawnR) * etfAny > redrawPts
doDraw = barstate.islastconfirmedhistory or (barstate.isrealtime and (barstate.isnew or ratioMoved))
if doDraw
drawnR := r
for b in boxes
box.delete(b)
boxes.clear()
for l in lines
line.delete(l)
lines.clear()
for lb in labels
label.delete(lb)
labels.clear()
strikes = array.new<float>()
calls = array.new<float>()
puts = array.new<float>()
if testMode
if not na(etfAny)
base = math.round(etfAny)
for j = -12 to 12
bigC = j >= 0 and j % 4 == 0
bigP = j <= 0 and j % 4 == 0
strikes.push(float(base + j))
calls.push(bigC ? 50000.0 : j >= 0 ? 9000.0 : 3000.0)
puts.push(bigP ? 50000.0 : j <= 0 ? 9000.0 : 3000.0)
else
for item in str.split(oiData, ",")
parts = str.split(str.trim(item), ":")
if parts.size() >= 3
s = str.tonumber(str.trim(parts.get(0)))
if not na(s)
strikes.push(s)
calls.push(nz(str.tonumber(str.trim(parts.get(1)))))
puts.push(nz(str.tonumber(str.trim(parts.get(2)))))
n = strikes.size()
int inRange = 0
string status = "OK"
if na(r)
status := "No ratio - check ETF symbol"
else if n == 0
status := "Paste OI data in settings"
else
maxOI = math.max(calls.max(), puts.max())
minOI = maxOI * minPct / 100
float step = 1.0
if n > 1
sorted = strikes.copy()
sorted.sort()
step := na
for i = 1 to n - 1
d = sorted.get(i) - sorted.get(i - 1)
if d > 0 and (na(step) or d < step)
step := d
step := nz(step, 1.0)
halfH = step * r * thick / 2
// Mark the top N walls on each side
isCWall = array.new<bool>(n, false)
isPWall = array.new<bool>(n, false)
if topN > 0
cIdx = calls.sort_indices(order.descending)
pIdx = puts.sort_indices(order.descending)
for k = 0 to math.min(topN, n) - 1
isCWall.set(cIdx.get(k), true)
isPWall.set(pIdx.get(k), true)
isRight = posMode == "Right of price"
x0 = isRight ? bar_index + gapBars : math.max(0, bar_index - leftBack)
for i = 0 to n - 1
y = strikes.get(i) * r
c = calls.get(i)
p = puts.get(i)
cw = isCWall.get(i)
pw = isPWall.get(i)
if y >= loRange and y <= hiRange
inRange += 1
cLen = maxOI > 0 ? math.max(1, int(math.round(c / maxOI * maxLen))) : 1
pLen = maxOI > 0 ? math.max(1, int(math.round(p / maxOI * maxLen))) : 1
if c >= minOI or cw
boxes.push(box.new(x0, y + halfH, x0 + cLen, y, xloc = xloc.bar_index, bgcolor = cw ? callWallCol : callCol, border_width = 0))
if p >= minOI or pw
boxes.push(box.new(x0, y, x0 + pLen, y - halfH, xloc = xloc.bar_index, bgcolor = pw ? putWallCol : putCol, border_width = 0))
if cw or pw
wallCol = cw ? callWallCol : putWallCol
lineStart = isRight ? math.max(0, bar_index - lineBack) : x0
lineEnd = isRight ? x0 : bar_index
if lineBack > 0
lines.push(line.new(lineStart, y, lineEnd, y, xloc = xloc.bar_index, color = wallCol, style = line.style_dotted, width = 1))
if showLbl
tag = cw and pw ? "C+P " : cw ? "C " : "P "
labels.push(label.new(x0 + math.max(cLen, pLen) + 1, y, tag + str.tostring(strikes.get(i)) + " " + str.tostring(y, "#.##"), xloc = xloc.bar_index, style = label.style_label_left, color = color.new(color.black, 100), textcolor = color.new(wallCol, 0), size = size.small))
if inRange == 0
status := "Off-screen: levels " + str.tostring(strikes.min() * r, "#") + " - " + str.tostring(strikes.max() * r, "#")
if showPanel or status != "OK"
dateText = testMode ? "TEST DATA" : dataDate == "" ? "-" : dataDate
table.cell(info, 0, 0, "ETF", text_color = color.white, text_size = size.tiny)
table.cell(info, 1, 0, etfSym, text_color = color.white, text_size = size.tiny)
table.cell(info, 0, 1, "Ratio", text_color = color.white, text_size = size.tiny)
table.cell(info, 1, 1, na(r) ? "none" : str.tostring(r, "#.####"), text_color = color.white, text_size = size.tiny)
table.cell(info, 0, 2, "OI date", text_color = color.white, text_size = size.tiny)
table.cell(info, 1, 2, dateText, text_color = color.white, text_size = size.tiny)
table.cell(info, 0, 3, "Status", text_color = color.white, text_size = size.tiny)
table.cell(info, 1, 3, status, text_color = status == "OK" ? color.lime : color.orange, text_size = size.tiny)
else
table.clear(info, 0, 0, 1, 3)