Hourly Open Box

Remigius1974 · study · 41 行 · 点赞 2 · TradingView 原页

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

Pine Script

//@version=6
indicator("Hourly Open Box", "Hourly Open Box", overlay=true, max_boxes_count=100)
 
// --- Inputs ---
int   winLen   = input.int(15, "Window length (minutes)", minval=1, maxval=59, step=1)
int   maxBoxes = input.int(24, "Keep last N boxes", minval=1, maxval=100, step=1)
color colBox   = input.color(#2962FF, "Box color")
 
// Oldest-first history of windows (finished + the forming one)
var box[] boxes = array.new_box()
var box   active = na
var int   winStartBar = -1
var float lo = na
var float hi = na
var bool  inWinPrev = false
 
// Window = first `winLen` minutes of each hour, judged by bar open time.
// Works on any timeframe: the box covers exactly the bars whose open falls in the window
bool inWin  = minute(time) < winLen
bool newWin = inWin and not inWinPrev
 
if newWin and winStartBar == -1
    // First tick of a fresh window: open the box over the first candle
    winStartBar := bar_index
    lo := low
    hi := high
    active := box.new(bar_index, high, bar_index, low, border_color=color.new(colBox, 20), border_width=1, bgcolor=color.new(colBox, 60))
    array.push(boxes, active)
    while array.size(boxes) > maxBoxes
        box.delete(array.shift(boxes))
else if inWin
    // Bar (or tick) inside the window: extend the box and track the running min/max
    lo := math.min(lo, low)
    hi := math.max(hi, high)
    box.set_right(active, bar_index)
    box.set_top(active, hi)
    box.set_bottom(active, lo)
if not inWin
    // Bar outside the window: the next window may open its own box
    winStartBar := -1
inWinPrev := inWin

← 返回列表