MonoIntel Globex IB: Overnight Initial Balance and Extensions

monobosh · study · 334 行 · 点赞 16 · TradingView 原页

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

Pine Script

//@version=6
indicator(
    title            = "MonoIntel Globex IB: Overnight Initial Balance and Extensions",
    shorttitle       = "MI Globex IB",
    overlay          = true,
    max_lines_count  = 500,
    max_boxes_count  = 500,
    max_labels_count = 500
)
 
// Built for CME futures on charts from 1 minute to 1 hour. All times are New York.
// The Globex IB is the first overnight hour, 18:00 to 19:00. Its box and its high, low and
// midpoint lines run right until the next session opens. Extension lines are multiples of the
// IB range above the high and below the low, drawn for the first four hours of the session.
// The Sunday 18:00 IB is also drawn as the weekly IB, which stays one hour wide unless set to
// extend right until the next Sunday open.
// The IB is measured from data underneath the chart, so the high and low are the true
// 18:00 to 19:00 values even on timeframes whose bars don't split at 18:00 and 19:00 (e.g. 45m).
// Old drawings are dropped automatically once the 500 line and label limits are reached
// (about 40 sessions of lines and 45 of labels in the default Extension Mode).
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 1 — INPUTS
// ══════════════════════════════════════════════════════════════════════════════
 
// ── IB Box ───────────────────────────────────────────────────────────────────
i_showBox      = input.bool(true,                    "Show IB Box",      group="IB Box",
     tooltip="Shades the 18:00 to 19:00 range until the next session opens.")
