Unclosed Daily Gaps [Auto-Extending Boxes]

kamartrades · study · 60 行 · 点赞 32 · TradingView 原页

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

Pine Script

//@version=6
indicator("Unclosed Daily Gaps", overlay = true, max_boxes_count = 500)
 
// --- User Inputs ---
upGapColor     = input.color(color.new(color.teal, 80), "Gap Up Fill Color")
upBorderColor  = input.color(color.teal, "Gap Up Border Color")
downGapColor   = input.color(color.new(color.red, 80), "Gap Down Fill Color")
downBorderColor= input.color(color.red, "Gap Down Border Color")
keepClosed     = input.bool(false, "Keep Closed Gaps on Chart", tooltip = "If unchecked, boxes are removed once price fills the gap.")
 
// --- Custom Type to Track Each Gap ---
type GapBox
    box   id
    float targetClose
    bool  isGapUp
 
var array<GapBox> activeGaps = array.new<GapBox>()
 
// --- Detect New Day & Create Gap Box ---
bool isNewDay = timeframe.change("1D")
 
if isNewDay and bar_index > 0
    float prevClose = close[1]
    float currOpen  = open
 
    if currOpen != prevClose
        bool isUp      = currOpen > prevClose
        float topPrice = math.max(prevClose, currOpen)
        float botPrice = math.min(prevClose, currOpen)
        color bgCol    = isUp ? upGapColor : downGapColor
        color brdCol   = isUp ? upBorderColor : downBorderColor
 
        // Draw box starting from the previous day's closing bar (bar_index - 1)
        box newBox = box.new(
             left         = bar_index - 1,
             top          = topPrice,
             right        = bar_index,
             bottom       = botPrice,
             border_color = brdCol,
             border_width = 1,
             bgcolor      = bgCol
         )
 
        activeGaps.push(GapBox.new(newBox, prevClose, isUp))
 
// --- Update & Check Active Unclosed Gaps ---
if activeGaps.size() > 0
    for i = activeGaps.size() - 1 to 0
        GapBox currentGap = activeGaps.get(i)
        
        // Extend the box to the current bar
        currentGap.id.set_right(bar_index)
 
        // Check if price has closed the gap
        bool isClosed = currentGap.isGapUp ? (low <= currentGap.targetClose) : (high >= currentGap.targetClose)
 
        if isClosed
            if not keepClosed
                currentGap.id.delete()
            activeGaps.remove(i)

← 返回列表