Earnings Gap Levels by Momentum_trade

Momentum_trade · study · 231 行 · 点赞 10 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Momentum_trade
 
//@version=6
indicator("Earnings Gap Levels by Momentum_trade", shorttitle = "Earnings Gaps", overlay = true, max_lines_count = 100, max_labels_count = 100)
 
// ───────────────────────────── Inputs ─────────────────────────────
grpG = "Gap detection"
minGap   = input.float(3.0, "Min gap %", minval = 0.5, step = 0.5, group = grpG)
lookDays = input.int(2, "Days after report to look for the gap", minval = 1, maxval = 3, group = grpG,
     tooltip = "1 = only the report day (before-open reports). 2 = report day or the next session (covers after-close reports).")
keepN    = input.int(1, "Earnings gaps to keep", minval = 1, maxval = 8, group = grpG)
 
grpS = "Display"
showHL     = input.bool(false, "Gap-day high / low", group = grpS)
showMid    = input.bool(true,  "50% gap fill", group = grpS)
hideFilled = input.bool(false, "Hide fully filled gaps", group = grpS)
showTable  = input.bool(true,  "Summary table", group = grpS)
tblPos     = input.string("bottom right", "Table position", options = ["bottom right", "bottom left", "bottom center", "top right", "top left"], group = grpS)
lblOffset  = input.int(10, "Label offset, bars", minval = 0, maxval = 200, group = grpS)
lblSize    = input.string("small", "Label size", options = ["tiny", "small", "normal"], group = grpS)
 
