Expiry Cycle Statistics

subrajitmishra · study · 350 行 · 点赞 5 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("Expiry Cycle Statistics", shorttitle = "Expiry Stats", overlay = true, max_lines_count = 20, max_labels_count = 20)
 
// ─────────────────────────────────────────────────────────────────────────────
// WHAT IT DOES
// Groups every past trading day by its trading days to the next weekly expiry (DTE 0–4)
// and shows how the index has actually behaved at each stage of the expiry cycle:
//   • average day range and average close-to-close change
//   • share of up-closes and of "big" days (|change| above a threshold)
//   • percentile of the move from the previous close to the expiry close
//   • percentile of the largest swing (high or low) reached before expiry
// Expiry days are found from the data itself (the last trading day on or before the
// scheduled weekday), so past holiday-shifted expiries are handled automatically.
// Works on NIFTY and SENSEX (index or continuous futures) and switches automatically.
// Built-in weekly expiry history:
//   NIFTY : Thursday (weekly options from 11 Feb 2019) → Tuesday from 1 Sep 2025
//   SENSEX: Friday (weekly relaunch 15 May 2023) → Tuesday from 1 Jan 2025 → Thursday from 1 Sep 2025
// Historical statistics only — not a forecast and not a trade recommendation.
// ─────────────────────────────────────────────────────────────────────────────
 
tz  = "Asia/Kolkata"
DAY = 86400000
 
// ── Inputs ──
grpE   = "Expiry schedule"
holStr = input.string("", "Upcoming exchange holidays (YYYY-MM-DD, comma separated)", group = grpE, tooltip = "Only needed for today's DTE. Past holidays are detected from the data.")
 
grpS   = "Statistics"
yrs    = input.int(3, "Look-back (years)", minval = 1, maxval = 15, group = grpS, tooltip = "Use the Daily timeframe for the longest history. On intraday charts the sample is limited to the loaded intraday bars.")
pctl   = input.int(80, "Percentile", options = [50, 70, 80, 90, 95], group = grpS)
bigTh  = input.float(1.0, "Big day if |change| ≥ (%)", step = 0.1, minval = 0.1, group = grpS)
 
