本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("OptionVision", shorttitle = "OptionVision", overlay = true, max_lines_count = 500, max_labels_count = 500)
G1 = "Signals"
iSig = input.bool(true, "Signal arrows", group = G1)
iHodN = input.int(2, "Signal on premium high-of-day break #", minval = 1, group = G1)
iWin = input.session("0931-1500", "Entry window (ET)", group = G1)
iDays = input.int(2, "Sessions of signals", minval = 1, maxval = 2, group = G1, tooltip = "TradingView allows 40 option contracts per script: 10 per session for signals, 14 for the flow")
iR1 = input.bool(true, "At the money · weekly", group = G1)
iR2 = input.bool(true, "3 strikes out of the money · weekly", group = G1)
iR3 = input.bool(true, "Straddle-sized offset · weekly", group = G1, tooltip = "Strike = price ± 0.6 × (ATM call + ATM put) once both have traded")
iR4 = input.bool(true, "Straddle-sized offset · monthly (21-27 days)", group = G1)
G2 = "Option flow"
iBub = input.bool(true, "Big-order circles", group = G2)
iBubMin = input.float(100000, "Circle when a strike trades ≥ $ in one minute", minval = 10000, step = 25000, group = G2)
iBubX = input.float(4, "…and ≥ this × the session's average strike-minute (0 = off)", minval = 0, step = 0.5, group = G2,
tooltip = "Scales the bar to each stock: SPY / QQQ same-day options trade $100K+ almost every minute")
iZones = input.bool(true, "Strike bars (premium traded today per strike)", group = G2)
iHideITM = input.bool(true, "Hide deep in-the-money strikes (> 3%)", group = G2)
C_UP = #33d69f
C_DOWN = #ff6b7a
C_TEXT = #8290a3
NY = "America/New_York"
DAY = 86400000
rth = not na(time(timeframe.period, "0930-1600", NY))
newDay = dayofmonth(time, NY) != dayofmonth(time[1], NY)
dayStart = rth and (newDay or not rth[1])
isMin = timeframe.period == "1"
inWin = not na(time(timeframe.period, iWin, NY))
strikeStep(float p) =>
syminfo.ticker == "SPY" or syminfo.ticker == "QQQ" ? 1.0 : p < 500 ? 2.5 : 5.0
optT(int expT, string right, float k) =>
"OPRA:" + syminfo.ticker + str.format_time(expT, "yyMMdd", NY) + right + str.tostring(k, "0.0##")
money(float v) =>
v >= 1e6 ? "$" + str.tostring(v / 1e6, v >= 1e7 ? "#" : "0.0") + "M" : "$" + str.tostring(math.round(v / 1e3)) + "K"
hm(int t) =>
str.format_time(t, "HH:mm", NY)
// last trade price of an option set during the day; "" until then (the request returns the stock, filtered out)
optPx(string sym) =>
raw = request.security(sym == "" ? syminfo.tickerid : sym, "1", close, gaps = barmerge.gaps_on, ignore_invalid_symbol = true)
sym != "" and raw < close * 0.5 ? raw : na
// ---------------------------------------------------------------- signal contracts, chosen each morning
// only the latest session(s): every contract requested counts toward TradingView's 40-request limit
lastStart = timestamp(NY, year(last_bar_time, NY), month(last_bar_time, NY), dayofmonth(last_bar_time, NY), 0, 0)
prevStart = lastStart - (dayofweek(last_bar_time, NY) == dayofweek.monday ? 3 : 1) * DAY
sigOn = isMin and time >= (iDays >= 2 ? prevStart : lastStart)
var float st = na
var float k0 = na
var int wkExp = 0
var int moExp = 0
var float kOC = na
var float kOP = na
var float kSC = na
var float kSP = na
var float kMC = na
var float kMP = na
var string sAC = ""
var string sAP = ""
var string sOC = ""
var string sOP = ""
var string sMAC = ""
var string sMAP = ""
var string sSC = ""
var string sSP = ""
var string sMC = ""
var string sMP = ""
if dayStart
st := strikeStep(open)
k0 := math.round(open / st) * st
wkExp := time + (6 - dayofweek(time, NY)) * DAY
moExp := wkExp + 21 * DAY
kOC := k0 + 3 * st
kOP := k0 - 3 * st
sAC := sigOn ? optT(wkExp, "C", k0) : ""
sAP := sigOn ? optT(wkExp, "P", k0) : ""
sOC := sigOn ? optT(wkExp, "C", kOC) : ""
sOP := sigOn ? optT(wkExp, "P", kOP) : ""
sMAC := sigOn ? optT(moExp, "C", k0) : ""
sMAP := sigOn ? optT(moExp, "P", k0) : ""
sSC := ""
sSP := ""
sMC := ""
sMP := ""
pAC = optPx(sAC)
pAP = optPx(sAP)
pOC = optPx(sOC)
pOP = optPx(sOP)
pMAC = optPx(sMAC)
pMAP = optPx(sMAP)
pSC = optPx(sSC)
pSP = optPx(sSP)
pMC = optPx(sMC)
pMP = optPx(sMP)
// straddle-sized strikes: once the at-the-money call and put have both traded
if sigOn and rth and sSC == "" and not na(pAC) and not na(pAP)
off = 0.6 * (pAC + pAP)
kSC := math.max(k0 + st, math.round((close + off) / st) * st)
kSP := math.min(k0 - st, math.round((close - off) / st) * st)
sSC := optT(wkExp, "C", kSC)
sSP := optT(wkExp, "P", kSP)
if sigOn and rth and sMC == "" and not na(pMAC) and not na(pMAP)
off = 0.6 * (pMAC + pMAP)
kMC := math.max(k0 + st, math.round((close + off) / st) * st)
kMP := math.min(k0 - st, math.round((close - off) / st) * st)
sMC := optT(moExp, "C", kMC)
sMP := optT(moExp, "P", kMP)
// ---------------------------------------------------------------- premium high-of-day breaks
// A break = a minute close at a new premium high after a minute that was not a high. Break #N inside the entry
// window fires the signal, once per contract per day. Uses option trade prices.
type Book
float hod = na
bool prevHigh = true
int breaks = 0
bool fired = false
var bAC = Book.new()
var bAP = Book.new()
var bOC = Book.new()
var bOP = Book.new()
var bSC = Book.new()
var bSP = Book.new()
var bMC = Book.new()
var bMP = Book.new()
if dayStart
bAC := Book.new()
bAP := Book.new()
bOC := Book.new()
bOP := Book.new()
bSC := Book.new()
bSP := Book.new()
bMC := Book.new()
bMP := Book.new()
fires(Book b, float px, bool on) =>
bool f = false
if not na(px)
first = na(b.hod)
isHigh = first or px > b.hod
if isHigh
b.hod := px
if not first and isHigh and not b.prevHigh
b.breaks += 1
if on and b.breaks == iHodN and inWin and not b.fired
b.fired := true
f := true
b.prevHigh := isHigh
f
sigAdd(array<string> ks, array<string> tips, string k, string rule, float px) =>
if ks.indexof(k) < 0
ks.push(k)
tips.push(k + " " + rule + " · premium " + str.tostring(px, "0.00"))
kTxt(float k) =>
str.tostring(k)
sgC = array.new<string>()
sgP = array.new<string>()
tpC = array.new<string>()
tpP = array.new<string>()
if sigOn and rth and barstate.isconfirmed
if fires(bAC, pAC, iR1)
sigAdd(sgC, tpC, kTxt(k0) + "C", "ATM weekly", pAC)
if fires(bAP, pAP, iR1)
sigAdd(sgP, tpP, kTxt(k0) + "P", "ATM weekly", pAP)
if fires(bOC, pOC, iR2)
sigAdd(sgC, tpC, kTxt(kOC) + "C", "3 OTM weekly", pOC)
if fires(bOP, pOP, iR2)
sigAdd(sgP, tpP, kTxt(kOP) + "P", "3 OTM weekly", pOP)
if fires(bSC, pSC, iR3)
sigAdd(sgC, tpC, kTxt(kSC) + "C", "straddle weekly", pSC)
if fires(bSP, pSP, iR3)
sigAdd(sgP, tpP, kTxt(kSP) + "P", "straddle weekly", pSP)
if fires(bMC, pMC, iR4)
sigAdd(sgC, tpC, kTxt(kMC) + "C", "straddle monthly", pMC)
if fires(bMP, pMP, iR4)
sigAdd(sgP, tpP, kTxt(kMP) + "P", "straddle monthly", pMP)
// arrows: a coloured bubble with ▲ / ▼ 0.35 ATR off the candle, strikes in small print past it, details on hover
sigGap = math.max(nz(ta.atr(14), high - low) * 0.35, syminfo.mintick * 2)
if iSig and sgC.size() > 0
label.new(bar_index, low - sigGap, " \n" + sgC.join("\n"), style = label.style_label_up, color = color(na), textcolor = C_UP, size = size.small)
label.new(bar_index, low - sigGap, "▲", style = label.style_label_up, color = C_UP, textcolor = chart.fg_color, size = size.small,
tooltip = "CALL signal · " + hm(time_close) + " ET · premium high-of-day break #" + str.tostring(iHodN) + "\n" + tpC.join("\n"))
if iSig and sgP.size() > 0
label.new(bar_index, high + sigGap, sgP.join("\n") + "\n ", style = label.style_label_down, color = color(na), textcolor = C_DOWN, size = size.small)
label.new(bar_index, high + sigGap, "▼", style = label.style_label_down, color = C_DOWN, textcolor = chart.fg_color, size = size.small,
tooltip = "PUT signal · " + hm(time_close) + " ET · premium high-of-day break #" + str.tostring(iHodN) + "\n" + tpP.join("\n"))
// ---------------------------------------------------------------- option flow: big-order circles and strike bars
// Latest session, 7 strikes around the opening price (calls and puts, nearest expiry: SPY / QQQ same day, others this
// week's Friday). Premium = volume × last price × 100 per strike-minute. Trade side is not available to scripts.
flowOn = isMin and (iBub or iZones)
lastDay = dayofmonth(time, NY) == dayofmonth(last_bar_time, NY) and time > last_bar_time - DAY
var float fk0 = na
var int fExp = 0
var float fSum = 0.0
var int fCnt = 0
var zk = array.new<float>()
var zc = array.new<float>()
var zp = array.new<float>()
if newDay
fk0 := na
fSum := 0.0
fCnt := 0
zk.clear()
zc.clear()
zp.clear()
fst = strikeStep(close)
if flowOn and rth and lastDay
if na(fk0)
fk0 := math.round(close / fst) * fst
fExp := syminfo.ticker == "SPY" or syminfo.ticker == "QQQ" ? time : time + (6 - dayofweek(time, NY)) * DAY
zAdd(float k, float c, float p) =>
ix = zk.indexof(k)
if ix < 0
zk.push(k)
zc.push(c)
zp.push(p)
else
zc.set(ix, zc.get(ix) + c)
zp.set(ix, zp.get(ix) + p)
var bubIds = array.new<label>()
var bubPrem = array.new<float>()
float bC = 0.0
float bP = 0.0
string tipC = ""
string tipP = ""
fThr = math.max(iBubMin, fCnt > 0 ? iBubX * fSum / fCnt : 0)
if flowOn and rth and lastDay and not na(fk0)
for i = -3 to 3
k = fk0 + i * fst
[cc, cv] = request.security(optT(fExp, "C", k), "1", [close, volume], gaps = barmerge.gaps_on, ignore_invalid_symbol = true)
[pc, pv] = request.security(optT(fExp, "P", k), "1", [close, volume], gaps = barmerge.gaps_on, ignore_invalid_symbol = true)
cPrem = not na(cc) and not na(cv) ? cc * cv * 100 : 0.0
pPrem = not na(pc) and not na(pv) ? pc * pv * 100 : 0.0
cITM = iHideITM and k < close * 0.97
pITM = iHideITM and k > close * 1.03
if barstate.isconfirmed
if cPrem > 0
fSum += cPrem
fCnt += 1
if pPrem > 0
fSum += pPrem
fCnt += 1
zAdd(k, cITM ? 0.0 : cPrem, pITM ? 0.0 : pPrem)
if not cITM and cPrem >= fThr
bC += cPrem
tipC += "\n" + str.tostring(k) + "C " + money(cPrem) + " (" + str.tostring(cv, "#") + " × " + str.tostring(cc, "0.00") + ")"
if not pITM and pPrem >= fThr
bP += pPrem
tipP += "\n" + str.tostring(k) + "P " + money(pPrem) + " (" + str.tostring(pv, "#") + " × " + str.tostring(pc, "0.00") + ")"
bubSize(float v) =>
z = 0.5 + math.log10(v / fThr + 1) * 1.3
z < 1.2 ? size.tiny : z < 1.8 ? size.small : size.normal
if iBub and barstate.isconfirmed and bC > 0
bubIds.push(label.new(bar_index, low, "", yloc = yloc.belowbar, style = label.style_circle, color = color.new(C_UP, 40), textcolor = C_UP,
size = bubSize(bC), tooltip = "Big CALL trades · " + hm(time_close) + " ET · " + money(bC) + tipC + "\nVolume × last price; buy / sell side not available"))
bubPrem.push(bC)
if iBub and barstate.isconfirmed and bP > 0
bubIds.push(label.new(bar_index, high, "", yloc = yloc.abovebar, style = label.style_circle, color = color.new(C_DOWN, 40), textcolor = C_DOWN,
size = bubSize(bP), tooltip = "Big PUT trades · " + hm(time_close) + " ET · " + money(bP) + tipP + "\nVolume × last price; buy / sell side not available"))
bubPrem.push(-bP)
// what's on screen: rightmost visible candle, bar count, candle range
var int zvR = na
var int zvN = 0
var float zvHi = na
var float zvLo = na
if time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
zvR := bar_index
zvN += 1
zvHi := na(zvHi) ? high : math.max(zvHi, high)
zvLo := na(zvLo) ? low : math.min(zvLo, low)
var zoneLines = array.new<line>()
var zoneLabels = array.new<label>()
var table note = table.new(position.top_right, 1, 1)
if barstate.islast
// the 5 biggest circles carry their $
if bubPrem.size() > 0
srt = bubPrem.copy()
for i = 0 to srt.size() - 1
srt.set(i, math.abs(srt.get(i)))
srt.sort(order.descending)
big = srt.get(math.min(4, srt.size() - 1))
for i = 0 to bubIds.size() - 1
p = bubPrem.get(i)
bubIds.get(i).set_text(math.abs(p) >= big ? money(math.abs(p)) + (p > 0 ? " calls" : " puts") : "")
// strike bars: hang off the rightmost candle on screen and grow left (longest ~22% of the bars in view), a few px
// thick, call just above the strike and put just below; only strikes inside the candle range on screen
for l in zoneLines
l.delete()
for l in zoneLabels
l.delete()
zoneLines.clear()
zoneLabels.clear()
if iZones and zk.size() > 0 and not na(zvR)
mx = math.max(1.0, zc.max(), zp.max())
L = math.max(4, math.round(zvN * 0.22))
ppp = (zvHi - zvLo) * 1.2 / 600.0
pad = (zvHi - zvLo) * 0.03
tot = array.new<float>()
for i = 0 to zk.size() - 1
k = zk.get(i)
tot.push(k <= zvHi + pad and k >= zvLo - pad ? zc.get(i) + zp.get(i) : 0)
tsrt = tot.copy()
tsrt.sort(order.descending)
topCut = tsrt.get(math.min(4, tsrt.size() - 1))
for i = 0 to zk.size() - 1
k = zk.get(i)
if k > zvHi + pad or k < zvLo - pad
continue
wc = math.round(L * zc.get(i) / mx)
wp = math.round(L * zp.get(i) / mx)
if zc.get(i) > 0
zoneLines.push(line.new(zvR - math.max(1, wc), k + 3 * ppp, zvR, k + 3 * ppp, color = color.new(C_UP, 35), width = 4))
if zp.get(i) > 0
zoneLines.push(line.new(zvR - math.max(1, wp), k - 3 * ppp, zvR, k - 3 * ppp, color = color.new(C_DOWN, 35), width = 4))
if tot.get(i) > 0 and tot.get(i) >= topCut
txt = str.tostring(k) + " " + (zc.get(i) > 0 ? money(zc.get(i)) + " C" : "") + (zc.get(i) > 0 and zp.get(i) > 0 ? " / " : "") + (zp.get(i) > 0 ? money(zp.get(i)) + " P" : "")
zoneLabels.push(label.new(zvR - math.max(wc, wp) - 1, k, txt, style = label.style_label_right, color = color.new(#080b12, 15),
textcolor = color.new(#e7edf5, 5), size = size.tiny, tooltip = "Option premium traded today at " + str.tostring(k) + " (volume × last price)"))
note.clear(0, 0, 0, 0)
if not isMin
note.cell(0, 0, "OptionVision runs on the 1-minute chart", text_color = C_TEXT, text_size = size.small)