MonoIntel Velocity Box: RTH Opening 30 Minutes and Extensions

monobosh · study · 238 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator(
    title            = "MonoIntel Velocity Box: RTH Opening 30 Minutes and Extensions",
    shorttitle       = "MI Velocity Box",
    overlay          = true,
    max_lines_count  = 500,
    max_boxes_count  = 500,
    max_labels_count = 500
)
 
// Built for charts from 1 minute to 1 hour. All times are New York.
// The Velocity Box is the 09:30 to 10:00 cash-open range. Its high, low and midpoint
// extend to the right until the next box, and half and full range extensions run
// from 09:30 to 16:00.
// The range is measured from 30-minute or 1-minute data underneath the chart, so it is the
// true 09:30 to 10:00 high and low on any timeframe, including 45 minute and 1 hour charts.
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 1 — INPUTS
// ══════════════════════════════════════════════════════════════════════════════
 
// ── Velocity Box ─────────────────────────────────────────────────────────────
i_showBox      = input.bool(true,                      "Show Velocity Box", group="Velocity Box",
     tooltip="Shades the 09:30 to 10:00 range.")
i_boxFillColor = input.color(color.new(color.red, 88), "Fill Color",        group="Velocity Box",
     tooltip="Fill colour of the box.")
i_showTopLine  = input.bool(true,                      "Show Top Line",     group="Velocity Box",
     tooltip="Extends the box high to the right.")
i_showBotLine  = input.bool(true,                      "Show Bottom Line",  group="Velocity Box",
     tooltip="Extends the box low to the right.")
i_vbLineColor  = input.color(color.red,                "Line Color",        group="Velocity Box",
     tooltip="Colour of the box border and the top and bottom lines.")
i_vbLineStyle  = input.string("Solid",                 "Line Style",        group="Velocity Box",
     options=["Solid", "Dashed", "Dotted"], tooltip="Style of the top and bottom lines.")
i_vbLineWidth  = input.int(1,                          "Line Width",        group="Velocity Box",
     minval=1, maxval=4, tooltip="Width of the top and bottom lines.")
 
// ── Midpoint ─────────────────────────────────────────────────────────────────
i_showMid  = input.bool(true,        "Show Midpoint", group="Midpoint",
     tooltip="Extends the middle of the box to the right.")
i_midColor = input.color(color.gray, "Color",         group="Midpoint",
     tooltip="Colour of the midpoint line.")
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_extColor = input.color(color.white, "Color", group="Extensions",
     tooltip="Colour of the half and full range extensions above and below the box.")
i_extStyle = input.string("Solid",    "Style", group="Extensions",
     options=["Solid", "Dashed", "Dotted"], tooltip="Style of the extension lines.")
i_extWidth = input.int(1,             "Width", group="Extensions",
     minval=1, maxval=4, tooltip="Width of the extension lines.")
i_extEdge  = input.bool(true,         "Grey edge", group="Extensions",
     tooltip="Draws a thin grey edge behind each extension line so white lines stay visible on a light chart. Turn off on a dark chart.")
 
// ── Display ──────────────────────────────────────────────────────────────────
i_showLabels = input.bool(false, "Show Price Labels", group="Display",
     tooltip="Shows the price at the right end of each line.")
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 2 — CONSTANTS + HELPERS
// ══════════════════════════════════════════════════════════════════════════════
 
string TZ        = "America/New_York"
int    WIN_START = 9 * 60 + 30     // 09:30, minutes after midnight
int    WIN_END   = 10 * 60         // 10:00
int    WIN_MS    = 30 * 60000      // box width, 30 minutes
int    EXT_MS    = 390 * 60000     // extensions run 09:30 to 16:00
 
f_lineStyle(s) =>
    switch s
        "Dashed" => line.style_dashed
        "Dotted" => line.style_dotted
        =>          line.style_solid
 
f_inWindow(int t) =>
    int m = hour(t, TZ) * 60 + minute(t, TZ)
    m >= WIN_START and m < WIN_END
 
// 09:30 New York on the day of t.
f_windowStart(int t) =>
    timestamp(TZ, year(t, TZ), month(t, TZ), dayofmonth(t, TZ), 9, 30)
 
f_level(float y, int x1, int x2, color col, string sty, int wid) =>
    line.new(x1, y, x2, y, xloc=xloc.bar_time, color=col, style=f_lineStyle(sty), width=wid)
 