grpC = "Colors"
cOpen = input.color(#F5D547, "Gap open", group = grpC)
cPrev = input.color(#8A93A6, "Pre-gap close (full fill)", group = grpC)
cMid  = input.color(#FF9F43, "50% fill", group = grpC)
cHi   = input.color(#22C58B, "Gap-day high", group = grpC)
cLo   = input.color(#F0506E, "Gap-day low", group = grpC)
 
sz = lblSize == "tiny" ? size.tiny : lblSize == "normal" ? size.normal : size.small
 
if timeframe.isweekly or timeframe.ismonthly
    runtime.error("Earnings Gap Levels works on daily and intraday charts.")
 
// ───────────────────────────── Data ─────────────────────────────
// Regular-session values are built on the chart itself, so intraday charts with
// extended hours use the 9:30 open and the 16:00 close, like a daily chart.
bool isDaily = timeframe.isdaily
var int   sessIdx   = -1
var float lastClose = na    // last regular-session close
var float prevClose = na    // regular close before the current session
var float sessOpen  = na
var float sessHi    = na
var float sessLo    = na
 
bool newSess = isDaily ? true : session.isfirstbar_regular
if newSess
    sessIdx   += 1
    prevClose := isDaily ? close[1] : lastClose
    sessOpen  := open
    sessHi    := high
    sessLo    := low
else if isDaily or session.ismarket
    sessHi := math.max(sessHi, high)
    sessLo := math.min(sessLo, low)
if isDaily or session.ismarket
    lastClose := close
 
// Earnings event: a value appears only on the bar where a new report arrives.
epsNew = request.earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_off, ignore_invalid_symbol = true)
 
var int earnSess = na
var int evCount  = 0
if not na(epsNew)
    earnSess := sessIdx
    evCount  += 1
 
gapPct = (sessOpen - prevClose) / prevClose * 100
 
// Checked once per session, on its first regular bar: the report day (before-open
// reports) and the next session (after-close reports).
int  sinceRep = sessIdx - nz(earnSess, -1000)
bool checkGap = newSess and not na(earnSess) and not na(prevClose) and sinceRep >= 0 and sinceRep < lookDays and math.abs(gapPct) >= minGap
 
// ───────────────────────────── Gap object ─────────────────────────────
type EGap
    int   dIdx
    bool  up
    float pct
    float prev
    float gOpen
    float hi
    float lo
    bool  halfHit = false
    bool  filled  = false
    line  lPrev
    line  lOpen
    line  lMid
    line  lHi
    line  lLo
    label bPrev
    label bOpen
    label bMid
    label bHi
    label bLo
 
fmt(float v) => str.tostring(v, format.mintick)
 
newLine(float y, color c, int w, string st) =>
    line.new(bar_index, y, bar_index + 1, y, extend = extend.right, color = c, width = w, style = st)
 
newLbl(float y, color c) =>
    label.new(bar_index + lblOffset, y, "", style = label.style_label_left, color = color.new(color.black, 100), textcolor = c, size = sz)
 
method make(EGap g) =>
    float m = (g.prev + g.gOpen) / 2
    g.lPrev := newLine(g.prev,  cPrev, 1, line.style_dotted)
    g.lOpen := newLine(g.gOpen, cOpen, 2, line.style_dashed)
    g.bPrev := newLbl(g.prev,  cPrev)
    g.bOpen := newLbl(g.gOpen, cOpen)
    if showMid
        g.lMid := newLine(m, cMid, 1, line.style_solid)
        g.bMid := newLbl(m, cMid)
    if showHL
        g.lHi := newLine(g.hi, cHi, 1, line.style_dotted)
        g.lLo := newLine(g.lo, cLo, 1, line.style_dotted)
        g.bHi := newLbl(g.hi, cHi)
        g.bLo := newLbl(g.lo, cLo)
    g
 
method del(EGap g) =>
    line.delete(g.lPrev)
    line.delete(g.lOpen)
    line.delete(g.lMid)
    line.delete(g.lHi)
    line.delete(g.lLo)
    label.delete(g.bPrev)
    label.delete(g.bOpen)
    label.delete(g.bMid)
    label.delete(g.bHi)
    label.delete(g.bLo)
 
method upd(EGap g, int curIdx) =>
    bool halfNow = false
    bool fillNow = false
    // Gap-day high/low keep updating until that session ends
    if curIdx == g.dIdx
        g.hi := sessHi
        g.lo := sessLo
    float m = (g.prev + g.gOpen) / 2
    if not g.halfHit and (g.up ? low <= m : high >= m)
        g.halfHit := true
        halfNow   := true
    if not g.filled and (g.up ? low <= g.prev : high >= g.prev)
        g.filled := true
        fillNow  := true
    int d = curIdx - g.dIdx
    int x = bar_index + lblOffset
    string sign = g.pct > 0 ? "+" : ""
    label.set_xy(g.bOpen, x, g.gOpen)
    label.set_text(g.bOpen, fmt(g.gOpen) + "  EARNINGS GAP " + sign + str.tostring(g.pct, "#.#") + "%  D" + str.tostring(d))
    label.set_xy(g.bPrev, x, g.prev)
    label.set_text(g.bPrev, fmt(g.prev) + "  FULL FILL" + (g.filled ? " ✓" : ""))
    if g.filled
        line.set_style(g.lPrev, line.style_solid)
    if showMid
        label.set_xy(g.bMid, x, m)
        label.set_text(g.bMid, fmt(m) + "  50% FILL" + (g.halfHit ? " ✓" : ""))
    if showHL
        line.set_y1(g.lHi, g.hi)
        line.set_y2(g.lHi, g.hi)
        line.set_y1(g.lLo, g.lo)
        line.set_y2(g.lLo, g.lo)
        label.set_xy(g.bHi, x, g.hi)
        label.set_text(g.bHi, fmt(g.hi) + "  GAP-DAY HIGH")
        label.set_xy(g.bLo, x, g.lo)
        label.set_text(g.bLo, fmt(g.lo) + "  GAP-DAY LOW")
    [halfNow, fillNow]
 
// ───────────────────────────── Logic ─────────────────────────────
var array<EGap> gaps = array.new<EGap>()
 
var int gapEarn = na   // report the latest gap belongs to
if checkGap
    bool sameReport = nz(gapEarn, -1) == earnSess and gaps.size() > 0
    bool bigger = true
    if sameReport
        bigger := math.abs(gapPct) > math.abs(gaps.last().pct)
    if bigger
        // after-close report: next session's gap replaces the report-day move
        if sameReport
            EGap prevG = gaps.pop()
            prevG.del()
        EGap g = EGap.new(sessIdx, gapPct > 0, gapPct, prevClose, sessOpen, sessHi, sessLo)
        g.make()
        gaps.push(g)
        gapEarn := earnSess
        if gaps.size() > keepN
            EGap old = gaps.shift()
            old.del()
 
bool alertHalf = false
bool alertFill = false
int  n = gaps.size()
if n > 0
    for i = n - 1 to 0
        EGap g = gaps.get(i)
        [h, f] = g.upd(sessIdx)
        if i == n - 1
            alertHalf := h
            alertFill := f
        if hideFilled and g.filled and i != n - 1
            g.del()
            gaps.remove(i)
 
// ───────────────────────────── Summary table ─────────────────────────────
var table tb = na
if showTable and na(tb)
    tb := table.new(tblPos == "bottom left" ? position.bottom_left : tblPos == "bottom center" ? position.bottom_center : tblPos == "top right" ? position.top_right : tblPos == "top left" ? position.top_left : position.bottom_right, 2, 5, bgcolor = color.new(#131722, 10), border_color = color.new(#8A93A6, 70), border_width = 1)
 
if showTable and barstate.islast
    if gaps.size() > 0
        EGap g = gaps.last()
        float m = (g.prev + g.gOpen) / 2
        string sign = g.pct > 0 ? "+" : ""
        table.cell(tb, 0, 0, "Last earnings gap", text_color = #E6E8EE, text_size = sz)
        table.cell(tb, 1, 0, sign + str.tostring(g.pct, "#.#") + "%", text_color = g.up ? cHi : cLo, text_size = sz)
        table.cell(tb, 0, 1, "Sessions since", text_color = #E6E8EE, text_size = sz)
        table.cell(tb, 1, 1, str.tostring(sessIdx - g.dIdx), text_color = #E6E8EE, text_size = sz)
        table.cell(tb, 0, 2, "50% fill", text_color = #E6E8EE, text_size = sz)
        table.cell(tb, 1, 2, fmt(m) + (g.halfHit ? "  hit" : "  open"), text_color = cMid, text_size = sz)
        table.cell(tb, 0, 3, "Full fill", text_color = #E6E8EE, text_size = sz)
        table.cell(tb, 1, 3, fmt(g.prev) + (g.filled ? "  filled" : "  open"), text_color = cPrev, text_size = sz)
        table.cell(tb, 0, 4, "Gap-day range", text_color = #E6E8EE, text_size = sz)
        table.cell(tb, 1, 4, fmt(g.lo) + " - " + fmt(g.hi), text_color = #E6E8EE, text_size = sz)
    else
        table.cell(tb, 0, 0, "No earnings gap ≥ " + str.tostring(minGap) + "% found (reports seen: " + str.tostring(evCount) + ")", text_color = #8A93A6, text_size = sz)
 
// ───────────────────────────── Alerts ─────────────────────────────
alertcondition(alertHalf, "Latest earnings gap: 50% fill touched", "{{ticker}} touched the 50% fill of its latest earnings gap")
alertcondition(alertFill, "Latest earnings gap: fully filled", "{{ticker}} fully filled its latest earnings gap")

← 返回列表