Nifty Theory of Averages final

realisewithrajaram · study · 185 行 · 点赞 20 · TradingView 原页

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

Pine Script

//@version=6
indicator("Nifty Theory of Averages - Levels", shorttitle="NTA Levels", overlay=true, max_labels_count=100)
 
// ───────── Inputs ─────────
tz          = "Asia/Kolkata"
cutoffMin   = input.int(915, "Yday Close cutoff (minutes from midnight IST, 915 = 3:15 PM)", minval=0, maxval=1440, group="Data")
ovYc        = input.float(0, "Manual Yday Close 3:15 (0 = auto)", group="Manual override (today only)")
ovYcas      = input.float(0, "Manual Yday CAS Close (0 = auto)", group="Manual override (today only)")
ovYv        = input.float(0, "Manual Yday VWAP (0 = auto)", group="Manual override (today only)")
ovOpen      = input.float(0, "Manual Today Open (0 = auto)", group="Manual override (today only)")
onlyToday   = input.bool(false, "Show only today's levels", group="Display")
showRefs    = input.bool(true, "Show Yday Close / Yday CAS / Yday VWAP / Open / Today VWAP", group="Display")
showZone    = input.bool(true, "Shade the zone (lowest to highest average)", group="Display")
showLabels  = input.bool(true, "Show labels on the right", group="Display")
showTable   = input.bool(true, "Show bias + sorted averages table", group="Display")
shortCodes  = input.bool(true, "Short codes on labels (C, GF, OK, VK, VC, VV, YV, YC, YK, O, V)", group="Display")
 
// ───────── Daily anchors ─────────
newDay = ta.change(time("D")) != 0
vw     = ta.vwap(hlc3)                       // today's live VWAP (resets each session)
 
var float c315Run = na                        // running 3:15 close for the current day
var float vwapRun = na                        // running VWAP for the current day
var float yC315   = na                        // yesterday's 3:15 close
var float yVwap   = na                        // yesterday's final VWAP
var float tOpen   = na                        // today's open
 
if newDay
    yC315   := c315Run
    yVwap   := vwapRun
    tOpen   := open
    c315Run := na
 
closeMin = hour(time_close, tz) * 60 + minute(time_close, tz)
if closeMin <= cutoffMin
    c315Run := close
vwapRun := vw
 
// Yesterday's official (CAS) close from the daily chart, no repainting
yCasAuto = request.security(syminfo.tickerid, "D", close[1], lookahead = barmerge.lookahead_on)
 
// Manual overrides apply only to the current session
isToday = year(time, tz) == year(timenow, tz) and month(time, tz) == month(timenow, tz) and dayofmonth(time, tz) == dayofmonth(timenow, tz)
yCas    = isToday and ovYcas > 0 ? ovYcas : yCasAuto
yC315u  = isToday and ovYc   > 0 ? ovYc   : yC315
yVwapu  = isToday and ovYv   > 0 ? ovYv   : yVwap
tOpenu  = isToday and ovOpen > 0 ? ovOpen : tOpen
 
// ───────── The 6 averages ─────────
core  = (yVwapu + tOpenu) / 2
gap50 = (tOpenu + yC315u) / 2
oCas  = (tOpenu + yCas) / 2
vCas  = (vw + yCas) / 2
vYc   = (vw + yC315u) / 2
vYv   = (vw + yVwapu) / 2
 
zoneHi = math.max(core, gap50, oCas, vCas, vYc, vYv)
zoneLo = math.min(core, gap50, oCas, vCas, vYc, vYv)
 
// ───────── Bias ─────────
bias     = tOpenu > yC315u and tOpenu > yVwapu ? "Long" : tOpenu < yC315u and tOpenu < yVwapu ? "Short" : "No trade"
sideLong = tOpenu >= yVwapu
 
// ───────── Visibility ─────────
okTf    = timeframe.isintraday
show    = okTf and (not onlyToday or isToday)
f(x)    => show ? x : na
fr(x)   => show and showRefs ? x : na
 