f_price(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 09:30 and 10:00 (30m and below, dividing 30m)
// read their own bars. Anything else reads 30m bars (above 30m) or 1m bars (below),
// so the window edges are exact.
 
int    tfSec   = timeframe.in_seconds()
bool   aligned = tfSec < 60 or 1800 % tfSec == 0
bool   active  = timeframe.isintraday and tfSec <= 3600   // 1 hour charts and below
string dataTF  = aligned or not active ? timeframe.period : tfSec > 1800 ? "30" : "1"
int    dataMS  = (aligned ? tfSec : tfSec > 1800 ? 1800 : 60) * 1000
 
[subTime, subHigh, subLow] = request.security_lower_tf(syminfo.tickerid, dataTF, [time, high, low],
     ignore_invalid_timeframe=true)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 4 — STATE
// ══════════════════════════════════════════════════════════════════════════════
 
var float vbHigh   = na
var float vbLow    = na
var int   vbStart  = na      // 09:30 of the box being built
var int   prevTime = na
var bool  prevIn   = false
 
// The box being built, and the lines and labels that extend right until the next box.
var box   activeBox = na
var line  topLine   = na
var line  botLine   = na
var line  midLine   = na
var label topLabel  = na
var label botLabel  = na
var label midLabel  = na
 
// ══════════════════════════════════════════════════════════════════════════════
// 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  inWin  = f_inWindow(t)
        bool  newDay = not na(prevTime) and dayofmonth(t, TZ) != dayofmonth(prevTime, TZ)
 
        // Window just closed: lock the box and draw the levels.
        if prevIn and (not inWin or newDay) and not na(vbHigh)
            float rng = vbHigh - vbLow
            float mid = vbLow + rng * 0.5
            int   ext = vbStart + EXT_MS
 
            if not na(activeBox)
                box.set_right(activeBox, vbStart + WIN_MS)
 
            if i_showTopLine
                topLine  := f_level(vbHigh, vbStart, time, i_vbLineColor, i_vbLineStyle, i_vbLineWidth)
                topLabel := i_showLabels ? f_price(time, vbHigh, i_vbLineColor) : na
            if i_showBotLine
                botLine  := f_level(vbLow, vbStart, time, i_vbLineColor, i_vbLineStyle, i_vbLineWidth)
                botLabel := i_showLabels ? f_price(time, vbLow, i_vbLineColor) : na
            if i_showMid
                midLine  := f_level(mid, vbStart, time, i_midColor, i_midStyle, i_midWidth)
                midLabel := i_showLabels ? f_price(time, mid, i_midColor) : na
 
            // Range extensions, fixed from 09:30 to 16:00.
            for m in array.from(0.5, 1.0)
                float up = vbHigh + m * rng
                float dn = vbLow  - m * rng
                if i_extEdge
                    f_level(up, vbStart, ext, color.new(#808080, 30), i_extStyle, i_extWidth + 2)
                    f_level(dn, vbStart, ext, color.new(#808080, 30), i_extStyle, i_extWidth + 2)
                f_level(up, vbStart, ext, i_extColor, i_extStyle, i_extWidth)
                f_level(dn, vbStart, ext, i_extColor, i_extStyle, i_extWidth)
                if i_showLabels
                    f_price(ext, up, i_extColor)
                    f_price(ext, dn, i_extColor)
 
            vbHigh    := na
            vbLow     := na
            activeBox := na
 
        // Window just opened: stop the previous day's lines here and start a new box.
        // Not on the first data, which may begin part-way through the window.
        if inWin and not na(prevTime) and (not prevIn or newDay)
            int s = f_windowStart(t)
            if not na(topLine)
                line.set_x2(topLine, s)
            if not na(botLine)
                line.set_x2(botLine, s)
            if not na(midLine)
                line.set_x2(midLine, s)
            if not na(topLabel)
                label.set_x(topLabel, s)
            if not na(botLabel)
                label.set_x(botLabel, s)
            if not na(midLabel)
                label.set_x(midLabel, s)
            topLine  := na
            botLine  := na
            midLine  := na
            topLabel := na
            botLabel := na
            midLabel := na
 
            vbStart := s
            vbHigh  := h
            vbLow   := l
            if i_showBox
                activeBox := box.new(s, h, math.min(t + dataMS, s + WIN_MS), l, xloc=xloc.bar_time,
                     bgcolor=i_boxFillColor, border_color=i_vbLineColor)
 
        // Inside the window: grow the box.
        else if inWin and not na(vbHigh)
            vbHigh := math.max(vbHigh, h)
            vbLow  := math.min(vbLow,  l)
            if not na(activeBox)
                box.set_top(activeBox,    vbHigh)
                box.set_bottom(activeBox, vbLow)
                box.set_right(activeBox,  math.min(t + dataMS, vbStart + WIN_MS))
 
        prevTime := t
        prevIn   := inWin
 
    // Carry the open lines and labels to the current bar.
    if not na(topLine)
        line.set_x2(topLine, time)
    if not na(botLine)
        line.set_x2(botLine, time)
    if not na(midLine)
        line.set_x2(midLine, time)
    if not na(topLabel)
        label.set_x(topLabel, time)
    if not na(botLabel)
        label.set_x(botLabel, time)
    if not na(midLabel)
        label.set_x(midLabel, time)
 
// ══════════════════════════════════════════════════════════════════════════════
// SECTION 6 — 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 Velocity Box works on 1 hour charts and below.",
         text_color=chart.fg_color, text_size=size.small)

← 返回列表