MonoIntel Opening Gaps: New Day and New Week Gaps

monobosh · study · 218 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
indicator("MonoIntel Opening Gaps: New Day and New Week Gaps", shorttitle="MI Gaps", overlay=true,
     max_boxes_count=500, max_lines_count=500, max_labels_count=500)
 
// Built for CME futures on charts from 1 minute to 4 hours. All times are New York.
// NDOG (New Day Opening Gap): the last price before the 17:00 close to the next 18:00 open.
// NWOG (New Week Opening Gap): the last price before the weekend halt to the Sunday 18:00 open.
// The gap starts at the close of the last bar before the halt, so early closes (Thanksgiving,
// Christmas Eve, holiday Mondays) use the real last price, not a fixed clock time.
// A gap is filled once price trades back to the level it started from.
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 1 — INPUTS
// ══════════════════════════════════════════════════════════════════════════════
 
// ── NDOG ─────────────────────────────────────────────────────────────────────
string G_NDOG = "NDOG (New Day Opening Gap)"
i_showNdog       = input.bool(true, "Show NDOG", group=G_NDOG,
     tooltip="Draw the gap between each day's close and the next open.")
i_ndogMinTicks   = input.int(1, "Min gap size (ticks)", group=G_NDOG, minval=1,
     tooltip="Only gaps of at least this many ticks are drawn.")
i_ndogShowMid    = input.bool(true, "Show midpoint", group=G_NDOG,
     tooltip="Draw a line through the middle of the gap.")
i_ndogHistory    = input.int(5, "Gaps kept", group=G_NDOG, minval=1, maxval=20,
     tooltip="How many NDOGs stay on the chart. The oldest is removed when a new one is added.")
i_ndogShowFilled = input.bool(false, "Show mitigated gaps", group=G_NDOG,
     tooltip="Keep filled gaps on the chart, faded with a dotted border.")
