0DTE Expected Move & SD Ladder

reecio2005 · study · 137 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("0DTE Expected Move & SD Ladder", shorttitle = "EM Ladder", overlay = true, max_lines_count = 100, max_labels_count = 100)
 
// ─────────────────────────────── 1 · Inputs ───────────────────────────────
GI = "1 · Inputs"
ivIn      = input.float(0.0, "ATM IV  (!iv_mid NDX)", minval = 0.0, step = 0.001, group = GI, tooltip = "Paste the number GEXBot returns. Decimal (0.194) or percent (19.4) both work. 0 = nothing drawn.")
dte       = input.float(1.0, "DTE", minval = 0.01, step = 1.0, group = GI)
daysYear  = input.int(252, "Trading days per year", minval = 1, group = GI)
useManual = input.bool(false, "Manual anchor price", group = GI, inline = "anc")
manualAnc = input.float(0.0, "", minval = 0.0, group = GI, inline = "anc")
tz        = input.string("America/New_York", "Timezone", group = GI)
openH     = input.int(9, "Anchor time  hour", minval = 0, maxval = 23, group = GI, inline = "t")
openM     = input.int(30, "min", minval = 0, maxval = 59, group = GI, inline = "t")
 
// ─────────────────────────────── 2 · Levels ───────────────────────────────
GL = "2 · Levels"
show025   = input.bool(true,  "0.25 SD", group = GL, inline = "k1")
show050   = input.bool(true,  "0.50 SD", group = GL, inline = "k1")
show075   = input.bool(true,  "0.75 SD", group = GL, inline = "k1")
show100   = input.bool(true,  "1.00 SD", group = GL, inline = "k2")
show150   = input.bool(false, "1.50 SD", group = GL, inline = "k2")
show200   = input.bool(false, "2.00 SD", group = GL, inline = "k2")
showAnc   = input.bool(true, "Show anchor (open) line", group = GL)
showTouch = input.bool(true, "Show touch probability in labels", group = GL)
 