i_boxFillColor = input.color(color.new(#089981, 90), "Fill Color",       group="IB Box",
     tooltip="Fill colour of the IB box.")
i_showTopLine  = input.bool(true,                    "Show Top Line",    group="IB Box",
     tooltip="Draws the IB high until the next session opens.")
i_showBotLine  = input.bool(true,                    "Show Bottom Line", group="IB Box",
     tooltip="Draws the IB low until the next session opens.")
i_ibLineColor  = input.color(#089981,                "Line Color",       group="IB Box",
     tooltip="Colour of the IB high and low lines and their price labels.")
i_ibLineStyle  = input.string("Solid",               "Line Style",       group="IB Box",
     options=["Solid", "Dashed", "Dotted"], tooltip="Style of the IB high and low lines.")
i_ibLineWidth  = input.int(1,                        "Line Width",       group="IB Box",
     minval=1, maxval=4, tooltip="Width of the IB high and low lines.")
 
// ── Midpoint ─────────────────────────────────────────────────────────────────
i_showMid  = input.bool(true,        "Show Midpoint", group="Midpoint",
     tooltip="Draws the middle of the IB range until the next session opens.")
i_midColor = input.color(color.gray, "Color",         group="Midpoint",
     tooltip="Colour of the midpoint line and its price label.")
i_midStyle = input.string("Dashed",  "Style",         group="Midpoint",
     options=["Solid", "Dashed", "Dotted"], tooltip="Style of the midpoint line.")
i_midWidth = input.int(1,            "Width",         group="Midpoint",
     minval=1, maxval=4, tooltip="Width of the midpoint line.")
 
// ── Extensions ───────────────────────────────────────────────────────────────
i_extMode  = input.string("1-4",    "Extension Mode", group="Extensions",
     options=["0.5-2", "0.5-4", "1-2", "1-4"],
     tooltip="Which multiples of the IB range to draw: 0.5-2 is 0.5, 1 and 2; 0.5-4 adds 3 and 4; 1-2 is 1 and 2; 1-4 is 1 to 4.")
i_extColor = input.color(#8C8C8C,   "Color",          group="Extensions",
     tooltip="Colour of the extension lines and their price labels.")
i_extStyle = input.string("Solid",  "Style",          group="Extensions",
     options=["Solid", "Dashed", "Dotted"], tooltip="Style of the daily and weekly extension lines.")
i_extWidth = input.int(1,           "Width",          group="Extensions",
     minval=1, maxval=4, tooltip="Width of the daily and weekly extension lines.")
 
// ── Weekly IB ────────────────────────────────────────────────────────────────
i_showWeeklyBox   = input.bool(true,                    "Show Weekly IB Box",     group="Weekly IB",
     tooltip="Draws the Sunday 18:00 to 19:00 range as the weekly IB.")
i_weeklyFillColor = input.color(color.new(#089981, 90), "Fill Color",             group="Weekly IB",
     tooltip="Fill colour of the weekly IB box.")
i_weeklyTextColor = input.color(#089981,                "Label Color",            group="Weekly IB",
     tooltip="Colour of the weekly IB label, midpoint and extension lines.")
i_showWeeklyMid   = input.bool(true,                    "Show Midpoint",          group="Weekly IB",
     tooltip="Draws the middle of the weekly IB range.")
i_extendWeeklyMid = input.bool(false,                   "Extend Midpoint Right",  group="Weekly IB",
     tooltip="Runs the weekly midpoint right until the next weekly IB.")
i_extendWeeklyBox = input.bool(false,                   "Extend Box Right",       group="Weekly IB",
     tooltip="Runs the weekly box right until the next weekly IB.")
i_showWeeklyExt   = input.bool(false,                   "Show Weekly Extensions", group="Weekly IB",
     tooltip="Draws the weekly IB's extension lines, using the Extension Mode multiples.")
i_extendWeeklyAll = input.bool(false,                   "Extend All Weekly Levels Right", group="Weekly IB",
     tooltip="Runs the weekly box, midpoint and extension lines right until the next weekly IB.")
 
// ── Display ──────────────────────────────────────────────────────────────────
i_showLabels = input.bool(true, "Show Price Labels", group="Display",
     tooltip="Prints the price beside the daily IB lines and extension lines.")
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 2 — CONSTANTS + HELPERS
// ══════════════════════════════════════════════════════════════════════════════
 
string TZ          = "America/New_York"
int    MS_PER_HOUR = 3600000
int    IB_START    = 18 * 60    // 18:00, minutes after midnight
int    IB_END      = 19 * 60    // 19:00
 
f_lineStyle(s) =>
    switch s
        "Dashed" => line.style_dashed
        "Dotted" => line.style_dotted
        =>          line.style_solid
 
f_inIB(int t) =>
    int m = hour(t, TZ) * 60 + minute(t, TZ)
    m >= IB_START and m < IB_END
 
f_gcd(int a, int b) =>
    int x = a
    int y = b
    while y != 0
        int r = x % y
        x := y
        y := r
    x
 
// Multiples of the IB range drawn above the high and below the low.
f_multiples(string mode) =>
    switch mode
        "0.5-2" => array.from(0.5, 1.0, 2.0)
        "0.5-4" => array.from(0.5, 1.0, 2.0, 3.0, 4.0)
        "1-2"   => array.from(1.0, 2.0)
        =>         array.from(1.0, 2.0, 3.0, 4.0)
 
f_priceLabel(int x, float y, color col) =>
    label.new(x, y, str.tostring(y, format.mintick), xloc=xloc.bar_time,
         style=label.style_none, textcolor=col, size=size.small)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 3 — DATA UNDER THE CHART
// ══════════════════════════════════════════════════════════════════════════════
// Charts whose bars split cleanly at 18:00 and 19:00 (timeframes that divide 1 hour)
// read their own bars. Anything else (e.g. 45m) reads the largest timeframe that does
// split there (15m for 45m), so the IB is always the exact 18:00 to 19:00 range.
 
int    tfSec   = timeframe.in_seconds()
bool   aligned = tfSec < 60 or 3600 % tfSec == 0
bool   active  = timeframe.isintraday and tfSec <= 3600   // 1 hour charts and below
string dataTF  = aligned or not active ? timeframe.period : str.tostring(f_gcd(tfSec, 3600) / 60)
 
[subTime, subHigh, subLow] = request.security_lower_tf(syminfo.tickerid, dataTF, [time, high, low],
     ignore_invalid_timeframe=true)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 4 — STATE
// ══════════════════════════════════════════════════════════════════════════════
 
var float ibHigh      = na
var float ibLow       = na
var int   ibStartTime = na
var bool  ibFormed    = false
var bool  isWeeklyIB  = false
var bool  prevInIB    = false
var bool  seenData    = false   // no IB starts on the first data, which may begin mid-hour
 
// The current daily session's objects, which run right until the next session opens.
var box   activeBox     = na
var line  activeTopLine = na
var line  activeBotLine = na
var line  activeMidLine = na
var label ibHighLabel   = na
var label ibLowLabel    = na
var label ibMidLabel    = na
 
// The current weekly IB's objects (Sunday 18:00 session).
var box    weeklyIBBox    = na
var line   weeklyMidLine  = na
var line[] weeklyExtLines = array.new<line>(0)
 
bool extendWeeklyBox = i_extendWeeklyBox or i_extendWeeklyAll
bool extendWeeklyMid = i_extendWeeklyMid or i_extendWeeklyAll
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 5 — WALK THE DATA
// ══════════════════════════════════════════════════════════════════════════════
 
if active
    for [k, t] in subTime
        float h    = array.get(subHigh, k)
        float l    = array.get(subLow,  k)
        bool  inIB = f_inIB(t)
 
        // ── 18:00: a new session opens ──────────────────────────────────────
        if inIB and not prevInIB and seenData
            bool thisIsWeekly = dayofweek(t, TZ) == dayofweek.sunday
 
            // A new weekly IB ends the last one. Objects set to extend stop at this
            // open; the rest keep their one hour width.
            if thisIsWeekly
                if extendWeeklyBox and not na(weeklyIBBox)
                    box.set_right(weeklyIBBox, t)
                if extendWeeklyMid and not na(weeklyMidLine)
                    line.set_x2(weeklyMidLine, t)
                if i_extendWeeklyAll
                    for ln in weeklyExtLines
                        line.set_x2(ln, t)
                weeklyIBBox   := na
                weeklyMidLine := na
                array.clear(weeklyExtLines)
            isWeeklyIB := thisIsWeekly
 
            // The previous daily session's objects stop at this open.
            if not na(activeBox)
                box.set_right(activeBox, t)
            if not na(activeTopLine)
                line.set_x2(activeTopLine, t)
            if not na(activeBotLine)
                line.set_x2(activeBotLine, t)
            if not na(activeMidLine)
                line.set_x2(activeMidLine, t)
            if not na(ibHighLabel)
                label.set_x(ibHighLabel, t)
            if not na(ibLowLabel)
                label.set_x(ibLowLabel, t)
            if not na(ibMidLabel)
                label.set_x(ibMidLabel, t)
            activeBox     := na
            activeTopLine := na
            activeBotLine := na
            activeMidLine := na
            ibHighLabel   := na
            ibLowLabel    := na
            ibMidLabel    := na
 
            ibHigh      := h
            ibLow       := l
            ibStartTime := t
            ibFormed    := false
 
            if i_showBox
                activeBox := box.new(t, h, t + MS_PER_HOUR, l, xloc=xloc.bar_time,
                     bgcolor=i_boxFillColor, border_color=na)
 
        // ── 18:00 to 19:00: build the range, growing the box live ───────────
        else if inIB and not na(ibHigh)
            ibHigh := math.max(ibHigh, h)
            ibLow  := math.min(ibLow,  l)
            if not na(activeBox)
                box.set_top(activeBox,    ibHigh)
                box.set_bottom(activeBox, ibLow)
 
        // ── First data after 19:00: the IB is final, draw its levels ────────
        if not inIB and prevInIB and not na(ibHigh)
            float ibRange = ibHigh - ibLow
            float ibMid   = ibLow + ibRange * 0.5
            int   ibEnd   = ibStartTime + MS_PER_HOUR
            int   extEnd  = ibStartTime + 4 * MS_PER_HOUR    // daily extensions run to 22:00
            array<float> mults = f_multiples(i_extMode)
 
            if i_showTopLine
                activeTopLine := line.new(ibStartTime, ibHigh, ibEnd, ibHigh, xloc=xloc.bar_time,
                     color=i_ibLineColor, style=f_lineStyle(i_ibLineStyle), width=i_ibLineWidth)
                if i_showLabels
                    ibHighLabel := f_priceLabel(ibEnd, ibHigh, i_ibLineColor)
 
            if i_showBotLine
                activeBotLine := line.new(ibStartTime, ibLow, ibEnd, ibLow, xloc=xloc.bar_time,
                     color=i_ibLineColor, style=f_lineStyle(i_ibLineStyle), width=i_ibLineWidth)
                if i_showLabels
                    ibLowLabel := f_priceLabel(ibEnd, ibLow, i_ibLineColor)
 
            if i_showMid
                activeMidLine := line.new(ibStartTime, ibMid, ibEnd, ibMid, xloc=xloc.bar_time,
                     color=i_midColor, style=f_lineStyle(i_midStyle), width=i_midWidth)
                if i_showLabels
                    ibMidLabel := f_priceLabel(ibEnd, ibMid, i_midColor)
 
            // Daily extension lines: fixed from 18:00 to 22:00, never moved again.
            for m in mults
                for lvl in array.from(ibHigh + m * ibRange, ibLow - m * ibRange)
                    line.new(ibStartTime, lvl, extEnd, lvl, xloc=xloc.bar_time,
                         color=i_extColor, style=f_lineStyle(i_extStyle), width=i_extWidth)
                    if i_showLabels
                        f_priceLabel(extEnd, lvl, i_extColor)
 
            // Weekly IB: one hour wide unless set to extend (Section 6 moves it).
            if isWeeklyIB
                if i_showWeeklyBox
                    weeklyIBBox := box.new(ibStartTime, ibHigh, ibEnd, ibLow, xloc=xloc.bar_time,
                         bgcolor=i_weeklyFillColor, border_color=na,
                         text="Weekly IB", text_color=i_weeklyTextColor,
                         text_halign=text.align_left, text_valign=text.align_top, text_size=size.small)
                if i_showWeeklyMid
                    weeklyMidLine := line.new(ibStartTime, ibMid, ibEnd, ibMid, xloc=xloc.bar_time,
                         color=i_weeklyTextColor, style=line.style_dashed, width=1)
                if i_showWeeklyExt
                    for m in mults
                        for lvl in array.from(ibHigh + m * ibRange, ibLow - m * ibRange)
                            array.push(weeklyExtLines, line.new(ibStartTime, lvl, ibEnd, lvl,
                                 xloc=xloc.bar_time, color=i_weeklyTextColor,
                                 style=f_lineStyle(i_extStyle), width=i_extWidth))
 
            ibFormed := true
 
        prevInIB := inIB
        seenData := true
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 6 — RUN THE CURRENT LEVELS RIGHT
// ══════════════════════════════════════════════════════════════════════════════
// After 19:00 the daily box, lines and labels follow the latest bar until the next
// session opens. Weekly objects follow only when set to extend. Never pulled back
// inside the IB hour, which matters on 45m charts where a bar opens at 18:45.
 
if active and ibFormed
    int xNow = math.max(time, ibStartTime + MS_PER_HOUR)
    if not na(activeBox)
        box.set_right(activeBox, xNow)
    if not na(activeTopLine)
        line.set_x2(activeTopLine, xNow)
    if not na(activeBotLine)
        line.set_x2(activeBotLine, xNow)
    if not na(activeMidLine)
        line.set_x2(activeMidLine, xNow)
    if not na(ibHighLabel)
        label.set_x(ibHighLabel, xNow)
    if not na(ibLowLabel)
        label.set_x(ibLowLabel, xNow)
    if not na(ibMidLabel)
        label.set_x(ibMidLabel, xNow)
 
if active
    if extendWeeklyBox and not na(weeklyIBBox)
        box.set_right(weeklyIBBox, math.max(time, box.get_right(weeklyIBBox)))
    if extendWeeklyMid and not na(weeklyMidLine)
        line.set_x2(weeklyMidLine, math.max(time, line.get_x2(weeklyMidLine)))
    if i_extendWeeklyAll
        for ln in weeklyExtLines
            line.set_x2(ln, math.max(time, line.get_x2(ln)))
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 7 — NOTICE ABOVE THE 1 HOUR CHART
// ══════════════════════════════════════════════════════════════════════════════
 
var table notice = table.new(position.bottom_right, 1, 1)
if barstate.islast and not active
    table.cell(notice, 0, 0, "MonoIntel Globex IB works on 1 hour charts and below.",
         text_color=chart.fg_color, text_size=size.small)

← 返回列表