CA Event Expected-Move Map v1

Crypto_Andy_ · study · 177 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
// CA Event Expected-Move Map v1
// Changelog: v1 - initial release.
//
// Chart-analysis indicator, symbol-agnostic. Measures how price actually reacted to a scheduled event
// (earnings, macro print) against the move the market expected before it.
//   - Anchor:        close of the last bar before the event bar (no data from after the event is used to set it).
//   - Expected move: either a manual % (e.g. the options-implied move quoted before the event)
//                    or a multiple of the prior day's daily ATR. Bands at anchor +/- 1 EM (and optional 2 EM).
//   - Reaction:      post-event high/low and the current close, in % and in EM multiples, plus how much of
//                    the initial spike is still kept.
//   - Context:       daily SMA (default 50) taken from the previous completed daily bar, so it never repaints.
//   - Optional manual levels (0 = off) for upside/downside levels quoted by an outside source.
// Nothing here is a signal. Bands and table are drawn on the last bar; post-event extremes include the
// current, still-open bar.
 
indicator("CA Event Expected-Move Map v1", shorttitle="CA Event EM v1", overlay=true, max_lines_count=50, max_labels_count=50, max_boxes_count=10)
 
// ───────────── Inputs: event ─────────────
string gEv = "Event"
int    evTime  = input.time(timestamp("21 Oct 2026 20:00 +0000"), "Event time", group=gEv, tooltip="Time of the release. The bar containing this time is the event bar; the anchor is the close of the bar before it.")
string evLabel = input.string("Earnings", "Event label", group=gEv)
 
// ───────────── Inputs: expected move ─────────────
string gEm = "Expected move"
string emMode    = input.string("Manual %", "Expected move source", options=["Manual %", "Daily ATR multiple"], group=gEm)
float  emPct     = input.float(3.2, "Manual expected move (%)", minval=0.1, maxval=50.0, step=0.1, group=gEm, tooltip="E.g. the options-implied move quoted before the event.")
int    dAtrLen   = input.int(14, "Daily ATR length", minval=1, group=gEm)
float  dAtrMult  = input.float(1.0, "Daily ATR multiple", minval=0.1, maxval=10.0, step=0.1, group=gEm)
bool   show2Em   = input.bool(true, "Show 2x expected move bands", group=gEm)
 
// ───────────── Inputs: context levels ─────────────
string gLv = "Context levels"
bool   showSma  = input.bool(true, "Show daily SMA", group=gLv)
int    smaLen   = input.int(50, "Daily SMA length", minval=2, maxval=400, group=gLv)
float  manUp    = input.float(0.0, "Manual upside level (0 = off)", minval=0.0, group=gLv, tooltip="A level quoted by an outside source. Visual reference only.")
float  manDn    = input.float(0.0, "Manual downside level (0 = off)", minval=0.0, group=gLv)
 
// ───────────── Inputs: visuals ─────────────
string gVis = "Visuals"
bool   showBands = input.bool(true, "Show expected-move bands", group=gVis)
bool   showExt   = input.bool(true, "Mark post-event high and low", group=gVis)
int    extBars   = input.int(8, "Extend lines right (bars)", minval=0, maxval=50, group=gVis)
bool   showTable = input.bool(true, "Show table", group=gVis)
string tablePos  = input.string("Top right", "Table position", options=["Top right", "Bottom right", "Top left", "Bottom left"], group=gVis)
 
// ───────────── Core series ─────────────
float atr  = ta.atr(14)
float dAtr = request.security(syminfo.tickerid, "D", ta.atr(dAtrLen)[1], lookahead=barmerge.lookahead_on)
float dSma = request.security(syminfo.tickerid, "D", ta.sma(close, smaLen)[1], lookahead=barmerge.lookahead_on)
 
// ───────────── Helpers ─────────────
px(float x) => na(x) ? "-" : str.tostring(math.round_to_mintick(x), format.mintick)
num(float x) => na(x) ? "-" : str.tostring(x, "#.##")
sgnPct(float x) => na(x) ? "-" : (x >= 0 ? "+" : "") + str.tostring(x, "#.##") + "%"
sgnNum(float x) => na(x) ? "-" : (x >= 0 ? "+" : "") + str.tostring(x, "#.##")
 
