Option Volume Profile

Ama7Choudhary · study · 158 行 · 点赞 57 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Option Volume Profile", "Option Profile", overlay = true, max_lines_count = 50, max_labels_count = 50, max_boxes_count = 200)
 
// ───────── Option data ─────────
optRoot = input.string("NSE:NIFTY", "Option symbol", group = "Option data", tooltip = "Exchange and root of the option contracts, e.g. NSE:NIFTY (not the futures contract).")
expiry  = input.string("", "Expiry (YYMMDD)", group = "Option data", tooltip = "Expiry in TradingView's format, e.g. 261117 for 17 Nov 2026.")
step    = input.float(50.0, "Strike step", minval = 0.000001, group = "Option data", tooltip = "Distance between strikes, e.g. 50 for NIFTY.")
 
// ───────── Settings ─────────
showProfile = input.bool(true, "Show profile graph")
showChange  = input.bool(true, "Volume change vs previous day")
strikesN    = input.int(20, "Strikes Count", minval = 2, maxval = 20, tooltip = "Total strikes around price: half below, half above.")
showSupport = input.bool(true, "Support strike")
showResist  = input.bool(true, "Resistance strike")
showMaxPain = input.bool(true, "Max Pain")
showExpMove = input.bool(true, "Expected Move")
callCol     = input.color(#f23645, "Call (CE) color")
putCol      = input.color(#089981, "Put (PE) color")
supLine     = input.color(#089981, "Support line color")
supTxt      = input.color(color.white, "Support text color")
resLine     = input.color(#f23645, "Resistance line color")
resTxt      = input.color(color.white, "Resistance text color")
mpLine      = input.color(#f9a825, "Max Pain line color")
mpTxt       = input.color(color.white, "Max Pain text color")
emUpLine    = input.color(#26a69a, "Expected Move Up line color")
emUpTxt     = input.color(color.white, "Expected Move Up text color")
emDnLine    = input.color(#ef5350, "Expected Move Down line color")
emDnTxt     = input.color(color.white, "Expected Move Down text color")
 
// ───────── Symbol building ─────────
string rootSym = str.trim(optRoot)
 
fmtStrike(float k) =>
    float r = math.round(k)
    math.abs(k - r) < 0.0000001 ? str.tostring(int(r)) : str.tostring(k)
 
optSym(float k, string side) =>
    rootSym + str.trim(expiry) + side + fmtStrike(k)
 
drawLevel(array<line> L, array<label> B, float y, string txt, color lc, color tc, bool dashed) =>
    array.push(L, line.new(bar_index - 1, y, bar_index, y, extend = extend.both, color = lc, width = 1, style = dashed ? line.style_dashed : line.style_dotted))
    array.push(B, label.new(chart.left_visible_bar_time, y, txt, xloc = xloc.bar_time, style = label.style_label_left, color = lc, textcolor = tc, size = size.small))
    true
 
// ───────── Main ─────────
var float center = na
var array<line>  dl  = array.new<line>()
var array<label> dlb = array.new<label>()
var array<box>   dbx = array.new<box>()
var table msg = table.new(position.bottom_right, 1, 1, bgcolor = color.new(color.black, 20), frame_color = color.new(color.gray, 50), frame_width = 1)
 
// Centre strike is fixed once per load (change any setting to re-centre)
if barstate.islast and na(center)
    center := math.round(close / step) * step
 
if barstate.islast and not na(center)
    while array.size(dl) > 0
        line.delete(array.pop(dl))
    while array.size(dlb) > 0
        label.delete(array.pop(dlb))
    while array.size(dbx) > 0
        box.delete(array.pop(dbx))
    table.cell(msg, 0, 0, "")
 
    int   half = strikesN / 2
    float lowK = center - half * step
    array<float> K  = array.new<float>()
    array<float> C  = array.new<float>()
    array<float> P  = array.new<float>()
    array<float> CC = array.new<float>()
    array<float> PC = array.new<float>()
    int   found = 0
    float atmC  = na
    float atmP  = na
 
    // One request per contract: price (for the expected move) and today's / yesterday's volume
    for i = 0 to strikesN - 1
        float k = lowK + i * step
        [cPx, cVol, cVolPrev] = request.security(optSym(k, "C"), "D", [close, volume, volume[1]], ignore_invalid_symbol = true)
        [pPx, pVol, pVolPrev] = request.security(optSym(k, "P"), "D", [close, volume, volume[1]], ignore_invalid_symbol = true)
        array.push(K, k)
        array.push(C, nz(cVol))
        array.push(P, nz(pVol))
        array.push(CC, nz(cVol) - nz(cVolPrev, nz(cVol)))
        array.push(PC, nz(pVol) - nz(pVolPrev, nz(pVol)))
        if not na(cPx) or not na(pPx)
            found += 1
        if i == half
            atmC := cPx
            atmP := pPx
 
    if found == 0
        table.cell(msg, 0, 0, "Option Profile: no option data found.\nTried: " + optSym(center, "C") + "\nCheck Option symbol, Expiry (YYMMDD) and Strike step.", text_color = color.orange, text_size = size.small)
    else
        // Scale, support (most put volume) and resistance (most call volume)
        float maxV   = 1.0
        float maxChg = 1.0
        int supI = 0
        int resI = 0
        for i = 0 to strikesN - 1
            maxV   := math.max(maxV, math.max(array.get(C, i), array.get(P, i)))
            maxChg := math.max(maxChg, math.max(math.abs(array.get(CC, i)), math.abs(array.get(PC, i))))
            if array.get(P, i) > array.get(P, supI)
                supI := i
            if array.get(C, i) > array.get(C, resI)
                resI := i
 
        // "Max pain" using volume as the weight (approximation, since OI is not available to scripts)
        float bestPain = na
        float maxPainK = na
        for a = 0 to strikesN - 1
            float ka = array.get(K, a)
            float pain = 0.0
            for b = 0 to strikesN - 1
                float kb = array.get(K, b)
                if kb < ka
                    pain += array.get(C, b) * (ka - kb)
                if kb > ka
                    pain += array.get(P, b) * (kb - ka)
            if na(bestPain) or pain < bestPain
                bestPain := pain
                maxPainK := ka
 
        // Profile bars: volume grows to the left of the anchor, change vs yesterday to the right
        int   anchor = bar_index + 70
        float h      = step * 0.4
        if showProfile
            for i = 0 to strikesN - 1
                float k  = array.get(K, i)
                int   wc = int(math.round(50 * array.get(C, i) / maxV))
                int   wp = int(math.round(50 * array.get(P, i) / maxV))
                if wc > 0
                    array.push(dbx, box.new(anchor - wc, k + h, anchor, k + h * 0.1, border_color = color(na), bgcolor = color.new(callCol, 10)))
                if wp > 0
                    array.push(dbx, box.new(anchor - wp, k - h * 0.1, anchor, k - h, border_color = color(na), bgcolor = color.new(putCol, 10)))
                if showChange
                    float cc  = array.get(CC, i)
                    float pc  = array.get(PC, i)
                    int   wcc = int(math.round(20 * math.abs(cc) / maxChg))
                    int   wpc = int(math.round(20 * math.abs(pc) / maxChg))
                    if wcc > 0
                        array.push(dbx, box.new(anchor, k + h, anchor + wcc, k + h * 0.1, border_color = color(na), bgcolor = color.new(callCol, cc >= 0 ? 0 : 65)))
                    if wpc > 0
                        array.push(dbx, box.new(anchor, k - h * 0.1, anchor + wpc, k - h, border_color = color(na), bgcolor = color.new(putCol, pc >= 0 ? 0 : 65)))
 
        // Key levels
        if showSupport
            float s = array.get(K, supI)
            drawLevel(dl, dlb, s, "Support strike " + fmtStrike(s), supLine, supTxt, false)
        if showResist
            float r = array.get(K, resI)
            drawLevel(dl, dlb, r, "Resistance strike " + fmtStrike(r), resLine, resTxt, false)
        if showMaxPain and not na(maxPainK)
            drawLevel(dl, dlb, maxPainK, "Max Pain " + fmtStrike(maxPainK), mpLine, mpTxt, false)
        if showExpMove and not na(atmC) and not na(atmP)
            float em = atmC + atmP
            drawLevel(dl, dlb, close + em, "Expected move " + str.tostring(close + em, format.mintick), emUpLine, emUpTxt, true)
            drawLevel(dl, dlb, close - em, "Expected move " + str.tostring(close - em, format.mintick), emDnLine, emDnTxt, true)

← 返回列表