// ─────────────────────────────── 3 · Style ────────────────────────────────
GS = "3 · Style"
colUp     = input.color(color.new(#00c853, 0),    "Upper",  group = GS, inline = "c")
colDn     = input.color(color.new(#ff1744, 0),    "Lower",  group = GS, inline = "c")
colAnc    = input.color(color.new(color.gray, 0), "Open",   group = GS, inline = "c")
lStyleIn  = input.string("Dashed", "Line style", options = ["Solid", "Dashed", "Dotted"], group = GS, inline = "l")
lWidth    = input.int(1, "Width", minval = 1, maxval = 5, group = GS, inline = "l")
lblSizeIn = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = GS)
lblOffset = input.int(5, "Label offset (bars right)", minval = 0, maxval = 100, group = GS)
showTable = input.bool(true, "Show ladder table", group = GS)
tblPosIn  = input.string("Top right", "Table position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = GS)
 
lineStyle = lStyleIn == "Solid" ? line.style_solid : lStyleIn == "Dotted" ? line.style_dotted : line.style_dashed
lblSize   = lblSizeIn == "Tiny" ? size.tiny : lblSizeIn == "Normal" ? size.normal : size.small
tblPos    = tblPosIn == "Bottom right" ? position.bottom_right : tblPosIn == "Top left" ? position.top_left : tblPosIn == "Bottom left" ? position.bottom_left : position.top_right
 
// ─────────────────────────────── Math ─────────────────────────────────────
f_erf(float x) =>
    float s  = x < 0 ? -1.0 : 1.0
    float ax = math.abs(x)
    float t  = 1.0 / (1.0 + 0.3275911 * ax)
    float y  = 1.0 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * math.exp(-ax * ax)
    s * y
 
f_ncdf(float x) =>
    0.5 * (1.0 + f_erf(x / math.sqrt(2.0)))
 
f_ptouch(float k) =>
    2.0 * (1.0 - f_ncdf(k))
 
// ─────────────────────────────── Anchor ───────────────────────────────────
f_openAt() =>
    var float o = na
    if hour(time, tz) == openH and minute(time, tz) == openM
        o := open
    o
 
float open1m = request.security(syminfo.tickerid, "1", f_openAt(), lookahead = barmerge.lookahead_off)
 
float tfMin    = timeframe.in_seconds() / 60.0
int   curMin   = hour(time, tz) * 60 + minute(time, tz)
int   prevMin  = hour(time[1], tz) * 60 + minute(time[1], tz)
int   target   = openH * 60 + openM
bool  newDay   = dayofmonth(time, tz) != dayofmonth(time[1], tz)
bool  contains = curMin <= target and curMin + tfMin > target
bool  gapPast  = curMin > target and (newDay or prevMin + tfMin <= target)
bool  isAnchorBar = timeframe.isintraday and (contains or gapPast)
 
var int   anchorBar   = na
var float chartAnchor = na
if isAnchorBar
    anchorBar   := bar_index
    chartAnchor := open
 
float anchor = useManual and manualAnc > 0 ? manualAnc : not na(open1m) ? open1m : chartAnchor
float iv     = ivIn >= 3.0 ? ivIn / 100.0 : ivIn
float em     = na(anchor) or iv <= 0 ? na : anchor * iv * math.sqrt(dte / daysYear)
 
// ─────────────────────────────── Drawing ──────────────────────────────────
f_c(table t, int c, int r, string txt, color col) =>
    t.cell(c, r, txt, text_color = col, text_size = size.small)
 
var array<line>  lns = array.new<line>()
var array<label> lbs = array.new<label>()
var table tbl = table.new(tblPos, 5, 10, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.new(color.gray, 70))
 
if barstate.islast
    while lns.size() > 0
        lns.pop().delete()
    while lbs.size() > 0
        lbs.pop().delete()
 
    array<float> ks = array.new<float>()
    if show025
        ks.push(0.25)
    if show050
        ks.push(0.5)
    if show075
        ks.push(0.75)
    if show100
        ks.push(1.0)
    if show150
        ks.push(1.5)
    if show200
        ks.push(2.0)
 
    if not na(em) and not na(anchorBar)
        if showAnc
            lns.push(line.new(anchorBar, anchor, bar_index, anchor, extend = extend.right, color = colAnc, style = line.style_dotted, width = 1))
            lbs.push(label.new(bar_index + lblOffset, anchor, "Open  " + str.tostring(anchor, "#.00"), style = label.style_label_left, color = color.new(colAnc, 85), textcolor = colAnc, size = lblSize))
        for k in ks
            float up  = anchor + k * em
            float dn  = anchor - k * em
            string pt = showTouch ? "  ·  " + str.tostring(f_ptouch(k) * 100.0, "#.0") + "%" : ""
            string kT = str.tostring(k, "0.00") + " SD"
            lns.push(line.new(anchorBar, up, bar_index, up, extend = extend.right, color = colUp, style = lineStyle, width = lWidth))
            lns.push(line.new(anchorBar, dn, bar_index, dn, extend = extend.right, color = colDn, style = lineStyle, width = lWidth))
            lbs.push(label.new(bar_index + lblOffset, up, "+" + kT + "  " + str.tostring(up, "#.00") + pt, style = label.style_label_left, color = color.new(colUp, 85), textcolor = colUp, size = lblSize))
            lbs.push(label.new(bar_index + lblOffset, dn, "-" + kT + "  " + str.tostring(dn, "#.00") + pt, style = label.style_label_left, color = color.new(colDn, 85), textcolor = colDn, size = lblSize))
 
    if showTable
        tbl.clear(0, 0, 4, 9)
        color hc = color.gray
        color tc = color.white
        if iv <= 0
            f_c(tbl, 0, 0, "Enter today's IV in settings", tc)
        else if na(em)
            f_c(tbl, 0, 0, "Waiting for anchor open", tc)
        else
            f_c(tbl, 0, 0, "Open", hc)
            f_c(tbl, 1, 0, str.tostring(anchor, "#.00"), tc)
            f_c(tbl, 2, 0, "IV", hc)

← 返回列表