i_ndogFillColor  = input.color(color.new(#2196F3, 92), "Fill color", group=G_NDOG,
     tooltip="Shading inside an open NDOG.")
 
// ── NWOG ─────────────────────────────────────────────────────────────────────
string G_NWOG = "NWOG (New Week Opening Gap)"
i_showNwog       = input.bool(true, "Show NWOG", group=G_NWOG,
     tooltip="Draw the gap between the last price of the week and the Sunday open.")
i_nwogMinTicks   = input.int(1, "Min gap size (ticks)", group=G_NWOG, minval=1,
     tooltip="Only gaps of at least this many ticks are drawn.")
i_nwogShowMid    = input.bool(true, "Show midpoint", group=G_NWOG,
     tooltip="Draw a line through the middle of the gap.")
i_nwogHistory    = input.int(3, "Gaps kept", group=G_NWOG, minval=1, maxval=10,
     tooltip="How many NWOGs stay on the chart. The oldest is removed when a new one is added.")
i_nwogShowFilled = input.bool(false, "Show mitigated gaps", group=G_NWOG,
     tooltip="Keep filled gaps on the chart, faded with a dotted border.")
i_nwogFillColor  = input.color(color.new(#00bcd4, 92), "Fill color", group=G_NWOG,
     tooltip="Shading inside an open NWOG.")
 
// ── Style ────────────────────────────────────────────────────────────────────
i_borderColor = input.color(color.black, "Border / line color", group="Style",
     tooltip="Colour of the gap border and midpoint line.")
i_borderWidth = input.int(1, "Border width", group="Style", minval=1, maxval=4,
     tooltip="Thickness of the gap border and midpoint line.")
i_labelColor  = input.color(color.black, "Label text color", group="Style",
     tooltip="Colour of the NDOG and NWOG labels.")
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 2 — CONSTANTS + HELPERS
// ══════════════════════════════════════════════════════════════════════════════
 
string TZ       = "America/New_York"
int    DAY_MS   = 86400000
int    SHIFT_MS = 6 * 3600000      // moves the 18:00 open onto the next calendar day
bool active = timeframe.isintraday and timeframe.in_seconds() <= 4 * 3600   // 4 hour charts and below
 
// A much more transparent version of a colour, for filled gaps.
f_fade(color c) =>
    color.new(c, 100 - (100 - color.t(c)) * 0.35)
 
// Trading week number, Monday based: a session opening at 18:00 belongs to the next
// calendar day, and day 0 (1 Jan 1970) was a Thursday.
f_tradingWeek(int t) =>
    int x   = t + SHIFT_MS
    int day = int(timestamp("UTC", year(x, TZ), month(x, TZ), dayofmonth(x, TZ)) / DAY_MS)
    math.floor((day + 3) / 7)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 3 — NEW SESSION DETECTION
// ══════════════════════════════════════════════════════════════════════════════
// The first bar of a session is the 18:00 open. The bar before it is the last bar
// before the halt, so its close is the last traded price before the close.
// A session that starts a new trading week is the weekly open (NWOG). A mid-week
// holiday (Christmas or New Year's Day on a Thursday) stays an NDOG.
 
bool  newSession = active and session.isfirstbar and not na(close[1])
bool  isNewWeek  = newSession and f_tradingWeek(time) != f_tradingWeek(time[1])
float gapFrom    = close[1]
int   gapFromX   = time[1]
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 4 — STATE
// ══════════════════════════════════════════════════════════════════════════════
// Parallel arrays per gap type, index 0 = oldest. All are updated together.
// gapUps: true when the open is above the close, so the gap fills when price
// trades back down to the lower edge; a gap down fills at the upper edge.
 
var box[]   ndog_boxes  = array.new<box>(0)
var line[]  ndog_mids   = array.new<line>(0)
var label[] ndog_labels = array.new<label>(0)
var float[] ndog_uppers = array.new<float>(0)
var float[] ndog_lowers = array.new<float>(0)
var bool[]  ndog_filled = array.new<bool>(0)
var bool[]  ndog_gapUps = array.new<bool>(0)
 
var box[]   nwog_boxes  = array.new<box>(0)
var line[]  nwog_mids   = array.new<line>(0)
var label[] nwog_labels = array.new<label>(0)
var float[] nwog_uppers = array.new<float>(0)
var float[] nwog_lowers = array.new<float>(0)
var bool[]  nwog_filled = array.new<bool>(0)
var bool[]  nwog_gapUps = array.new<bool>(0)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 5 — DRAWING HELPERS
// ══════════════════════════════════════════════════════════════════════════════
 
// Prune the oldest gap when at the limit, then draw a new box, midpoint and label.
f_spawn(array<box> boxes, array<line> mids, array<label> labels,
     array<float> uppers, array<float> lowers, array<bool> filled, array<bool> gapUps,
     int historyLimit, int x1, float upper, float lower, color fillColor,
     bool showMid, string labelText, bool isGapUp) =>
    if array.size(boxes) >= historyLimit
        box.delete(array.shift(boxes))
        line.delete(array.shift(mids))
        label.delete(array.shift(labels))
        array.shift(uppers)
        array.shift(lowers)
        array.shift(filled)
        array.shift(gapUps)
 
    float mid = (upper + lower) / 2.0
    bx = box.new(left=x1, top=upper, right=time, bottom=lower, xloc=xloc.bar_time,
         bgcolor=fillColor, border_color=i_borderColor, border_width=i_borderWidth)
    line midLn = showMid ? line.new(x1=x1, y1=mid, x2=time, y2=mid, xloc=xloc.bar_time,
         color=i_borderColor, width=i_borderWidth) : na
    lb = label.new(x=time, y=mid, text=labelText, xloc=xloc.bar_time,
         style=label.style_none, textcolor=i_labelColor, size=size.small)
 
    array.push(boxes,  bx)
    array.push(mids,   midLn)
    array.push(labels, lb)
    array.push(uppers, upper)
    array.push(lowers, lower)
    array.push(filled, false)
    array.push(gapUps, isGapUp)
 
// Check each gap for a fill, then extend what stays on the chart to this bar.
// A filled gap is deleted, or faded with a dotted border when showFilled is on.
// Reverse iteration keeps the indexes valid while removing.
f_update(array<box> boxes, array<line> mids, array<label> labels,
     array<float> uppers, array<float> lowers, array<bool> filled, array<bool> gapUps,
     bool showFilled, color fillColor) =>
    int i = array.size(boxes) - 1
    while i >= 0
        box   bx    = array.get(boxes,  i)
        line  midLn = array.get(mids,   i)
        label lb    = array.get(labels, i)
 
        if not array.get(filled, i)
            bool isGapUp = array.get(gapUps, i)
            if (isGapUp and low <= array.get(lowers, i)) or (not isGapUp and high >= array.get(uppers, i))
                array.set(filled, i, true)
                box.set_bgcolor(bx, f_fade(fillColor))
                box.set_border_color(bx, f_fade(i_borderColor))
                box.set_border_style(bx, line.style_dotted)
                if not na(midLn)
                    line.set_color(midLn, f_fade(i_borderColor))
                    line.set_style(midLn, line.style_dotted)
                label.set_textcolor(lb, color.new(i_labelColor, 60))
 
        if array.get(filled, i) and not showFilled
            box.delete(bx)
            line.delete(midLn)
            label.delete(lb)
            array.remove(boxes,  i)
            array.remove(mids,   i)
            array.remove(labels, i)
            array.remove(uppers, i)
            array.remove(lowers, i)
            array.remove(filled, i)
            array.remove(gapUps, i)
        else
            box.set_right(bx, time)
            if not na(midLn)
                line.set_x2(midLn, time)
            label.set_x(lb, time)
 
        i -= 1
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 6 — NEW GAPS, THEN FILLS (every bar)
// ══════════════════════════════════════════════════════════════════════════════
 
if newSession
    float gapUpper = math.max(gapFrom, open)
    float gapLower = math.min(gapFrom, open)
    int   gapTicks = int(math.round((gapUpper - gapLower) / syminfo.mintick))
    bool  isGapUp  = open > gapFrom
    if isNewWeek
        if i_showNwog and gapTicks >= i_nwogMinTicks
            f_spawn(nwog_boxes, nwog_mids, nwog_labels, nwog_uppers, nwog_lowers, nwog_filled, nwog_gapUps,
                 i_nwogHistory, gapFromX, gapUpper, gapLower, i_nwogFillColor, i_nwogShowMid, "NWOG", isGapUp)
    else
        if i_showNdog and gapTicks >= i_ndogMinTicks
            f_spawn(ndog_boxes, ndog_mids, ndog_labels, ndog_uppers, ndog_lowers, ndog_filled, ndog_gapUps,
                 i_ndogHistory, gapFromX, gapUpper, gapLower, i_ndogFillColor, i_ndogShowMid, "NDOG", isGapUp)
 
if active
    f_update(ndog_boxes, ndog_mids, ndog_labels, ndog_uppers, ndog_lowers, ndog_filled, ndog_gapUps,
         i_ndogShowFilled, i_ndogFillColor)
    f_update(nwog_boxes, nwog_mids, nwog_labels, nwog_uppers, nwog_lowers, nwog_filled, nwog_gapUps,
         i_nwogShowFilled, i_nwogFillColor)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 7 — NOTICE ABOVE THE 4 HOUR CHART
// ══════════════════════════════════════════════════════════════════════════════
 
var table notice = table.new(position.bottom_right, 1, 1)
if barstate.islast and not active
    table.cell(notice, 0, 0, "MonoIntel Opening Gaps works on 4 hour charts and below.",
         text_color=chart.fg_color, text_size=size.small)

← 返回列表