// ───────── Plots ─────────
plot(f(core),  "Core",            color.new(color.orange, 0),  3, plot.style_linebr)
plot(f(gap50), "50% Gap Fill",    color.new(color.gray, 0),    1, plot.style_linebr)
plot(f(oCas),  "Open+YdayCAS",    color.new(color.purple, 0),  1, plot.style_linebr)
plot(f(vCas),  "VWAP+YdayCAS",    color.new(color.teal, 0),    1, plot.style_linebr)
plot(f(vYc),   "VWAP+YdayClose",  color.new(color.olive, 0),   1, plot.style_linebr)
plot(f(vYv),   "VWAP+YdayVWAP",   color.new(color.maroon, 0),  1, plot.style_linebr)
 
pZh = plot(show and showZone ? zoneHi : na, "Zone High", color.new(color.orange, 100), 1, plot.style_linebr)
pZl = plot(show and showZone ? zoneLo : na, "Zone Low",  color.new(color.orange, 100), 1, plot.style_linebr)
fill(pZh, pZl, color.new(color.orange, 88), "Zone")
 
plot(fr(yVwapu), "Yday VWAP (final stop)", color.new(color.red, 0),   2, plot.style_linebr)
plot(fr(yC315u), "Yday Close 3:15",        color.new(color.blue, 0),  1, plot.style_linebr)
plot(fr(yCas),  "Yday CAS Close",         color.new(color.aqua, 0),  1, plot.style_linebr)
plot(fr(tOpenu), "Today Open",             color.new(color.silver, 0), 1, plot.style_linebr)
plot(fr(vw),    "Today VWAP (live)",      color.new(color.fuchsia, 0), 1, plot.style_circles)
 
// ───────── Right-side labels (close levels are merged into one label, so nothing overlaps) ─────────
mergePts = input.float(0, "Merge labels closer than (points, 0 = auto from zoom)", minval = 0, group = "Display")
lblH     = input.float(2.2, "Auto: label height as % of visible price range", minval = 0.5, maxval = 10, step = 0.1, group = "Display")
// visible price range -> how many points one label line occupies at the current zoom
inView = time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
var float vHi = na
var float vLo = na
if inView
    vHi := na(vHi) ? high : math.max(vHi, high)
    vLo := na(vLo) ? low  : math.min(vLo, low)
autoGap = na(vHi) ? 5.0 : (vHi - vLo) * lblH / 100
gapPts  = mergePts > 0 ? mergePts : math.max(autoGap, 0.5)
 
var array<label> labs = array.new<label>()
 
if barstate.islast and showLabels and okTf
    for l in labs
        label.delete(l)
    array.clear(labs)
    ys = array.from(core, gap50, oCas, vCas, vYc, vYv)
    tx = shortCodes ? array.from("C", "GF", "OK", "VK", "VC", "VV") : array.from("Core", "50% Gap Fill", "Open+YdayCAS", "VWAP+YdayCAS", "VWAP+YdayClose", "VWAP+YdayVWAP")
    cs = array.from(color.orange, color.gray, color.purple, color.teal, color.olive, color.maroon)
    if showRefs
        array.push(ys, yVwapu), array.push(tx, shortCodes ? "YV" : "Yday VWAP"),  array.push(cs, color.red)
        array.push(ys, yC315u), array.push(tx, shortCodes ? "YC" : "Yday Close 3:15"), array.push(cs, color.blue)
        array.push(ys, yCas),   array.push(tx, shortCodes ? "YK" : "Yday CAS Close"),  array.push(cs, color.aqua)
        array.push(ys, tOpenu), array.push(tx, shortCodes ? "O" : "Today Open"),  array.push(cs, color.silver)
        array.push(ys, vw),     array.push(tx, shortCodes ? "V" : "Today VWAP"),  array.push(cs, color.fuchsia)
    idx = array.sort_indices(ys, order.descending)
    GY = array.new<float>()
    GT = array.new<string>()
    GN = array.new<int>()
    GC = array.new<color>()
    string gTxt = ""
    float  gTop = na
    float  gSum = 0.0
    int    gN   = 0
    color  gCol = color.gray
    n = array.size(idx)
    for i = 0 to n - 1
        k = array.get(idx, i)
        y = array.get(ys, k)
        if na(y)
            continue
        line_ = array.get(tx, k) + " " + str.tostring(y, "#,##0.#")
        if gN > 0 and gTop - y < gapPts
            gTxt := gTxt + "\n" + line_
            gSum += y
            gN += 1
        else
            if gN > 0
                array.push(GY, gSum / gN), array.push(GT, gTxt), array.push(GN, gN), array.push(GC, gCol)
            gTxt := line_
            gTop := y
            gSum := y
            gN := 1
            gCol := array.get(cs, k)
    if gN > 0
        array.push(GY, gSum / gN), array.push(GT, gTxt), array.push(GN, gN), array.push(GC, gCol)
    // place groups top to bottom, pushing a label down if it would touch the one above
    float prevY = na
    int   prevN = 0
    if array.size(GY) > 0
        for j = 0 to array.size(GY) - 1
            gy = array.get(GY, j)
            gn = array.get(GN, j)
            py = na(prevY) ? gy : math.min(gy, prevY - gapPts * (prevN + gn) / 2.0)
            gc = array.get(GC, j)
            array.push(labs, label.new(bar_index + 2, py, array.get(GT, j), style = label.style_label_left, color = color.new(gc, 80), textcolor = gn > 1 ? chart.fg_color : gc, size = size.small))
            prevY := py
            prevN := gn
 