grpB   = "Chart bands (today)"
showB  = input.bool(true, "Show percentile bands for today", group = grpB)
showSw = input.bool(true, "Also show max-swing bands", group = grpB)
extB   = input.int(20, "Extend bands right (bars)", minval = 0, maxval = 200, group = grpB)
cBand  = input.color(#42A5F5, "Expiry-close band", group = grpB, inline = "b1")
wBand  = input.int(2, "width", minval = 1, maxval = 4, group = grpB, inline = "b1")
cSw    = input.color(#FF9800, "Max-swing band", group = grpB, inline = "b2")
wSw    = input.int(1, "width", minval = 1, maxval = 4, group = grpB, inline = "b2")
cRef   = input.color(color.new(#9E9E9E, 30), "Reference (prev close)", group = grpB)
lblSz  = input.string("Small", "Band label size", options = ["Tiny", "Small", "Normal", "Large"], group = grpB)
cLblTx = input.color(#FFFFFF, "Band label text", group = grpB)
 
grpT    = "Table"
showTbl = input.bool(true, "Show table", group = grpT)
tblPos  = input.string("Top right", "Position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpT)
tblSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large"], group = grpT)
cTBg    = input.color(color.new(#131722, 5), "Background", group = grpT, inline = "t1")
cTHdBg  = input.color(color.new(#1E222D, 0), "Header bg", group = grpT, inline = "t1")
cTHdTx  = input.color(#FFFFFF, "Header text", group = grpT, inline = "t2")
cTKey   = input.color(#A3A6AF, "Label text", group = grpT, inline = "t2")
cTVal   = input.color(#D1D4DC, "Value text", group = grpT, inline = "t3")
cTNow   = input.color(color.new(#42A5F5, 75), "Today's row bg", group = grpT, inline = "t3")
cTUp    = input.color(#4ADE80, "Up text", group = grpT, inline = "t5")
cTDn    = input.color(#FF6B6B, "Down / big text", group = grpT, inline = "t5")
cTFrame = input.color(color.new(#787B86, 70), "Frame", group = grpT, inline = "t4")
cTGrid  = input.color(color.new(#787B86, 85), "Grid lines", group = grpT, inline = "t4")
 
// ── Helpers ──
f_size(string s) => s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Normal" ? size.normal : size.large
f_dayStart(int t) => timestamp(tz, year(t, tz), month(t, tz), dayofmonth(t, tz), 0, 0)
f_parseHol(string s) =>
    arr = array.new<string>()
    for p in str.split(s, ",")
        t = str.trim(p)
        if str.length(t) == 10
            array.push(arr, t)
    arr
f_isTrading(int t, array<string> hol) =>
    dw = dayofweek(t, tz)
    dw != dayofweek.saturday and dw != dayofweek.sunday and not array.includes(hol, str.format_time(t, "yyyy-MM-dd", tz))
f_prevTrading(int t, array<string> hol) =>
    tt = t
    for i = 0 to 10
        if f_isTrading(tt, hol)
            break
        tt -= DAY
    tt
f_nextTrading(int t, array<string> hol) =>
    tt = t
    for i = 0 to 10
        if f_isTrading(tt, hol)
            break
        tt += DAY
    tt
f_weekly(int sess, int wd, array<string> hol) =>
    int exp = na
    for k = 0 to 3
        t = sess + k * 7 * DAY
        for d = 0 to 6
            if dayofweek(t, tz) == wd
                break
            t += DAY
        e = f_prevTrading(t, hol)
        if e >= sess
            exp := e
            break
    exp
f_tradingDte(int sess, int exp, array<string> hol) =>
    n = 0
    if not na(exp)
        t = sess + DAY
        for i = 0 to 60
            if t > exp
                break
            n += f_isTrading(t, hol) ? 1 : 0
            t += DAY
    n
f_pct(float v) => na(v) ? "—" : str.tostring(v, "0.00") + "%"
 
hol   = f_parseHol(holStr)
 
// ── Index detection & built-in expiry schedule ──
tick     = syminfo.ticker
isNifty  = tick == "NIFTY" or tick == "NIFTY1!" or tick == "NIFTY2!"
isSensex = tick == "SENSEX" or tick == "SENSEX1!" or tick == "SENSEX2!" or tick == "BSX1!"
okSym    = isNifty or isSensex
idxName  = isNifty ? "NIFTY" : isSensex ? "SENSEX" : ""
T_N0  = timestamp(tz, 2019, 2, 11, 0, 0)
T_S0  = timestamp(tz, 2023, 5, 15, 0, 0)
T_S1  = timestamp(tz, 2025, 1, 1, 0, 0)
T_SEP = timestamp(tz, 2025, 9, 1, 0, 0)
// weekly expiry weekday in force on a given day (na = no weekly expiry then)
f_wdAt(int ds) =>
    int w = na
    if isNifty
        w := ds < T_N0 ? na : ds < T_SEP ? dayofweek.thursday : dayofweek.tuesday
    else if isSensex
        w := ds < T_S0 ? na : ds < T_S1 ? dayofweek.friday : ds < T_SEP ? dayofweek.tuesday : dayofweek.thursday
    w
eraStart = isNifty ? T_N0 : T_S0
 
// ── Completed daily bars (previous days only → no repainting) ──
[dT, dH, dL, dC] = request.security(syminfo.tickerid, "D", [time[1], high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
[tH, tL, tC]     = request.security(syminfo.tickerid, "D", [high, low, close], lookahead = barmerge.lookahead_on)
 
var array<int>   aT = array.new<int>()
var array<float> aH = array.new<float>()
var array<float> aL = array.new<float>()
var array<float> aC = array.new<float>()
if not na(dT) and not na(dC) and (array.size(aT) == 0 or dT > array.last(aT))
    array.push(aT, dT)
    array.push(aH, dH)
    array.push(aL, dL)
    array.push(aC, dC)
 
// ── Results per DTE (0–4) ──
var array<int>   rCnt  = array.new<int>(5, 0)
var array<float> rRng  = array.new<float>(5, na)
var array<float> rCC   = array.new<float>(5, na)
var array<float> rUp   = array.new<float>(5, na)
var array<float> rBig  = array.new<float>(5, na)
var array<float> rToE  = array.new<float>(5, na)
var array<float> rSw   = array.new<float>(5, na)
var int   nExp    = 0
var int   sampleT = na
var int   lastN   = 0
 
if okSym and barstate.islast and array.size(aT) > 30 and array.size(aT) != lastN
    lastN := array.size(aT)
    n = array.size(aT)
    tEnd = array.get(aT, n - 1)
    s0 = 1
    for i = 1 to n - 1
        if array.get(aT, i) >= math.max(tEnd - yrs * 365 * DAY, eraStart)
            s0 := i
            break
    sampleT := array.get(aT, s0)
 
    // 1) find actual expiry days: last trading day of each week on or before the scheduled weekday
    exps = array.new<int>()
    int curWk = na
    int cand = na
    for i = s0 to n - 1
        t  = array.get(aT, i)
        ds = f_dayStart(t)
        dw = dayofweek(t, tz)
        wk = ds - ((dw + 5) % 7) * DAY
        wd = f_wdAt(ds)
        if na(curWk) or wk != curWk
            if not na(cand)
                array.push(exps, cand)
            curWk := wk
            cand := na
        if not na(wd) and dw <= wd
            cand := i
    if not na(cand)
        dsl  = f_dayStart(array.get(aT, n - 1))
        wdl  = f_wdAt(dsl)
        sched = curWk + (nz(wdl, 2) - 2) * DAY
        if not na(wdl) and f_prevTrading(sched, hol) <= dsl
            array.push(exps, cand)
    nExp := array.size(exps)
 
    // 2) measure every day by its trading days to the next expiry
    dteA = array.new<int>()
    rngA = array.new<float>()
    ccA  = array.new<float>()
    toA  = array.new<float>()
    swA  = array.new<float>()
    if nExp > 0
        p = 0
        for i = s0 to n - 1
            while p < nExp and array.get(exps, p) < i
                p += 1
            if p >= nExp
                break
            e = array.get(exps, p)
            k = e - i
            if k <= 4
                pc = array.get(aC, i - 1)
                array.push(dteA, k)
                array.push(rngA, (array.get(aH, i) - array.get(aL, i)) / pc * 100)
                array.push(ccA, (array.get(aC, i) / pc - 1) * 100)
                array.push(toA, math.abs(array.get(aC, e) / pc - 1) * 100)
                mx = 0.0
                for j = i to e
                    mx := math.max(mx, math.max(array.get(aH, j) / pc - 1, 1 - array.get(aL, j) / pc) * 100)
                array.push(swA, mx)
 
    // 3) aggregate per DTE
    m = array.size(dteA)
    for k = 0 to 4
        cnt = 0
        up = 0
        big = 0
        sR = 0.0
        sC = 0.0
        tArr = array.new<float>()
        wArr = array.new<float>()
        if m > 0
            for q = 0 to m - 1
                if array.get(dteA, q) == k
                    c = array.get(ccA, q)
                    cnt += 1
                    up += c > 0 ? 1 : 0
                    big += math.abs(c) >= bigTh ? 1 : 0
                    sR += array.get(rngA, q)
                    sC += math.abs(c)
                    array.push(tArr, array.get(toA, q))
                    array.push(wArr, array.get(swA, q))
        array.set(rCnt, k, cnt)
        array.set(rRng, k, cnt > 0 ? sR / cnt : na)
        array.set(rCC,  k, cnt > 0 ? sC / cnt : na)
        array.set(rUp,  k, cnt > 0 ? 100.0 * up / cnt : na)
        array.set(rBig, k, cnt > 0 ? 100.0 * big / cnt : na)
        array.set(rToE, k, cnt > 0 ? array.percentile_nearest_rank(tArr, pctl) : na)
        array.set(rSw,  k, cnt > 0 ? array.percentile_nearest_rank(wArr, pctl) : na)
 
// ── Today ──
// After the last session's date has passed (from midnight IST), switch to the next session
lastDay   = f_dayStart(time)
nowDay    = f_dayStart(timenow)
preOpen   = nowDay > lastDay
todaySess = preOpen ? f_nextTrading(nowDay, hol) : lastDay
wdToday   = nz(f_wdAt(todaySess), dayofweek.tuesday)
expToday  = okSym ? f_weekly(todaySess, wdToday, hol) : na
dteToday  = f_tradingDte(todaySess, expToday, hol)
kT        = math.min(dteToday, 4)
prevC     = preOpen ? tC : array.size(aC) > 0 ? array.last(aC) : na
 
dChg   = ta.change(time("D"))
newDay = timeframe.isintraday ? dChg != 0 : true
var int dayBar = na
if newDay
    dayBar := bar_index
 
// ── Bands ──
var array<line>  bl = array.new<line>()
var array<label> bt = array.new<label>()
f_band(float y, color c, int w, string st, string txt) =>
    array.push(bl, line.new(preOpen ? bar_index : dayBar, y, bar_index + extB, y, color = c, width = w, style = st))
    if txt != ""
        array.push(bt, label.new(bar_index + extB, y, txt, style = label.style_label_left, size = f_size(lblSz), color = color.new(c, 15), textcolor = cLblTx))
 
if barstate.islast
    for l in bl
        line.delete(l)
    for l in bt
        label.delete(l)
    array.clear(bl)
    array.clear(bt)
    pT = array.get(rToE, kT)
    pS = array.get(rSw, kT)
    if okSym and showB and not na(prevC) and not na(pT)
        tag = "P" + str.tostring(pctl) + " DTE " + str.tostring(kT)
        f_band(prevC, cRef, 1, line.style_dotted, "")
        f_band(prevC * (1 + pT / 100), cBand, wBand, line.style_solid, tag + " expiry close +" + str.tostring(pT, "0.00") + "%")
        f_band(prevC * (1 - pT / 100), cBand, wBand, line.style_solid, tag + " expiry close −" + str.tostring(pT, "0.00") + "%")
        if showSw and not na(pS)
            f_band(prevC * (1 + pS / 100), cSw, wSw, line.style_dashed, tag + " max swing +" + str.tostring(pS, "0.00") + "%")
            f_band(prevC * (1 - pS / 100), cSw, wSw, line.style_dashed, tag + " max swing −" + str.tostring(pS, "0.00") + "%")
 
// ── Table ──
pos = tblPos == "Top right" ? position.top_right : tblPos == "Top left" ? position.top_left : tblPos == "Bottom right" ? position.bottom_right : position.bottom_left
tsz = f_size(tblSize)
var table tb = table.new(pos, 8, 14, border_width = 1, frame_width = 1, frame_color = cTFrame, border_color = cTGrid)
 
f_cell(int c, int r, string s, color tc, color bg) =>
    table.cell(tb, c, r, s, text_color = tc, bgcolor = bg, text_size = tsz)
 
if barstate.islast and showTbl
    table.clear(tb, 0, 0, 7, 13)
    table.cell(tb, 0, 0, "EXPIRY CYCLE STATISTICS · " + (okSym ? idxName : syminfo.ticker), text_color = cTHdTx, bgcolor = cTHdBg, text_size = tsz, text_halign = text.align_left)
    table.merge_cells(tb, 0, 0, 7, 0)
if barstate.islast and showTbl and not okSym
    table.cell(tb, 0, 1, "Open a NIFTY or SENSEX chart to use this indicator.", text_color = cTVal, bgcolor = cTBg, text_size = tsz, text_halign = text.align_left)
    table.merge_cells(tb, 0, 1, 7, 1)
if barstate.islast and showTbl and okSym
    nextTxt = na(expToday) ? "—" : str.format_time(expToday, "dd MMM yyyy (EEE)", tz)
    table.cell(tb, 0, 1, "Next expiry: " + nextTxt + (preOpen ? "  ·  Next session is DTE " : "  ·  Today is DTE ") + str.tostring(dteToday) + (dteToday == 0 ? " (expiry day)" : ""), text_color = cTVal, bgcolor = cTBg, text_size = tsz, text_halign = text.align_left)
    table.merge_cells(tb, 0, 1, 7, 1)
    table.cell(tb, 0, 2, "Sample: since " + (na(sampleT) ? "—" : str.format_time(sampleT, "dd MMM yyyy", tz)) + "  ·  " + str.tostring(nExp) + " expiries", text_color = cTKey, bgcolor = cTBg, text_size = tsz, text_halign = text.align_left)
    table.merge_cells(tb, 0, 2, 7, 2)
    hdr = array.from("DTE", "Days", "Avg range", "Avg |chg|", "Up close", "|chg| ≥ " + str.tostring(bigTh, "0.0") + "%", "P" + str.tostring(pctl) + " to expiry", "P" + str.tostring(pctl) + " max swing")
    for c = 0 to 7
        f_cell(c, 3, array.get(hdr, c), cTKey, cTHdBg)
    for k = 4 to 0
        r = 4 + (4 - k)
        isNow = k == kT
        bg = isNow ? cTNow : cTBg
        up = array.get(rUp, k)
        f_cell(0, r, str.tostring(k) + (k == 0 ? " (expiry)" : ""), isNow ? cTHdTx : cTVal, bg)
        f_cell(1, r, str.tostring(array.get(rCnt, k)), cTVal, bg)
        f_cell(2, r, f_pct(array.get(rRng, k)), cTVal, bg)
        f_cell(3, r, f_pct(array.get(rCC, k)), cTVal, bg)
        f_cell(4, r, na(up) ? "—" : str.tostring(up, "0") + "%", na(up) ? cTVal : up >= 50 ? cTUp : cTDn, bg)
        f_cell(5, r, na(array.get(rBig, k)) ? "—" : str.tostring(array.get(rBig, k), "0") + "%", cTDn, bg)
        f_cell(6, r, "±" + f_pct(array.get(rToE, k)), cTVal, bg)
        f_cell(7, r, "±" + f_pct(array.get(rSw, k)), cTVal, bg)
    if preOpen and not na(prevC)
        pT0 = array.get(rToE, kT)
        txt0 = "Next session " + str.format_time(todaySess, "dd MMM (EEE)", tz) + " · reference = last close " + str.tostring(prevC, "#,##0.00")
        if not na(pT0)
            txt0 += "  ·  P" + str.tostring(pctl) + " to expiry ≈ ±" + str.tostring(prevC * pT0 / 100, "#,##0") + " pts"
        table.cell(tb, 0, 9, txt0, text_color = cTVal, bgcolor = cTBg, text_size = tsz, text_halign = text.align_left)
        table.merge_cells(tb, 0, 9, 7, 9)
    else if not na(prevC)
        todayRng = (tH - tL) / prevC * 100
        avgR = array.get(rRng, kT)
        pT = array.get(rToE, kT)
        txt = "Today's range so far " + f_pct(todayRng) + " vs DTE " + str.tostring(kT) + " average " + f_pct(avgR)
        if not na(pT)
            txt += "  ·  P" + str.tostring(pctl) + " to expiry ≈ ±" + str.tostring(prevC * pT / 100, "#,##0") + " pts from prev close"
        table.cell(tb, 0, 9, txt, text_color = cTVal, bgcolor = cTBg, text_size = tsz, text_halign = text.align_left)
        table.merge_cells(tb, 0, 9, 7, 9)
    table.cell(tb, 0, 10, "Moves measured from the previous close. Historical statistics, not a forecast.", text_color = cTKey, bgcolor = cTBg, text_size = size.tiny, text_halign = text.align_left)
    table.merge_cells(tb, 0, 10, 7, 10)

← 返回列表