Opening Gaps [Ordinary Trader]

Ordinary-Trader · study · 122 行 · 点赞 1 · TradingView 原页

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

Pine Script

// ══════════════════════════════════════════════════════════════════════════════
// Opening Gaps [Ordinary Trader] v1.0.0
// See CHANGELOG.md for full version history.
// ══════════════════════════════════════════════════════════════════════════════
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("Opening Gaps [Ordinary Trader] v1.0.0", shorttitle = "Opening Gaps [Ordinary Trader]", overlay = true, max_boxes_count = 100, max_lines_count = 100, max_labels_count = 100)
 
// ═════════════════════════════════════════════════════════════════════════════
// 1. Inputs
// ═════════════════════════════════════════════════════════════════════════════
const string G_GAP = "Opening gaps"
bool   nwogOn   = input.bool(true, "NWOG", inline = "nw", group = G_GAP, display = display.none)
int    nwogN    = input.int(1, "show last", minval = 1, maxval = 10, inline = "nw", group = G_GAP, display = display.none)
color  nwogCol  = input.color(#F2CC8F4D, "", inline = "nw", group = G_GAP, tooltip = "New Week Opening Gap: from Friday's 17:00 close to Sunday's 18:00 open (New York time). Show last: how many of the latest NWOGs to draw.", display = display.none)
bool   ndogOn   = input.bool(true, "NDOG", inline = "nd", group = G_GAP, display = display.none)
int    ndogN    = input.int(2, "show last", minval = 1, maxval = 10, inline = "nd", group = G_GAP, display = display.none)
color  ndogCol  = input.color(#F2CC8F4D, "", inline = "nd", group = G_GAP, tooltip = "New Day Opening Gap: from the 17:00 close to the 18:00 reopen (New York time), except Sunday, which is the NWOG. Show last: how many of the latest NDOGs to draw.", display = display.none)
bool   ogFilled = input.bool(false, "Show filled gaps", group = G_GAP, tooltip = "A gap is filled once price trades through it. Filled gaps are hidden unless this is ticked; then they stop at the fill and fade.", display = display.none)
bool   ogCE     = input.bool(true, "Show CE (50%)", group = G_GAP, tooltip = "A dotted line through the middle of each gap (consequent encroachment).", display = display.none)
 
const string G_EH = "EHPDA"
bool   ehOn     = input.bool(true, "EHPDA", inline = "eh", group = G_EH, display = display.none)
color  ehCol    = input.color(#787B86, "", inline = "eh", group = G_EH, tooltip = "Event Horizon PD Array: a dashed line halfway between the nearest unfilled NWOG above price and the nearest below. Needs NWOG on.", display = display.none)
 
const string TZ = "America/New_York"   // label dates
 
// ═════════════════════════════════════════════════════════════════════════════
// 2. Types and state
// ═════════════════════════════════════════════════════════════════════════════
type OGap
    bool  week             // NWOG (true) or NDOG
    float top
    float btm
    float prevClose        // the side price has to reach to fill it
    int   t                // the reopen candle's time
    bool  filled = false
    int   fillT
 
var array<OGap>  ogaps   = array.new<OGap>()
var array<box>   ogBoxes = array.new<box>()
var array<line>  ogLines = array.new<line>()
var array<label> ogLbls  = array.new<label>()
 
bool newDay  = timeframe.change("D")
bool newWeek = timeframe.change("W")
 
// ═════════════════════════════════════════════════════════════════════════════
// 3. Detection
// ═════════════════════════════════════════════════════════════════════════════
// A new gap at the reopen (18:00 ET): the new week's first candle makes an NWOG, any other day's an NDOG.
if timeframe.isintraday and not na(close[1]) and (newWeek or newDay) and math.abs(open - close[1]) >= syminfo.mintick
    bool wk = newWeek
    if wk ? nwogOn : ndogOn
        ogaps.push(OGap.new(wk, math.max(open, close[1]), math.min(open, close[1]), close[1], time))
        // Keep a few more NWOGs than shown, for the EHPDA.
        int kept = 0
        for i = ogaps.size() - 1 to 0
            OGap g = ogaps.get(i)
            if g.week == wk
                kept += 1
                if kept > (wk ? math.max(nwogN, 10) : ndogN)
                    ogaps.remove(i)
 
// Filled: price traded back to the previous close, all the way through the gap.
for g in ogaps
    if not g.filled and time >= g.t and (g.prevClose == g.btm ? low <= g.btm : high >= g.top)
        g.filled := true
        g.fillT  := time
 
// ═════════════════════════════════════════════════════════════════════════════
// 4. Drawing (rebuilt on the last bar)
// ═════════════════════════════════════════════════════════════════════════════
if barstate.islast
    for b in ogBoxes
        box.delete(b)
    for l in ogLines
        line.delete(l)
    for lb in ogLbls
        label.delete(lb)
    ogBoxes.clear()
    ogLines.clear()
    ogLbls.clear()
    int nW = 0
    int nD = 0
    for i = ogaps.size() - 1 to 0
        if ogaps.size() == 0
            break
        OGap g = ogaps.get(i)
        int n = g.week ? nW : nD
        if n < (g.week ? nwogN : ndogN)
            if g.week
                nW += 1
            else
                nD += 1
            if not g.filled or ogFilled
                color col = g.week ? nwogCol : ndogCol
                color c   = g.filled ? color.new(col, math.min(100, color.t(col) + 10)) : col
                int   r   = g.filled ? g.fillT : time
                string nm = (g.week ? "NWOG " : "NDOG ") + str.format_time(g.t, "MM/dd", TZ)
                color ln  = g.filled ? color.new(#787B86, 60) : #787B86
                ogBoxes.push(box.new(g.t, g.top, r, g.btm, xloc = xloc.bar_time, border_color = color(na), bgcolor = c))
                // Solid edges above and below, dotted CE in the middle.
                ogLines.push(line.new(g.t, g.top, r, g.top, xloc = xloc.bar_time, color = ln, style = line.style_solid))
                ogLines.push(line.new(g.t, g.btm, r, g.btm, xloc = xloc.bar_time, color = ln, style = line.style_solid))
                if ogCE
                    ogLines.push(line.new(g.t, (g.top + g.btm) / 2, r, (g.top + g.btm) / 2, xloc = xloc.bar_time, color = ln, style = line.style_dotted))
                ogLbls.push(label.new(r, g.top, nm, xloc = xloc.bar_time, color = color(na), style = label.style_label_lower_left, textcolor = ln, size = size.small))
    // EHPDA: halfway between the nearest unfilled NWOG above price and the nearest below.
    if ehOn and nwogOn
        OGap up = na
        OGap dn = na
        for g in ogaps
            if g.week and not g.filled
                if g.btm > close and (na(up) or g.btm < up.btm)
                    up := g
                else if g.top < close and (na(dn) or g.top > dn.top)
                    dn := g
        if not na(up) and not na(dn)
            float eh = (up.btm + dn.top) / 2
            ogLines.push(line.new(math.max(up.t, dn.t), eh, time, eh, xloc = xloc.bar_time, color = ehCol, style = line.style_dashed))
            ogLbls.push(label.new(time, eh, "EHPDA", xloc = xloc.bar_time, color = color(na), style = label.style_label_left, textcolor = ehCol, size = size.small))

← 返回列表