// ───────── Table: bias + averages sorted in the order price meets them ─────────
var table tb = table.new(position.top_right, 3, 15, bgcolor = color.new(chart.bg_color, 5), border_width = 1, border_color = color.new(color.gray, 70))
if barstate.islast and showTable and okTf
    vals  = array.from(core, gap50, oCas, vCas, vYc, vYv)
    names = array.from("Core", "50% Gap Fill", "Open+YdayCAS", "VWAP+YdayCAS", "VWAP+YdayClose", "VWAP+YdayVWAP")
    codes = array.from("C", "GF", "OK", "VK", "VC", "VV")
    idx   = array.sort_indices(vals, sideLong ? order.descending : order.ascending)
    biasCol = bias == "Long" ? color.green : bias == "Short" ? color.red : color.gray
    table.cell(tb, 1, 0, "Bias", text_color = chart.fg_color, text_size = size.small)
    table.cell(tb, 2, 0, bias, text_color = biasCol, text_size = size.small)
    table.cell(tb, 1, 1, "Levels side", text_color = chart.fg_color, text_size = size.small)
    table.cell(tb, 2, 1, sideLong ? "Long (high to low)" : "Short (low to high)", text_color = chart.fg_color, text_size = size.small)
    for i = 0 to 5
        k = array.get(idx, i)
        table.cell(tb, 0, i + 2, array.get(codes, k), text_color = chart.fg_color, text_size = size.small)
        table.cell(tb, 1, i + 2, array.get(names, k), text_color = chart.fg_color, text_size = size.small)
        table.cell(tb, 2, i + 2, str.tostring(array.get(vals, k), "#,##0.0"), text_color = chart.fg_color, text_size = size.small)
    refC = array.from("YV", "YC", "YK", "O", "V")
    refN = array.from("Yday VWAP (final stop)", "Yday Close 3:15", "Yday CAS Close", "Today Open", "Today VWAP")
    refV = array.from(yVwapu, yC315u, yCas, tOpenu, vw)
    for r = 0 to 4
        table.cell(tb, 0, r + 8, array.get(refC, r), text_color = chart.fg_color, text_size = size.small)
        table.cell(tb, 1, r + 8, array.get(refN, r), text_color = chart.fg_color, text_size = size.small)
        table.cell(tb, 2, r + 8, str.tostring(array.get(refV, r), "#,##0.0"), text_color = r == 0 ? color.red : chart.fg_color, text_size = size.small)
    table.cell(tb, 1, 13, "Zone width (pts)", text_color = chart.fg_color, text_size = size.small)
    table.cell(tb, 2, 13, str.tostring(zoneHi - zoneLo, "#,##0.0"), text_color = chart.fg_color, text_size = size.small)

← 返回列表