// ───────────── Event anchor ─────────────
var int   evBar  = na
var int   evBarT = na
var float anchor = na
var float em     = na
var float postHi = na
var float postLo = na
var int   postHiBar = na
var int   postLoBar = na
 
if na(evBar) and evTime >= time and evTime < time_close and bar_index > 0
    evBar  := bar_index
    evBarT := time
    anchor := close[1]
    em     := emMode == "Manual %" ? anchor * emPct / 100.0 : (na(dAtr) ? na : dAtr * dAtrMult)
 
if not na(evBar)
    if na(postHi) or high > postHi
        postHi := high
        postHiBar := bar_index
    if na(postLo) or low < postLo
        postLo := low
        postLoBar := bar_index
 
// ───────────── Plots ─────────────
plot(showSma ? dSma : na, "Daily SMA", color=color.new(color.orange, 0), linewidth=2)
 
// ───────────── Drawings (last bar) ─────────────
var array<line>  lns  = array.new<line>()
var array<label> lbls = array.new<label>()
var box emBox = na
 
clearAll() =>
    for l in lns
        l.delete()
    lns.clear()
    for lb in lbls
        lb.delete()
    lbls.clear()
 
hLevel(int x1, float y, string txt, color col, string st, int w) =>
    lns.push(line.new(x1, y, bar_index + extBars, y, color=col, style=st, width=w))
    lbls.push(label.new(bar_index + extBars, y, txt, style=label.style_label_left, color=color.new(col, 20), textcolor=color.white, size=size.small))
 
bool hasEv = not na(evBar) and not na(anchor) and anchor > 0
bool hasEm = hasEv and not na(em) and em > 0
 
if barstate.islast
    clearAll()
    box.delete(emBox)
    if hasEv
        lns.push(line.new(evBar, low, evBar, high, extend=extend.both, color=color.new(color.gray, 40), style=line.style_dotted))
        lbls.push(label.new(evBar, postHi, evLabel, style=label.style_label_down, yloc=yloc.abovebar, color=color.new(color.gray, 20), textcolor=color.white, size=size.small))
        hLevel(evBar - 1, anchor, "Pre-event " + px(anchor), color.gray, line.style_solid, 1)
    if hasEm and showBands
        emBox := box.new(evBar, anchor + em, bar_index + extBars, anchor - em, border_color=color.new(color.gray, 100), bgcolor=color.new(color.blue, 90))
        hLevel(evBar, anchor + em, "+1 EM " + px(anchor + em), color.teal, line.style_dashed, 1)
        hLevel(evBar, anchor - em, "-1 EM " + px(anchor - em), color.red, line.style_dashed, 1)
        if show2Em
            hLevel(evBar, anchor + 2 * em, "+2 EM " + px(anchor + 2 * em), color.new(color.teal, 40), line.style_dotted, 1)
            hLevel(evBar, anchor - 2 * em, "-2 EM " + px(anchor - 2 * em), color.new(color.red, 40), line.style_dotted, 1)
    if hasEv and showExt
        lbls.push(label.new(postHiBar, postHi, "Post-event high " + px(postHi), style=label.style_label_down, color=color.new(color.teal, 100), textcolor=color.teal, size=size.small))
        lbls.push(label.new(postLoBar, postLo, "Post-event low " + px(postLo), style=label.style_label_up, color=color.new(color.red, 100), textcolor=color.red, size=size.small))
    int x0 = hasEv ? evBar : math.max(0, bar_index - 50)
    if manUp > 0
        hLevel(x0, manUp, "Upside ref " + px(manUp), color.yellow, line.style_dashed, 1)
    if manDn > 0
        hLevel(x0, manDn, "Downside ref " + px(manDn), color.yellow, line.style_dashed, 1)
 
// ───────────── Table ─────────────
string tPos = tablePos == "Top right" ? position.top_right : tablePos == "Bottom right" ? position.bottom_right : tablePos == "Top left" ? position.top_left : position.bottom_left
var table tbl = table.new(tPos, 2, 12, bgcolor=color.new(#1e222d, 8), border_width=1, border_color=color.new(color.gray, 60))
color tW = color.white
color tG = color.new(color.gray, 10)
 
cell(int c, int r, string t, color tc) =>
    table.cell(tbl, c, r, t, text_color=tc, text_size=size.small, text_halign=c == 0 ? text.align_left : text.align_right)
 
if barstate.islast and showTable
    if not hasEv
        cell(0, 0, "Event reaction", tW)
        cell(1, 0, "Event time not on the loaded chart", color.orange)
    else
        float upPct  = (postHi - anchor) / anchor * 100
        float dnPct  = (postLo - anchor) / anchor * 100
        float nowPct = (close - anchor) / anchor * 100
        float upEm   = hasEm ? (postHi - anchor) / em : na
        float dnEm   = hasEm ? (postLo - anchor) / em : na
        float nowEm  = hasEm ? (close - anchor) / em : na
        float kept   = postHi > anchor ? (close - anchor) / (postHi - anchor) * 100 : na
        string status = not hasEm ? "-" : nowEm > 1 ? "Above expected move" : nowEm < -1 ? "Below expected move" : (upEm > 1 or dnEm < -1) ? "Overshot, now back inside" : "Inside expected move"
        color  stCol  = not hasEm ? tG : nowEm > 1 ? color.teal : nowEm < -1 ? color.red : color.orange
        float smaDist = na(dSma) ? na : (close - dSma) / dSma * 100
        float smaAtr  = na(dSma) or na(atr) or atr <= 0 ? na : (close - dSma) / atr
        cell(0, 0, "Event reaction", tW)
        cell(1, 0, evLabel + ", " + str.format_time(evBarT, "dd MMM HH:mm", "UTC") + " UTC bar", tW)
        cell(0, 1, "Pre-event close (anchor)", tW)
        cell(1, 1, px(anchor), tW)
        cell(0, 2, "Expected move", tW)
        cell(1, 2, hasEm ? "±" + px(em) + " (" + num(em / anchor * 100) + "%)" : "-", tW)
        cell(0, 3, "1 EM band", tW)
        cell(1, 3, hasEm ? px(anchor - em) + " – " + px(anchor + em) : "-", tW)
        cell(0, 4, "Post-event high", tW)
        cell(1, 4, px(postHi) + "  " + sgnPct(upPct) + "  (" + sgnNum(upEm) + " EM)", color.teal)
        cell(0, 5, "Post-event low", tW)
        cell(1, 5, px(postLo) + "  " + sgnPct(dnPct) + "  (" + sgnNum(dnEm) + " EM)", color.red)
        cell(0, 6, "Now vs anchor", tW)
        cell(1, 6, px(close) + "  " + sgnPct(nowPct) + "  (" + sgnNum(nowEm) + " EM)", nowPct >= 0 ? color.teal : color.red)
        cell(0, 7, "Spike kept", tW)
        cell(1, 7, na(kept) ? "-" : num(kept) + "%", na(kept) ? tG : kept >= 50 ? color.teal : color.red)
        cell(0, 8, "Status", tW)
        cell(1, 8, status, stCol)
        cell(0, 9, "Daily SMA(" + str.tostring(smaLen) + ")", tW)
        cell(1, 9, na(dSma) ? "-" : px(dSma) + "  (" + sgnPct(smaDist) + ", " + sgnNum(smaAtr) + " ATR)", color.orange)
        string refTxt = (manUp > 0 ? "up " + px(manUp) + " (" + sgnPct((manUp - close) / close * 100) + ")" : "") + (manUp > 0 and manDn > 0 ? " / " : "") + (manDn > 0 ? "down " + px(manDn) + " (" + sgnPct((manDn - close) / close * 100) + ")" : "")
        cell(0, 10, "Reference levels", tW)
        cell(1, 10, refTxt == "" ? "off" : refTxt, color.yellow)
        cell(0, 11, "EM = expected move. Spike kept = share of the move from anchor to post-event high still held. Post-event values include the open bar.", tG)
        table.merge_cells(tbl, 0, 11, 1, 11)

← 返回列表